Skip to content

Manage keys and identities across environments

Pick the credential setup for a service, pipeline or person (key type, workspace, expiry or federation), and respond to a leaked key in the right order.

Scenario

A developer pastes a config file into a public gist to ask for help. It holds the API key that the production chatbot, the staging build and two nightly jobs all share.

Disabling the key stops the abuse, and also every one of those four services at once. Most of this lesson is about never being in that spot.

Explanation

Choose the credential by who or what uses it. The docs describe API keys (personal or service account), Workload Identity Federation and App Attest:

Credential Fits
Personal key Your own development and scripts. Acts as you, and stops working when you leave
Service account key Shared or automated workloads (CI, production services). Acts as a service account, so it survives staff changes
Workload Identity Federation Workloads with a platform-issued identity: AWS, Google Cloud, Kubernetes, CI such as GitHub Actions, or another OIDC issuer. Short-lived tokens, no static secret
App Attest iOS and macOS apps you distribute that call the API directly, with short-lived tokens limited to the Messages API

Workspace keys still work but are legacy; the docs prefer identity-backed keys or federation.

One key per service and environment. Create workspaces for development, staging and production, each with its own spend and rate limits, and scope each key to a single workspace. Then a leak or a runaway test touches one environment, and usage reports show which service spent what.

Keep static keys out of code. Store them in a secrets manager and let the SDK read ANTHROPIC_API_KEY from the environment. Set an expiration when you create a key; it limits how long a leaked key works, but doesn't replace secret hygiene.

Prefer federation for workloads. With Workload Identity Federation the workload's own JWT is exchanged for a short-lived access token (an hour by default; each rule sets it between one minute and 24 hours), which the SDK refreshes. A federation rule says which tokens (issuer, subject, claims) may act as which service account, with which scope. There is no static secret to store, rotate or leak. It is only as strong as the identity provider that signs the JWT.

When a key leaks:

  1. Disable it in the Console (reversible) or delete it (permanent).
  2. Create a replacement, deploy it where the service reads its key, confirm requests succeed.
  3. Delete the old key if you only disabled it.
  4. Check what the leaked key was used for: the Usage and Cost API can filter by api_key_ids.

Worked example

Before: one workspace key named prod, used by the support chatbot, the staging build, a nightly report job on GitHub Actions and an analyst's notebook.

After:

User Credential
Support chatbot (production, Kubernetes) Federation rule matching its Kubernetes service account, mapped to a service account in the Production workspace
Staging build Service account key scoped to the Staging workspace, in the secrets manager
Nightly report job Federation rule matching the repository's main branch
Analyst's notebook The analyst's personal key, Development workspace, 30-day expiry

Now a leaked staging key stops only staging, the two workloads have no key to leak, and the analyst's key ends with the analyst's access.