Services

Resources

Company

/

API key

API key

An API key is a shared secret. The provider generates a long random string, shows it to you once, and from then on every request that carries it in a header such as Authorization: Bearer ... or x-api-key is treated as you. The server looks the key up, finds the owner, and then decides what that owner may do. Those are the two steps of authentication and authorization, and a key on its own only covers the first.

How it works

A call to an OpenAI-compatible model server looks like this. The key comes from an environment variable and never sits in the code:

curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer $LLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3-8b", "messages": [{"role": "user", "content": "hi"}]}'
curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer $LLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3-8b", "messages": [{"role": "user", "content": "hi"}]}'
curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer $LLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3-8b", "messages": [{"role": "user", "content": "hi"}]}'
curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer $LLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3-8b", "messages": [{"role": "user", "content": "hi"}]}'
curl https://llm.example.com/v1/chat/completions \
  -H "Authorization: Bearer $LLM_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model": "qwen3-8b", "messages": [{"role": "user", "content": "hi"}]}'

A careful server stores only a SHA-256 hash of each key plus a short prefix, so a database dump does not leak usable keys and support staff can still tell keys apart. It attaches limits to the key record: an expiry date, the models or endpoints it may reach, and a rate limit. GPUStack works this way, with per-user keys that can expire and be restricted to selected models.

An OAuth 2.0 access token is a different kind of credential. An authorization server issues it to an app after a user signs in and consents. It carries scopes and usually expires within an hour, and a refresh token gets the next one. An API key normally has no user consent step and lives until someone revokes it. Keys suit server-to-server calls you control; when an app acts for many different users, OAuth fits better.

Shared keys are where it hurts. Say 12 services call an LLM gateway with one key, and it leaks into a CI log. It is used from an unknown address at 400 requests a minute for 6 hours before anyone notices: 400 x 60 x 6 = 144,000 requests billed to you.

The usage page shows one key, so you cannot tell which service leaked it, and revoking it takes all 12 down at once. With one key per service, you revoke the leaked key, redeploy one service with a new key, and the other 11 keep running.

Why it matters in production

Keys leak through git commits, client-side JavaScript and mobile apps, CI logs, error messages and chat. Keep them in a secret store such as AWS Secrets Manager, synced into pods with the External Secrets Operator if you run Kubernetes, and turn on secret scanning in your repositories.

Give each service its own key with the least privilege it needs and an expiry date. Rotate by issuing the new key, deploying it, watching traffic on the old key drop to zero, and only then revoking the old one. An AI gateway or API gateway in front of your models gives you one place to check keys and see usage per key.

See also

On the One2N blog

References

  1. Google Cloud: Manage API keys

  2. RFC 6750: The OAuth 2.0 Authorization Framework: Bearer Token Usage

Side by side comparison of API key setups. Left: 12 services share one key. In red, the leaked key is used at 400 requests a minute x 60 x 6 hours = 144,000 requests, and revoking it takes all 12 services down. Right: each of 12 services has its own key; one key is revoked and its service redeployed with a new key, so 11 keep running.