# DeepSeek API keys: find, store, rotate, and use them safely with AI agents

Source: https://botpassword.com/keys/deepseek
Last checked: 2026-11-08

DeepSeek API keys are created on the API keys page of the DeepSeek Platform (`platform.deepseek.com/api_keys`) and sent as `Authorization: Bearer <key>` to `https://api.deepseek.com`. DeepSeek's own examples read the key from the `DEEPSEEK_API_KEY` environment variable and use the OpenAI SDK with a changed base URL. The same key also works on DeepSeek's Anthropic-format endpoint, `https://api.deepseek.com/anthropic`.

## Key types

| Type | Prefix | Secret | Scope | Expiry |
|---|---|---|---|---|
| DeepSeek API key | — | Yes | Every model and endpoint on the DeepSeek account; usage is billed against the account balance | — |

## How to create a DeepSeek key

1. Sign in to the DeepSeek Platform at `platform.deepseek.com`.
2. Top up the account balance; DeepSeek rejects requests with HTTP 402 when the balance is empty.
3. Open the **API keys** page (`platform.deepseek.com/api_keys`) and create a new key.
4. Copy the DeepSeek API key and store it in `DEEPSEEK_API_KEY` or a secrets manager.

## Limiting what the key can do

DeepSeek API keys carry no documented permission scopes, model restrictions or spending caps. Every DeepSeek key can call every model the account can use and spends from the same balance. To separate workloads, give each application its own DeepSeek key so one can be deleted without touching the others, and keep the account balance low enough that a leaked key cannot spend much. The `user_id` request field isolates your own end users for concurrency and content-safety purposes; it is not an access control on the key.

## How to rotate a DeepSeek key

1. Create a second DeepSeek key on the API keys page.
2. Replace the old value in `DEEPSEEK_API_KEY` (or `ANTHROPIC_API_KEY` for Anthropic-format clients) wherever it is deployed.
3. Confirm requests succeed with the new key, then delete the old key.

## How to revoke a DeepSeek key

1. Sign in to the DeepSeek Platform and open the **API keys** page.
2. Delete the DeepSeek key; requests that use it then fail with HTTP 401 Authentication Fails.

## If the key leaked

1. Delete the exposed DeepSeek key on the API keys page.
2. Create a replacement key and deploy it to every service that used the old one.
3. Check the usage and billing pages for spend you do not recognise, since any DeepSeek key spends the shared account balance.
4. Keep only a small balance on the account until the leak is understood.

## Environment variables

- `DEEPSEEK_API_KEY`

## Gotchas

- A 402 Insufficient Balance error looks like a key problem but is a billing one: the DeepSeek key is valid and the account needs funds.
- Concurrency limits apply to the whole DeepSeek account regardless of which key is used, so creating more keys does not raise throughput.
- DeepSeek keys have no documented scopes or spending caps, so any leaked key can spend the full account balance.
- DeepSeek exposes an OpenAI-format base URL (`https://api.deepseek.com`) and an Anthropic-format one (`https://api.deepseek.com/anthropic`); pointing an SDK at the wrong one fails even with a valid key.

## Using this key with AI agents (BotPassword)

- `run_with`: Runs a command with the key injected as an environment variable or a 0600 temporary file, never on the command line, and redacts the key from the output.
- `http_request`: Makes an authenticated API call after checking the destination against the hosts this key may be sent to. The response comes back with credential-shaped values redacted, and one that holds the key itself is refused.

## FAQ

### Where do I get a DeepSeek API key?

Create one on the API keys page of the DeepSeek Platform at `platform.deepseek.com/api_keys`. Add funds to the account first, because requests from an account with no balance fail with HTTP 402.

### What is the DeepSeek API key environment variable?

DeepSeek's documentation sets `DEEPSEEK_API_KEY` and reads it in its Python (`os.environ.get('DEEPSEEK_API_KEY')`) and Node examples. Anthropic-format clients read the same DeepSeek key from `ANTHROPIC_API_KEY` with `ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic`.

### Can I use a DeepSeek API key with the OpenAI SDK?

Yes. DeepSeek says its API format is compatible with OpenAI, so set the OpenAI client's `base_url` to `https://api.deepseek.com` and pass the DeepSeek key as the API key.

### How do I use a DeepSeek key with Claude Code or another coding agent?

DeepSeek offers an Anthropic-compatible endpoint, so tools built on the Anthropic SDK can use it by setting `ANTHROPIC_BASE_URL=https://api.deepseek.com/anthropic` and supplying the DeepSeek key. DeepSeek accepts the key in either the `x-api-key` or the `Authorization: Bearer` header there, and maps Claude model names to DeepSeek models.

### Why does my DeepSeek API key return 401?

DeepSeek returns `401 Authentication Fails` when the key is incorrect, deleted or not sent. Check the `Authorization: Bearer` header and create a new key if the old one was deleted.

### Can an AI agent use my DeepSeek key without seeing it?

Yes, if the key never enters the agent's context. Instead of pasting the key into a chat or leaving it in a `.env` file the agent can read, keep it in a credential broker that injects it into the command or request on the agent's behalf. BotPassword does this with `run_with`, `http_request`, so the agent gets the result and never the value.

## Sources

- https://api-docs.deepseek.com/
- https://api-docs.deepseek.com/quick_start/error_codes
- https://api-docs.deepseek.com/quick_start/rate_limit
- https://api-docs.deepseek.com/guides/anthropic_api
