DeepSeek API Setup: Make Your First Model Request
Set up the DeepSeek API to send a prompt to a model and receive a response from your own code. You’ll create an API key, store it safely, and make a first request using Python or curl; you’ll need internet access and a DeepSeek account with API access.
What You’ll Need
- A DeepSeek account and access to its API platform.
- An API key created in the platform. Treat it like a password.
- Python 3 and the
requestspackage, or curl in a terminal. The curl example does not require Python. - A network connection, and any required API balance or billing setup shown in your account.
Step-by-Step
- Open the DeepSeek API platform. Sign in to the official DeepSeek platform and find the API keys or account settings area. The platform interface can change, so follow its current labels. If API access or billing needs to be enabled for your account, complete that first.
- Create a key. Choose the option to create a new API key. Copy it when it is displayed and store it in a password manager or a local environment variable. Some platforms show a key only once. Do not paste it into public code, a shared document, a screenshot, or a client-side website or app.
- Check the current API reference. DeepSeek’s API is designed to be compatible with the OpenAI chat-completions format. The commonly documented endpoint is
https://api.deepseek.com/chat/completions, and the chat-completions path may also be used with the base URLhttps://api.deepseek.com. Confirm the current endpoint, model names, and request options in DeepSeek’s documentation before relying on them; these can change. - Set up Python if you prefer it. Check that Python is installed by running
python --versionor, on some systems,python3 --version. Install the HTTP library withpython -m pip install requests(usepython3 -m pipif that is your Python command). You can skip this step if you will use curl. - Make the API key available to your terminal. Set an environment variable named
DEEPSEEK_API_KEY. In macOS or Linux, runexport DEEPSEEK_API_KEY='your-key-here'in the terminal you will use. In PowerShell, run$env:DEEPSEEK_API_KEY='your-key-here'. Replace the example text with your key. These commands apply to the current terminal session; avoid putting the real key in a script committed to source control. - Send a first request with Python. Save this as
first_request.py, then run it in the same terminal where you set the variable. Replacedeepseek-chatwith a model name currently listed in DeepSeek’s documentation if needed.import os import requests api_key = os.environ.get("DEEPSEEK_API_KEY") if not api_key: raise RuntimeError("Set the DEEPSEEK_API_KEY environment variable first") response = requests.post( "https://api.deepseek.com/chat/completions", headers={"Authorization": f"Bearer {api_key}"}, json={ "model": "deepseek-chat", "messages": [ {"role": "user", "content": "Explain what an API is in one sentence."} ], "stream": False }, timeout=60 ) response.raise_for_status() data = response.json() print(data["choices"][0]["message"]["content"])The request includes a bearer authorization header, the model, and a user message. A successful chat-completions response contains the generated text under
choices[0].message.content. Do not assume every response contains that field if the API returns an error or a different response type. - Or try curl. Run this command in a terminal after setting the environment variable. The JSON payload is quoted for common macOS/Linux shells; PowerShell quoting rules may differ, so use the Python example if the command is troublesome.
curl https://api.deepseek.com/chat/completions \ -H "Authorization: Bearer $DEEPSEEK_API_KEY" \ -H "Content-Type: application/json" \ -d '{"model":"deepseek-chat","messages":[{"role":"user","content":"Explain what an API is in one sentence."}],"stream":false}'In PowerShell, the variable reference in a curl command may be interpreted differently, and
curlmay be an alias depending on the setup. Usecurl.exeand adapt quoting, or use Python. - Confirm the result. Look for a successful HTTP response and the model’s text. If you receive a structured error, read its message rather than treating it as generated content. Make a short, non-sensitive test prompt first; then adjust the prompt or model once the connection works.
Common Mistakes
- Using an invalid or old model name: Model IDs can change. Copy one from the current API documentation instead of relying on a name from an old tutorial.
- Sending the key in the wrong place: The key belongs in the
Authorization: Bearer ...header, not in the URL or prompt. - Running in a different terminal: Environment variables set for one session may not exist in another. Set the variable again in the terminal that runs the script.
- Printing or committing the key: Avoid logging request headers, embedding keys in repositories, or exposing them in browser code. If a key is exposed, revoke it in the platform and create a replacement.
- Expecting a plain-text response: Chat completions return structured JSON. Extract the message content from the response rather than printing the whole object if you only want the answer.
- Ignoring usage and limits: API requests may consume account credits or be subject to rate limits. Check account usage and current platform terms before running repeated or large requests.
Tips
- Start with one short request and a modest prompt. Add more messages or options only after the basic call succeeds.
- Keep the API key in environment variables or a secrets manager, and use a separate key for each project when practical.
- Set a timeout in code so a stalled network request does not wait indefinitely. For a real application, also handle connection failures and API error responses explicitly.
- For streamed responses, the server sends partial output rather than one complete JSON response. Keep
stream: falsefor this first test, then consult the documentation before implementing streaming. - Check the official API reference for model capabilities, context limits, supported parameters, and current endpoint details. Compatibility with a familiar request format does not mean every option behaves identically.
Troubleshooting
401 or authentication error: Confirm that the key is copied correctly, has not been revoked, and is being sent as a bearer token. Check that the environment variable is set in the active terminal without extra spaces or quotation marks included in its value.
404 or model-not-found error: Verify the endpoint path and model ID against the current DeepSeek documentation. Do not assume the model name in an example remains available.
429 or rate-limit response: Slow down repeated requests, check account limits and usage, and follow any retry guidance in the response. Avoid tight retry loops, which can create more failures.
Timeout, DNS, or connection failure: Check your internet connection, proxy or firewall settings, and whether the API host is reachable from your network. Retry once after confirming connectivity; do not repeatedly resend a request without considering that it may already have completed.
Python cannot find requests or the key: Install the package using the same Python executable that runs the script. Re-enter the environment-variable command in that terminal, then run the script again. If an HTTP error occurs, inspect response.status_code and response.text while taking care not to reveal credentials.
Frequently Asked Questions
Is the DeepSeek API the same as using the chat website?
The API is for software to send requests programmatically. It uses an API key and may have separate access, usage limits, or billing from a consumer-facing chat service.
Do I need to install a special DeepSeek SDK?
Not for this example. A standard HTTP client such as Python’s requests or curl can send the request. Check the current documentation for any official client libraries.
Which model should I use?
Choose a model ID currently supported by the API and suitable for your task. Model availability and capabilities can change, so check DeepSeek’s current model documentation.
Can I call the API directly from a web page?
A browser-based page can expose its API key to visitors. Keep the key on a server you control and have the browser call your server instead.
Why does my request return JSON instead of just text?
The endpoint returns a structured response with choices and message metadata. Read the message content field to retrieve the generated text.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.