← Blog

Claude API Secure Key Storage: Backend Best Practices

2026-10-14 · 5 min read · SubToAPI Team

Storing a Claude API key securely means one thing above all else: the key never touches the client. It lives only in backend environment variables, a secrets manager, or a key management service, and every request from your frontend or mobile app goes through a server you control, which attaches the key before forwarding the request to Anthropic.

This matters because a leaked key is not a theoretical risk. Keys embedded in frontend JavaScript, mobile app binaries, or public GitHub repos get scraped within minutes by automated bots that scan for credential patterns. Once found, a key gets used for abuse until someone notices the bill or Anthropic flags it. The fix is architectural, not just "be careful" — you need a storage and access pattern that makes exposure structurally impossible, not just unlikely.

Why client-side storage is never acceptable

Any key placed in client-side code — a React bundle, a mobile app's config file, a browser extension — is public. It doesn't matter if it's "obfuscated" or loaded from a remote config at runtime; if the client can use it, the client can extract it. This applies to:

The only safe pattern is: client calls your backend, your backend calls Claude.

Backend storage patterns that actually work

1. Environment variables with a secrets manager

For most teams, the baseline is storing the key as an environment variable injected at deploy time, sourced from a secrets manager rather than a plaintext .env file committed to version control.

# .env — never commit this file
ANTHROPIC_API_KEY=sk-ant-xxxxxxxxxxxxxxxx

Use a dedicated secrets store (AWS Secrets Manager, Google Secret Manager, HashiCorp Vault, or your hosting provider's built-in secrets feature) instead of a raw .env in production. These services give you rotation, access logging, and the ability to revoke a key without redeploying code.

// server.js — key read once at startup, never logged, never returned to client
const apiKey = process.env.ANTHROPIC_API_KEY;

if (!apiKey) {
  throw new Error("Missing ANTHROPIC_API_KEY");
}

2. Keep the key out of logs and error messages

A surprisingly common leak path is accidental logging. Request interceptors, error handlers, and debug middleware can print full request headers, including Authorization. Strip sensitive headers before logging:

function sanitizeHeaders(headers) {
  const clone = { ...headers };
  delete clone.authorization;
  delete clone["x-api-key"];
  return clone;
}

3. Scope keys per application, not per team

A single shared key used across every app and environment means one compromised service exposes everything. Where possible, issue separate keys per application and per environment (staging vs production), so a leak in one surface can be revoked without taking down everything else.

This is one of the gaps SubToAPI fills. Instead of handing out your raw Anthropic credentials to every internal service, you generate scoped application keys (sub_live_...) from one dashboard, each tied to your underlying Claude access. If a key used by a staging environment or a third-party integration gets compromised, you revoke that one key instead of rotating credentials across your whole stack. See /docs/quickstart for how key issuance works.

4. Rotate keys on a schedule, not just after incidents

Set a calendar reminder to rotate keys quarterly, and rotate immediately whenever:

5. Proxy pattern for frontend apps

If you're building a single-page app or mobile client that calls Claude, route every request through your backend rather than letting the client call Anthropic's API directly:

// Backend route — client never sees the key
app.post("/api/chat", async (req, res) => {
  const response = await fetch("https://api.anthropic.com/v1/messages", {
    method: "POST",
    headers: {
      "x-api-key": process.env.ANTHROPIC_API_KEY,
      "anthropic-version": "2023-06-01",
      "content-type": "application/json",
    },
    body: JSON.stringify(req.body),
  });
  const data = await response.json();
  res.json(data);
});

This pattern also gives you a place to add rate limiting, user authentication, and request validation before anything reaches Claude — all things you'd otherwise have no control over if the client called the API directly.

Using SubToAPI as the storage and access layer

If you don't want to run your own proxy and secrets infrastructure, SubToAPI gives you that backend layer as a hosted service. You generate per-app keys in the dashboard, each scoped to your plan (Solo €9, Team €19/seat, Scale €49/seat), and your code talks to https://api.subtoapi.app/v1/messages with Authorization: Bearer $SUBTOAPI_KEY instead of storing raw Anthropic credentials across multiple services:

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": 1024,
    "messages": [{"role": "user", "content": "Summarize this report"}]
  }'

You still store $SUBTOAPI_KEY the same way you'd store any backend secret — environment variable, secrets manager, never in client code — but you get usage metadata, streaming, and tool use per key without managing the underlying Claude credential yourself. See /docs/messages and /docs/streaming for details, and /pricing for plan specifics. A free trial is available at /signup.

A quick checklist

Questions

Can I store a Claude API key in a frontend .env file if I just don't commit it? No. Frontend build tools inline environment variables into the shipped JavaScript bundle at build time, so anyone can extract it from the browser regardless of whether it was committed to git.

What's the difference between a secrets manager and just using environment variables? Environment variables alone have no access logging, rotation, or revocation tooling — a secrets manager (Vault, AWS/GCP Secrets Manager) adds those controls and lets you update a key without redeploying your app.

Does SubToAPI replace my own backend, or do I still need one? SubToAPI is itself the secure backend layer for Claude requests — you still store your SubToAPI key server-side like any other secret, but you no longer need to manage raw Anthropic credentials or build your own proxy.

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 →