How to Secure Claude API Keys in Production
Securing Claude API keys in production means treating them like passwords: never ship them to the client, never commit them to version control, rotate them on a schedule, and scope each key so a leak has limited blast radius. The short answer is to store keys in a secrets manager or environment variables on the server side only, issue separate keys per environment and per application, monitor usage for anomalies, and have a fast revocation path when something goes wrong.
The longer answer involves a few layers: where keys live, how they're transmitted, who can use them, and what happens when one is compromised. Below is a practical checklist you can apply today, whether you're calling the Claude API directly or through a proxy layer like SubToAPI.
Never Put Keys in Client-Side Code
This is the single most common mistake. If your API key is in a frontend bundle, a browser extension, or a mobile app binary, it's public — no matter how it's obfuscated. Anyone can open DevTools, inspect network traffic, or decompile the app.
Rule: all calls to Claude's API must originate from your backend. The browser talks to your server, and your server talks to Claude.
// WRONG — key exposed in the browser bundle
fetch("https://api.anthropic.com/v1/messages", {
headers: { "x-api-key": "sk-ant-..." } // visible to anyone
});
// RIGHT — browser calls your own backend endpoint
fetch("/api/chat", {
method: "POST",
body: JSON.stringify({ prompt: userInput })
});
// your backend holds the key and forwards the request
If you're exposing an API to third-party developers or internal teams, this is exactly the problem application-level keys solve — see the scoping section below.
Store Keys in Environment Variables or a Secrets Manager
Don't hardcode keys in source files, config files checked into git, or Dockerfiles. Use:
- Environment variables injected at runtime (
.envfiles excluded via.gitignore) - A dedicated secrets manager (AWS Secrets Manager, GCP Secret Manager, HashiCorp Vault, Doppler)
- Platform-native secret stores (Vercel, Railway, Fly.io environment settings)
# .env (never committed)
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxx
# .gitignore
.env
.env.local
Add a pre-commit hook or CI secret scanner (git-secrets, truffleHog, GitHub's push protection) so an accidental git add . doesn't leak a key into history. Keys committed to a public repo are usually found and abused within minutes.
Use Separate Keys Per Environment and Per App
One key for dev, staging, and production is a liability. If your staging environment is compromised, your production usage and billing shouldn't be at risk.
- Issue distinct keys for
dev,staging,production - Issue distinct keys per application or service, not one shared key for everything
- Name keys clearly so you know what to revoke when something looks wrong
This is where a layer that supports application-level API keys becomes useful. Instead of distributing your single underlying Claude credential across ten internal tools, you generate scoped keys (e.g. sub_live_...) per app or per team, each independently revocable. SubToAPI issues these kinds of keys per project from one dashboard, so a compromised key for one app doesn't touch the others — see the quickstart for how key issuance works.
Rotate Keys on a Schedule
Rotation limits how long a leaked key stays useful even if you don't notice the leak immediately.
- Rotate every 60–90 days as a baseline
- Rotate immediately after any suspected exposure (laptop theft, log leak, ex-employee offboarding)
- Automate rotation where possible — generate the new key, update the secret store, deploy, then revoke the old key after confirming traffic has shifted
A zero-downtime rotation pattern: issue the new key, deploy it to all consumers, verify traffic on the new key in your dashboard, then revoke the old one.
Scope Permissions and Limit Blast Radius
If your provider supports scoped or role-limited keys, use the minimum scope necessary. A read-only analytics key should not also be able to trigger production workflows. If you're building a multi-tenant product on top of Claude, don't hand every tenant the same raw provider key — issue per-tenant keys so you can rate-limit, bill, and revoke individually.
Monitor Usage and Set Alerts
A leaked key often shows up first as a usage spike. You want visibility into:
- Request volume per key, per day
- Token usage and cost per key
- Error rates (a sudden wave of 401s/403s can indicate someone is probing with a stolen or malformed key)
Set alert thresholds so you're notified — not surprised by an invoice — when a key's usage jumps well above baseline. A usage dashboard that breaks down calls by key and by app makes this diagnosis fast instead of forensic.
Put a Proxy Layer in Front of the Raw Key
Instead of distributing your core Anthropic credential to every service, team, and script that needs Claude access, put one controlled layer in front of it. This gives you:
- One place to rotate the underlying credential without touching every consumer
- Per-app, per-team keys issued and revoked independently
- Centralized logging of who called what, when, and how much it cost
This is the core idea behind SubToAPI: it turns your existing Claude access into a proper HTTPS API with sub_live_... keys, streaming, tool use, and usage metadata, so your teams integrate against a key you fully control rather than passing around the raw provider credential. Check pricing if you're evaluating this for a team, or the messages and streaming docs for integration details.
curl https://api.subtoapi.app/v1/messages \
-H "Authorization: Bearer $SUBTOAPI_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 512,
"messages": [{"role": "user", "content": "Summarize this ticket"}]
}'
Build a Revocation Runbook
Have a documented process before you need it:
- Identify which key is compromised
- Revoke it immediately from the dashboard
- Issue a replacement and redeploy
- Audit recent usage logs for that key to assess impact
- Rotate any other keys that shared storage or deployment with it
FAQ
Is it safe to use the same Claude API key across multiple apps?
No. Use separate keys per app and per environment so a compromise or bug in one app doesn't affect billing or access for the others, and so you can revoke one without breaking the rest.
What should I do immediately if a Claude API key leaks?
Revoke the key right away, issue a new one, redeploy with the new key, and review usage logs from the exposure window to check for unexpected activity or charges.
How often should I rotate production API keys?
A reasonable baseline is every 60–90 days, plus an immediate rotation whenever you suspect exposure, such as a leaked log file, a lost device, or staff offboarding.