AI and model APIs

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

Anthropic API keys are created on the API keys page of the Claude Console (platform.claude.com/settings/keys) and sent as Authorization: Bearer <key> (the legacy x-api-key header still works). SDKs read the ANTHROPIC_API_KEY environment variable. You choose an expiry from 3 hours to Never when you create the key.

Anthropic key types

TypePrefixSecret?ScopeExpiry
Personal key
Archived when you are removed from the organization.
sk-ant-apiYesActs as you, with your roles and permissions, in one workspace or the workspaces your role allowsChosen at creation (3 hours, 1 day, 7 days, 30 days, custom or Never); stops working if you lose access to the organization
Service account key
Recommended for shared or automated workloads such as CI and production services.
sk-ant-apiYesActs as a service account, in one workspace or anything the service account can accessChosen at creation; stops working when the service account is archived
Workspace key (legacy)
Still works but Anthropic calls it legacy and prefers identity-backed keys or Workload Identity Federation.
sk-ant-apiYesThe single workspace it was created inExpires, or is disabled or deleted, or its workspace is archived

How to create a Anthropic key

  1. Sign in to the Claude Console.
  2. Open the API keys page (platform.claude.com/settings/keys) and click Create key.
  3. Name the key and pick an expiration.
  4. Set Linked account to yourself for a personal key, or to a service account for a shared key.
  5. Optionally scope the key to one workspace, then copy it.

Open Anthropic's key settings

Limiting what the key can do

A key can be scoped to a single workspace. A key not scoped to a workspace must send the anthropic-workspace-id header on every request. Use workspaces to separate environments and control spend.

How to rotate a Anthropic key

  1. Create a new key on the API keys page, scoped to the integration's workspace.
  2. Deploy it wherever the old one is read, typically ANTHROPIC_API_KEY or a secrets manager entry.
  3. Confirm requests succeed, then delete the old key.

Expiration is set at creation and cannot be changed afterwards. Anthropic emails the creator before expiry for keys with a lifetime of at least 7 days.

How to revoke a Anthropic key

  1. Open the API keys page in the Claude Console.
  2. Click Disable to turn a key off reversibly (it can be re-enabled), or Delete to archive it permanently.

If your Anthropic key leaked

  1. Delete the exposed Anthropic API key on the API keys page (three-dot menu, Delete API key), or Disable it first if you must investigate.
  2. Create a replacement with Create key and store it in a secrets manager, not in version control.
  3. Check the Logs and Usage pages in the Claude Console for requests you do not recognise.
  4. Set spend limits on the Billing page (or turn on auto-reload with a careful threshold) so a leaked key cannot run up unexpected usage.
  5. Prefer a short expiration or Workload Identity Federation for the new credential.
  6. Contact Anthropic Support if suspicious API activity continues. Anthropic is a GitHub secret scanning partner, so when GitHub finds an Anthropic API key in a public repository, Anthropic deactivates it and emails the account holder.

Environment variables and CLI

Anthropic's SDKs and docs read the key from ANTHROPIC_API_KEY.

Anthropic CLI (ant): Reads ANTHROPIC_API_KEY from the environment

Things that catch people out

  • Expired keys return 401 authentication_error and cannot be reactivated.
  • A key not scoped to a workspace is rejected with a 400 unless the anthropic-workspace-id header is sent.
  • Disable is reversible; Delete is permanent.
  • Anthropic suggests rotating keys on a fixed schedule, such as every 90 days.

Common Anthropic authentication errors

authentication_error

HTTP 401. The key is malformed, revoked or expired.

permission_error

HTTP 403. The key does not have permission to use the requested resource.

Recent incidents that put Anthropic keys at risk

If you were affected by one of these, rotate your Anthropic keys using the rotation steps above.

Using your Anthropic key with AI agents

A coding agent that can read your .env file or your shell history can read the key, and anything in its context can end up in logs, transcripts or a prompt-injected request. The safer pattern is to keep the key in a credential broker that performs the authenticated step for the agent and hands back only the result.

BotPassword works this way. For a Anthropic key, these intents fit:

IntentWhat BotPassword does
run_withRuns 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_requestMakes 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.
rotatePartly built: creates a new key, stores it as pending, verifies it, promotes it, then revokes the old key after a grace period. Today it works for Cloudflare tokens and AWS IAM keys you have set up, and an agent can start it only under a grant with autopilot; other providers cannot be rotated through their API yet.
store_secretSaves a value the agent already holds as a new vault item.
capture_secretPlanned, not built yet: will read a newly created key straight from the page or command output into the vault.

Set it up in your agent: Aider · Amp · Claude Code · Claude Desktop · Cline · OpenAI Codex CLI · all agents

Frequently asked questions

Where do I find my Anthropic API key?

Create one at platform.claude.com/settings/keys in the Claude Console. Existing keys cannot be shown again.

What is the Anthropic API key environment variable?

The SDKs read ANTHROPIC_API_KEY.

Do Anthropic API keys expire?

You choose at creation: a preset (3 hours, 1 day, 7 days, 30 days), a custom duration, or Never. Your organization may cap the maximum.

What should I do if my Anthropic API key is compromised?

Delete the key on the API keys page of the Claude Console, create a replacement, and check the Logs and Usage pages. Contact Anthropic Support if suspicious activity continues.

What happens if I push an Anthropic API key to GitHub?

Anthropic is a GitHub secret scanning partner. GitHub notifies Anthropic when it finds an Anthropic API key in a public repository, and Anthropic deactivates the key and emails the account holder.

What is the difference between disabling and deleting an Anthropic API key?

Disabling turns the key off and can be undone; deleting archives it permanently.

Can an AI agent use my Anthropic 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, store_secret, so the agent gets the result and never the value.

Stop pasting Anthropic keys into agents

BotPassword is in early access. Request an invite and we'll email you when your spot opens.