How to Get and Use a DeepSeek API Key
Where the key comes from
Create a free API key on the DeepSeek Platform. Paid API usage draws from your available account balance.
- Sign in at platform.deepseek.com.
- Open the API keys section and create a key. Copy it when it is shown — treat it like a password, because anyone holding it can spend your balance.
- Check your balance before making a request and top up if needed. DeepSeek uses eligible granted credit before funds you add.
- Put the key in an environment variable (
DEEPSEEK_API_KEY) on the server, never in browser code or a public repository.
DeepSeek API base URLs and endpoints
Use the same DeepSeek key across the three interfaces. Set the SDK's base URL to the value below. For raw HTTP, append the request path to that base URL.
| Interface | SDK base URL | HTTP request path |
|---|---|---|
| OpenAI Chat Completions | https://api.deepseek.com |
/chat/completions |
| OpenAI Responses | https://api.deepseek.com |
/responses |
| Anthropic Messages | https://api.deepseek.com/anthropic |
/v1/messages |
Supported fields and tools vary by interface. For example, DeepSeek’s Responses API requires the conversation input on each request. Chat Completions reference · Responses API guide · Anthropic API guide.
The two model names are deepseek-flash and deepseek-v4-pro. The older name deepseek-v4-flash is still accepted and billed at the Flash price, so old code does not break.
A first request
import os
from openai import OpenAI
client = OpenAI(
api_key=os.environ["DEEPSEEK_API_KEY"],
base_url="https://api.deepseek.com",
)
r = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": "Say hello in five words."}],
)
print(r.choices[0].message.content)
print(r.usage.model_dump())
For a plain HTTP request, send the same JSON body to https://api.deepseek.com/chat/completions with an Authorization: Bearer $DEEPSEEK_API_KEY header. A successful response confirms that your key, base URL and model worked for that request. If a later request fails, check the HTTP status and error message before retrying.
Read your token usage
The Python example prints the response's usage object. Use prompt_tokens for input. It is the sum of prompt_cache_hit_tokens and prompt_cache_miss_tokens. Use completion_tokens for output, including reasoning. The completion_tokens_details.reasoning_tokens field is already included in that total.
Sample requests from the workload you want to price. Divide total prompt tokens and total completion tokens by the number of sampled requests to get the two per-request averages. For cache hit rate, divide total cached input tokens by total prompt tokens and multiply by 100.
For example, three requests with 6,000 prompt tokens, 3,600 cached input tokens and 1,500 completion tokens in total give you 2,000 input tokens per request, 500 output tokens per request and a 60% cache hit rate. Enter those averages and your expected monthly request count in the cost calculator. See the Chat Completions usage fields.
To see how changing the start of a prompt affects those fields, run the context caching example.
What the key costs you before you ship
DeepSeek charges different rates for cached input, uncached input and output. Reusing an input prefix can help when it results in a cache hit. Peak rates apply from 01:00–04:00 and 06:00–10:00 UTC, Monday through Friday, excluding Chinese public holidays. Off-peak token rates are half the peak rates. Estimate your workload with the pricing calculator and confirm rates in DeepSeek’s official pricing table.
How to troubleshoot DeepSeek API errors
- 400 or 422 — request format or parameters. Read the error message and check the JSON body, model name and options supported by the endpoint.
- 401 — authentication failed. Check that your client is sending an active key in the
Authorization: Bearer ...header. - 402 — insufficient balance. Check your available account balance and top up if needed.
- 429 — rate limit reached. Reduce concurrency or slow down your requests, then retry with bounded backoff.
- 500 or 503 — service error or overload. Wait briefly before retrying. If the issue persists, check DeepSeek’s support guidance.
See DeepSeek’s official error codes for additional statuses and remedies.
Frequently asked questions
Is a DeepSeek API key free?
Yes. Creating a key on the DeepSeek Platform is free. API use draws from your available account balance. If your account has granted credit, DeepSeek uses that credit before the funds you add.
How much does a DeepSeek API key cost to use?
A key is free to create. API use is prepaid: charges come out of your account balance and are billed per token — cached input, uncached input and output all at different rates. Peak hours cost twice the off-peak rate for the same tokens.
Work out your monthly bill in the DeepSeek API pricing calculator →
Can I keep using the OpenAI SDK?
Yes. Set base_url to https://api.deepseek.com and use deepseek-flash or deepseek-v4-pro. The SDK supports both Chat Completions and Responses. Check DeepSeek’s documentation for the fields and tools each endpoint accepts.
What is the difference between the two base URLs?
Use https://api.deepseek.com for OpenAI Chat Completions and Responses. Use https://api.deepseek.com/anthropic for Anthropic Messages. The same DeepSeek key authenticates across all three interfaces.
Why does my request fail after a few hundred calls?
Check the HTTP status and error message first. If DeepSeek returns 429, reduce concurrency or slow down requests, then retry with backoff. The published account limits are 2,500 concurrent requests for deepseek-flash and 500 for deepseek-v4-pro.