Claude API Key Storage Best Practices
If you're asking how to store a Claude API key safely, the short answer is: never in source code or client-side bundles, always loaded from a secrets store or environment variable at runtime, scoped as narrowly as possible, and rotated on a schedule with a documented revocation path.
The longer answer matters because a leaked Claude API key isn't just an inconvenience — it's a billing and data-exposure risk. Anyone holding the key can run completions against your account, read your usage history, and in some setups access conversation data. The practices below cover the full lifecycle: where keys live, how they're accessed, who can see them, and what happens when one leaks.
Why Key Storage Is a Separate Problem From Key Usage
Most teams get the "how do I call the API" part right and skip the "how do I keep the credential alive safely" part. Storage problems show up in predictable places:
- A key committed to a public or semi-public git repo (even in a
.env.examplefile by accident) - A key baked into a frontend JavaScript bundle, visible to anyone who opens dev tools
- A key shared over Slack or email and never rotated afterward
- A single key used across dev, staging, and production with no way to isolate a compromise
- A key with no expiry, no usage cap, and no owner once the person who created it leaves
None of these are exotic failure modes. They're the default outcome of treating a key like a password you set once and forget.
Core Storage Rules
1. Never store keys in code or version control
Add .env, *.pem, and any credential file patterns to .gitignore before the first commit, not after. Use a pre-commit hook (git-secrets, gitleaks) to catch accidental commits. If a key does end up in git history, rotating it is mandatory — removing the commit does not remove the exposure, since the key already lived in history and possibly in CI logs.
2. Keep the key server-side, always
Claude API keys must never reach the browser, a mobile app bundle, or any client the end user controls. If you need Claude functionality in a frontend, put a thin backend in front of it that holds the key and forwards requests. This is also where a hosted API layer like SubToAPI fits: your application gets a sub_live_... key that's scoped to your own product, while the underlying model access and your Anthropic credentials stay on SubToAPI's side, out of your client code entirely.
3. Use environment variables as the baseline, a secrets manager as the standard
For local development and small projects, environment variables loaded from an untracked .env file are fine as a floor, not a ceiling. For anything running in production:
- Use your cloud provider's secrets service (AWS Secrets Manager, GCP Secret Manager, Azure Key Vault) or a dedicated tool like HashiCorp Vault
- Inject secrets into the runtime environment at deploy time, not baked into container images
- Avoid printing secrets to build logs — many CI systems mask known variable names automatically, but custom scripts can still leak them via
echoor stack traces
4. Scope keys per environment and per service
One key for dev, staging, and production means one compromise affects all three. Issue separate keys per environment, and where the platform supports it, separate keys per service or team. SubToAPI's dashboard lets you generate distinct application keys per project so a leak in a staging service doesn't require rotating the key your production billing system depends on. See /docs for how key scoping works across projects.
5. Restrict who can read the key, not just who can use it
Storage and access control are different layers. A secret can be stored correctly in Vault and still be over-exposed if every engineer's service account has read access to every secret path. Apply least privilege: a service should only be able to read the specific key it needs, and humans should access keys through an audited path (a secrets manager UI with logging), not by SSHing into a box and cat-ing an env file.
6. Rotate on a schedule, not just after an incident
Set a rotation cadence — 90 days is a reasonable default for most teams — and automate it where possible. Rotation should be a non-event: generate the new key, deploy it alongside the old one, confirm traffic has moved, then revoke the old key. If rotation requires downtime, that's a sign the key is stored in too few places or too tightly coupled to a single deploy.
7. Log usage, not the key itself
Never log full API keys, even at debug level. Log a truncated or hashed identifier (last four characters, or a key ID if the platform issues one) so you can correlate requests to a specific credential without storing the secret in your log aggregator. SubToAPI's dashboard separates this concern for you — usage and request metadata are visible per key without ever displaying the full secret after creation, and you can review streaming and tool-call activity (see /docs/streaming and /docs/tools) without touching the raw credential.
A Practical Storage Checklist
- [ ] Key is excluded from version control via
.gitignoreand a pre-commit secret scanner - [ ] Key is injected at runtime, not embedded in build artifacts or container images
- [ ] Key never appears in frontend code, mobile bundles, or browser network requests
- [ ] Separate keys exist per environment (dev, staging, prod)
- [ ] Access to the secrets store is scoped per service with audit logging enabled
- [ ] A rotation schedule exists and has been tested at least once, end to end
- [ ] Logs reference a truncated or hashed key identifier, never the full secret
- [ ] There's a documented, fast revocation path for a confirmed leak
If you're setting up Claude access for the first time, /docs/quickstart walks through generating and storing your first key correctly from day one, and /docs/messages covers the request format once the key is in place. Getting storage right at the start is cheaper than retrofitting it after a leak.
Questions
Do I need a secrets manager for a small side project? Not strictly — a .env file excluded from git is an acceptable minimum for local development. But move to a secrets manager before you deploy anything with real usage or billing attached to it.
What should I do immediately if a key leaks? Revoke the key first, then investigate. Don't wait to confirm abuse — unauthorized usage can rack up cost within minutes, and most platforms let you issue a replacement key instantly from the dashboard.
Is it safe to use the same Claude API key across multiple apps? It works, but it removes your ability to isolate a compromise or measure per-app usage. Issue separate keys per application wherever the platform supports it, as SubToAPI does at /pricing for Solo, Team, and Scale plans.