Claude API Environment Variable Management Guide
If you're building anything beyond a weekend script with the Claude API, you'll quickly hit the same problem every team does: where do the API keys and config values live, how do they differ between environments, and how do you stop someone from committing a live key to a public repo. This guide covers the practical patterns for managing Claude API environment variables across local development, CI/CD, staging, and production.
The short answer: never hardcode keys, use .env files locally with a loader library, inject secrets at runtime through your platform's secret manager in production, and separate keys by environment so a leaked staging key can't touch production traffic. The rest of this guide walks through how to actually implement that.
Why environment variables matter for the Claude API
Claude API keys are long-lived credentials that grant direct billing access to Anthropic's models (or, if you're using a gateway like SubToAPI, to your application API key). If one leaks in a public GitHub repo, a Slack message, or a client-side bundle, someone else can run up charges on your account within minutes. Environment variables exist specifically to keep that credential out of your source code, your version control history, and your compiled frontend bundle.
The core rule is simple: secrets live in the environment, not in the codebase. Code reads process.env.ANTHROPIC_API_KEY (or process.env.SUBTOAPI_KEY) at runtime; it never contains the literal string.
Naming conventions that scale
Pick a consistent naming scheme before you have ten services doing it ten different ways. A pattern that works well:
ANTHROPIC_API_KEY=sk-ant-...
ANTHROPIC_MODEL=claude-sonnet-4-20250514
ANTHROPIC_MAX_TOKENS=4096
CLAUDE_ENV=production
If you're routing through SubToAPI instead of calling Anthropic directly, the same pattern applies:
SUBTOAPI_KEY=sub_live_xxxxxxxxxxxxxxxxxxxx
SUBTOAPI_BASE_URL=https://api.subtoapi.app/v1
Prefixing variables by vendor (ANTHROPIC_, SUBTOAPI_) rather than generic names like API_KEY prevents collisions when a project talks to multiple AI providers. It also makes grepping your codebase for secret usage trivial.
Local development: .env files and loaders
For local dev, a .env file loaded by a library like dotenv (Node) or python-dotenv (Python) is the standard approach:
# .env
SUBTOAPI_KEY=sub_live_xxxxxxxxxxxxxxxxxxxx
SUBTOAPI_BASE_URL=https://api.subtoapi.app/v1
import 'dotenv/config';
const response = await fetch(`${process.env.SUBTOAPI_BASE_URL}/messages`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.SUBTOAPI_KEY}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [{ role: 'user', content: 'Hello' }],
}),
});
Three things to get right immediately:
- Add
.envto.gitignorebefore you create the file, not after. A key committed once in history stays recoverable even if you delete it later. - Commit a
.env.examplewith variable names and placeholder values so teammates know what to configure without ever seeing a real key. - Rotate immediately if a
.envfile is ever accidentally committed — treat it as compromised, don't just delete the commit.
Separating keys by environment
A common mistake is using one Claude API key across dev, staging, and production. If that key leaks from a staging server, production traffic is exposed too, and you can't tell which environment generated a given usage spike when debugging billing.
Instead, issue separate keys per environment:
# Development
SUBTOAPI_KEY_DEV=sub_live_dev_xxxx
# Staging
SUBTOAPI_KEY_STAGING=sub_live_staging_xxxx
# Production
SUBTOAPI_KEY_PROD=sub_live_prod_xxxx
Each environment's runtime config only ever contains its own key — the application code reads a single SUBTOAPI_KEY variable, but the value injected differs by deployment target. This also lets you set different rate limits or budgets per key if your provider supports it, and makes revoking a compromised staging key a non-event for production.
Injecting secrets in CI/CD and production
Local .env files are fine for your laptop, but CI/CD pipelines and production servers should pull secrets from a dedicated secret store rather than a checked-in file:
- GitHub Actions: repository or environment secrets, referenced as
${{ secrets.SUBTOAPI_KEY }}. - Vercel / Netlify: project environment variables, scoped per deployment context (production, preview, development).
- AWS: Secrets Manager or Parameter Store, fetched at container startup.
- Docker / Kubernetes: secrets mounted as environment variables or files, never baked into the image layer.
A quick smell test: if you can docker history your image and see a key, or if git log -p ever surfaces one, your pipeline has a leak. Secrets should be injected at runtime, not build time, whenever possible — this avoids baking a key into an image layer that gets pushed to a shared registry.
Validating required variables at startup
A silent undefined API key that only fails on the first request is a bad developer experience. Fail fast instead:
const required = ['SUBTOAPI_KEY', 'SUBTOAPI_BASE_URL'];
for (const key of required) {
if (!process.env[key]) {
throw new Error(`Missing required environment variable: ${key}`);
}
}
This five-line check saves hours of confused debugging when someone forgets to set a variable before deploying.
Rotating keys without downtime
Because Claude API keys (and SubToAPI application keys) can be revoked and reissued independently of your code, rotation should be a config change, not a deploy. The pattern:
- Generate a new key from your dashboard (SubToAPI's
/pricingplans each include dashboard-managed keys and seats). - Update the secret in your CI/CD or hosting platform's secret store.
- Redeploy or restart the service to pick up the new value.
- Revoke the old key once you've confirmed traffic is flowing on the new one.
Keeping this process scripted — a rotate-key.sh that updates the secret store and triggers a redeploy — means rotation takes minutes, not an afternoon, which makes teams far more willing to rotate regularly instead of only after an incident.
If you're managing this across a team, a dashboard that issues per-member keys under one billing account removes a lot of the manual secret-passing that causes leaks in the first place — see the quickstart for how key issuance and the messages endpoint fit together.
questions
Do I need separate environment variables for streaming requests? No. Streaming uses the same API key and base URL as standard requests — only the request parameters change. See streaming docs for the parameter differences.
Should I store the Claude model name as an environment variable? Yes, treating the model identifier as config (e.g. ANTHROPIC_MODEL=claude-sonnet-4-20250514) lets you switch model versions without a code change or redeploy.
What's the fastest way to get started with a working environment setup? Sign up at /signup, copy your sub_live_ key into a local .env file following the quickstart guide, and you have a working setup in under five minutes.