In short

  • A key is a bearer credential: whoever holds it can spend your credits until it is disabled.
  • Keep keys in environment variables or a secrets manager, never in source code or client-side apps.
  • Use personal keys for yourself, service account keys for shared workloads, and scope each key to one workspace.
  • Set expirations by default; rotate by creating, deploying, then disabling the old key.

Most incidents with API keys are not dramatic. A key slips into a commit, a screenshot, or a support ticket; a few days later the bill looks wrong. The defense is a set of small habits rather than a single tool, and the Anthropic Console, the dashboard at platform.claude.com, provides every control they rely on.

Why keys deserve care

A Developer API key is a bearer credential. The API does not ask who is sending a request; it asks whether the key is valid and what that key is allowed to do. Anyone who obtains a key can spend your credits until the key is disabled, expires, or its workspace is archived. Anthropic scans public repositories and automatically deactivates keys it finds exposed there, but that is a safety net, not a plan.

Choose the right kind of key

When you create a key, the Linked account setting decides what kind it is:

  • Personal key. Acts as you, with your roles, and stops working the day you leave the organization. The right choice for tools that only you run.
  • Service account key. Acts as a service account, so it keeps working when people come and go. The right choice for shared or unattended workloads such as scheduled jobs and servers. Only organization admins can create one.
  • Workspace key (legacy). Acts as no one and lives only in the workspace where it was created. Anthropic advises choosing one of the other two for new integrations.

A separate family of credentials, Admin API keys, manages the organization rather than calling Claude. They carry broad powers and cannot invoke a model, so they should never be used where a developer key belongs.

Before reaching for a key at all, check whether you need one. On your own computer, signing in with the ant CLI gives you a short-lived token instead of a long-lived secret. In CI or on a cloud with an identity provider, Workload Identity Federation does the same for a service account. A Developer API key is for servers with no identity provider, and for tools that simply ask for a key.

Store it outside the code

A key that appears in source code is in version history forever, even after it is deleted. Load it at runtime instead. All official SDKs read ANTHROPIC_API_KEY from the environment automatically, so no file in the repository needs to hold the secret:

app.pyPython
import anthropic  # reads ANTHROPIC_API_KEY, so no secret is stored in this file

client = anthropic.Anthropic()

On servers, inject the variable from a secrets manager such as AWS Secrets Manager, Google Secret Manager, or HashiCorp Vault. On a developer’s machine, a local .env file is convenient, provided it never leaves that machine.

.gitignoreGit
# never commit local environment files
.env
.env.*

For extra assurance, add a secret scanner such as gitleaks or GitHub’s push protection. They catch the most common pattern of all: a key pasted into a file and committed before anyone notices.

Never expose a key to users

Anything a browser or a phone can read, a user can read. Anthropic’s guidance is explicit: call the Claude API from your own back end, not from code that runs in your users’ browsers. The front end calls your server; your server holds the credential and calls Anthropic. The same applies to mobile apps, where a bundled key can be extracted from the binary. Keep keys out of prompts as well, because anything in a prompt can end up in a log.

Expire and rotate

Every key can be given an expiration when it is created: presets of 3 hours, 1 day, 7 days, or 30 days, a custom date and time, or Never. The choice cannot be changed afterward. If a key will last at least three days, Anthropic emails its creator before it expires. An expired key returns a 401 authentication_error and can only be deleted, so plan the replacement in advance.

Rotation is a four-step routine:

  1. Create a new key with the same scope and a fresh expiration.
  2. Deploy it wherever the old key was used, usually the environment variable or the secrets manager entry.
  3. Disable the old key on the API keys page of the Anthropic Console. Disable is reversible; Delete is not.
  4. Confirm that requests still succeed, then delete the old key.

Teams that want to automate the audit can use the Admin API, which lists keys and reports each key’s expires_at and status. The API never returns the secret itself; keys are only ever created by a person in the Console.

Apply the principle of least privilege

Least privilege means a key should be able to do its job and nothing more. In practice:

  • One key per environment and per service. Separate keys for development, staging, and production mean a leaked test key cannot touch the production budget, and the Usage page can attribute spend to each service.
  • Scope keys to a single workspace. A key scoped to one workspace cannot act in any other. If a key truly must work across several, scope it to the organization and send the anthropic-workspace-id header on every request.
  • Match the role to the need. Members with billing-only roles cannot call the API at all, and an organization admin can turn off key creation for everyone on the Security page of the Console.
  • Cap the blast radius with spend limits. Give each project its own workspace and set a monthly spend limit for it. Per-key spend limits are not a Console feature, so workspaces are the way to ring-fence a budget.
  • Restrict where a key works. IP allowlisting, in public beta for Claude Platform organizations and Enterprise plans, rejects requests from addresses outside your allowlist with a 403.

If a key leaks

  1. Disable the key on the API keys page right away. Requests using it begin failing immediately.
  2. Look at the Usage page, filtered to that key, for spend you do not recognize.
  3. Create a replacement, deploy it, and update every service that depended on the old key.
  4. Delete the leaked key. Anthropic’s advice is to delete rather than re-enable, because you cannot know who copied it.
  5. Find out how it leaked and close that path: add the file to .gitignore, turn on scanning, and give the next key a shorter expiration.

A checklist for new projects

  • Key loaded from the environment or a secrets manager
  • .env listed in .gitignore; secret scanning enabled
  • No key in front-end code, mobile bundles, or prompts
  • Separate keys per environment and per service
  • Keys scoped to one workspace; the project has its own workspace and spend limit
  • Expiration set on every key; a rotation date on the calendar
  • Only the people who need it can create keys

Official sources