← Blog

How to Secure Claude API Keys in Production

2026-11-02 · 6 min read · SubToAPI Team

If you're asking how to secure Claude API keys in production, the short answer is: never embed them in client code, store them in a secrets manager (not .env files committed to git), rotate them regularly, scope them per application or environment, and put rate limiting and logging between your users and the raw key. This article walks through each of those practices concretely, including where most teams get it wrong.

The core problem is that a raw Anthropic API key is a bearer credential with full account access — whoever holds it can spend your budget, see your usage data, and call any model. Treating it like a password you paste into a frontend bundle or a Slack message is how keys end up leaked on GitHub, in browser dev tools, or in a compiled mobile app. Production security for Claude keys is really about reducing the blast radius when something goes wrong, not just preventing leaks outright.

1. Never ship the key to the client

If your Claude key is in any code that runs in a browser, mobile app, or Electron app, it is effectively public. Minified JavaScript can be read by anyone with dev tools open. The fix is always the same: put a server between the client and Anthropic's API.

// client — never does this
fetch("https://api.anthropic.com/v1/messages", {
  headers: { "x-api-key": "sk-ant-..." } // leaked instantly
});

// client — calls your own backend instead
fetch("/api/chat", { method: "POST", body: JSON.stringify({ prompt }) });

Your backend holds the real key, applies auth/rate limits, and forwards the request. This is non-negotiable for any production app with a public-facing UI.

2. Keep keys out of source control and config files

.env files are fine for local development but should never be committed. Use .gitignore, and in CI/CD use your platform's secrets store (GitHub Actions secrets, Vercel environment variables, AWS Secrets Manager, HashiCorp Vault) rather than baking keys into Docker images or config files that get checked in.

A quick audit checklist:

3. Scope keys per environment and per service

One of the most common production mistakes is using a single Claude key across dev, staging, and production, or across every microservice. If that key leaks or gets rate-limited, everything breaks at once, and you can't tell which system caused the spike in usage.

Instead, issue separate keys per environment and ideally per service, so you can:

This is exactly the kind of scoping SubToAPI is built for — it turns your existing Claude access into application-level API keys (sub_live_...) per app or team, instead of one shared key everyone copies into every repo. You get usage metadata per key, so if something looks off you know exactly which application caused it. See the quickstart for how key issuance works.

4. Rotate keys on a schedule, not just after an incident

Rotation shouldn't be a reaction to a breach — it should be routine. A practical rotation policy:

  1. Generate a new key alongside the old one
  2. Deploy the new key to all services that need it
  3. Verify traffic has shifted (check logs/metrics)
  4. Revoke the old key after a short overlap window (e.g. 24–48 hours)

Build this into your runbooks, not just your memory. If your provider doesn't support overlapping keys, schedule rotation during low-traffic windows and have a rollback plan ready.

5. Rate limit and monitor at the application layer

Even with a perfectly secured key, a bug in your own code — an infinite retry loop, a scraper hitting your endpoint, a prompt injection that triggers repeated tool calls — can blow through your budget in minutes. Don't rely solely on the provider's account-level limits; add your own:

// simple per-user rate limit example
const limiter = new Map();

function isAllowed(userId, maxPerMinute = 20) {
  const now = Date.now();
  const windowStart = now - 60_000;
  const timestamps = (limiter.get(userId) || []).filter(t => t > windowStart);
  if (timestamps.length >= maxPerMinute) return false;
  timestamps.push(now);
  limiter.set(userId, timestamps);
  return true;
}

Pair this with logging of request counts, token usage, and error rates per key or per user. If you're using SubToAPI, usage metadata is already tracked per application key in the dashboard, so you don't have to build this instrumentation yourself — you can see spend and request volume per key without wiring up a custom logging pipeline. Streaming requests are logged the same way as standard calls; see streaming for details on how responses come back.

6. Control team access, not just the key itself

In a team setting, the bigger risk often isn't a leaked key — it's too many people having access to the one key that exists. Treat Claude access like any other production credential:

SubToAPI's team plans (Team at €19/seat, Scale at €49/seat) are built around this — each seat gets its own scoped access without everyone sharing one root credential, and admins can manage who holds which keys from a single dashboard. Check pricing for how seats map to usage.

7. Set spend alerts and hard caps where possible

Secrets management prevents leaks; spend controls limit the damage if one happens anyway. Set up budget alerts wherever your billing lives, and if your provider or gateway supports hard spending caps per key, use them — especially for keys used in automated or background jobs where no human is watching in real time.

Quick security checklist

FAQ

Is it safe to store a Claude API key in an environment variable?

Yes, for the server process that calls the API — environment variables are the standard way to inject secrets at runtime. The risk isn't the variable itself, it's exposing it through client bundles, logs, or crash reports. Keep it server-side and avoid printing it in debug output.

What should I do if a Claude API key leaks?

Revoke it immediately from your provider's dashboard, issue a new one, and redeploy. Check usage logs for the period the key was exposed to see if it was used maliciously, and audit your git history and CI logs to find how it leaked so you can close that specific gap.

Can I give different team members different levels of access to the same Claude account?

Not directly through a single raw API key — it's all-or-nothing access. Tools like SubToAPI solve this by issuing separate application keys per team member or project under one Claude account, with individual usage tracking, so you get scoped access without sharing one credential. See /docs for setup details.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →