← Blog

How to Generate a Claude API Key Securely

2026-09-25 · 6 min read · SubToAPI Team

Generating a Claude API key is straightforward — the security part is where most teams get it wrong. This guide covers the actual key generation steps, then the practices that keep that key from ending up in a git history, a client bundle, or a leaked log file.

If you just need the two-minute answer: create the key from your Anthropic Console, store it in an environment variable or secrets manager (never in code), scope access with a proxy or gateway if multiple people or apps need to use it, and rotate it on a schedule. Everything below explains why each step matters and how to implement it.

Step 1: Generate the key in the right place

Anthropic issues keys through the Console under API Keys. Each key is tied to a workspace and organization, so before generating one, decide:

Generate the key, copy it once (Anthropic won't show it again), and immediately put it somewhere durable — a password manager or secrets vault — before doing anything else with it. Don't paste it into a chat, a ticket, or a shared doc "just for now." That's how keys leak.

Step 2: Never put the key in client-side code

This is the single most common mistake. If your Claude API key ends up in a browser bundle, a mobile app binary, or a public repo, it will be scraped and abused within hours — API keys get harvested by bots that scan GitHub and public JS bundles continuously.

The fix is architectural: the raw key should only ever exist on a server you control. Your frontend calls your backend, and your backend calls the Claude API (or a gateway in front of it). If you're building a product that issues API access to your own users, you need a second layer of keys — application-level keys that your users hold, which map internally to your real credentials. This is exactly the gap SubToAPI fills: it sits between your Anthropic-based access and your application, issuing scoped sub_live_... keys per app or per customer so the underlying credential is never exposed to end users or client code.

Step 3: Store the key correctly on the server

Once the key lives server-side, how you store it still matters:

# .env (never committed)
CLAUDE_API_KEY=sk-ant-xxxxxxxxxxxxxxxxxxxx
// server.js
const apiKey = process.env.CLAUDE_API_KEY;

if (!apiKey) {
  throw new Error("CLAUDE_API_KEY is not set");
}

Rules that prevent most incidents:

If you ever do commit a key by accident, treat it as compromised immediately — revoke it in the Console and generate a new one. Removing it from a later commit does not remove it from git history.

Step 4: Limit blast radius with scoped access

A single API key that every developer, script, and environment shares is a single point of failure. If it leaks, everything using it is exposed, and you often can't tell which system caused the leak.

Better patterns:

This last case is where raw provider keys stop scaling. If you need to give ten internal teams or external customers their own credentials with independent usage tracking, you don't want to hand out copies of one root key. SubToAPI generates a sub_live_... key per application, so you can revoke one team's access without touching anyone else's, and see usage broken out per key in the dashboard. See /docs/quickstart for how key issuance works.

Step 5: Rotate keys on a schedule, not just after an incident

Rotation shouldn't be a reactive process. A reasonable baseline:

To rotate without downtime: generate the new key, deploy it to your services, confirm requests are succeeding with the new key, then revoke the old one. Keeping both active for a short overlap window avoids a hard cutover failure.

Step 6: Monitor usage for anomalies

A leaked key often shows up first as a spike in usage or requests from unexpected volumes before anyone notices manually. Whatever you use to call the API, make sure you can see:

If you're calling Claude directly, this means building your own logging around every request. If you're routing through a gateway like SubToAPI, usage metadata — tokens, cost, latency — is captured automatically per key in the dashboard, which makes an unusual spike easy to spot without instrumenting it yourself. Streaming responses are covered the same way; see /docs/streaming for details on how metadata is reported for streamed requests.

Quick checklist

If you're issuing access to multiple apps or team members, /signup gets you a SubToAPI account with scoped keys and per-key usage tracking out of the box — see /pricing for plan details and /docs for full API reference.

FAQ

Is it safe to store a Claude API key in a .env file? Yes for local development, as long as .env is in .gitignore and never committed. In production, use a secrets manager rather than a plain .env file on a server's disk.

What should I do if I accidentally committed my API key to GitHub? Revoke the key immediately in the Console and generate a replacement. Rewriting git history to remove it is good practice but doesn't undo the exposure — assume it's compromised the moment it's pushed.

Can I give different team members or apps their own API keys instead of sharing one? Yes, and you should. Separate keys per service or team let you revoke and monitor access independently. Tools like SubToAPI generate per-application keys automatically if you need this at scale rather than managing it manually.

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 →