Skip to content
mentoor

Topic 2 of 4 · 4 min read

Rotating, scoping and revoking API keys

Goal: Plan a key rotation and leak response that replaces a key without downtime and limits what each key can reach.

Give every environment and service its own narrow key, keep it in a secret manager, and plan the swap so that replacing a key never needs downtime. Then a leak is a short, boring event instead of an outage.

Check yourself · 1 of 3

You are rotating a production key with no downtime. What is the right order?

Choose one answer

Why it matters

Keys leak. They get pasted in chat, printed in a CI log, committed by mistake or left on a laptop. Public code hosts scan for known key formats, and attackers do too, so a pushed key should be treated as exposed even if you delete the commit a minute later. What decides the damage is how much that key can do and how fast you can replace it.

How to do it

  1. Separate keys. One key per environment (development, staging, production) and per service or team. A leaked development key then cannot touch production, and usage by each service shows up on its own line.
  2. Scope them. Use provider projects, workspaces or restricted permissions where they exist. Give a key only the models and endpoints that service needs. Set a spend limit on the workspace.
  3. Store in a secret manager. Keep keys in the platform's secret store, not in the repository, image or a shared document. Load them at start-up or at call time. Add secret scanning and a pre-commit check.
  4. Rotate without downtime. Overlap two keys:
    • Create the new key.
    • Store it, and deploy so servers read the new value.
    • Watch usage move from the old key to the new one.
    • Revoke the old key only when its traffic reaches zero.
  5. Rotate on a schedule and on events. Schedule it (for example every quarter) and also when someone with access leaves or when exposure is suspected. A secret manager can automate the schedule.
  6. Have a leak runbook. Revoke first, then investigate. Check the provider's usage page for calls you did not make, replace the key, and search logs and repositories for other copies.

Before and after

Before: one key named prod is used by the web app, the batch job and a contractor's notebook. It was created two years ago. When it leaks, nobody knows which of the three caused it, and revoking it stops all three at once.

After: three keys, each in the secret manager, each limited to its project. The notebook key has a low spend cap. The leaked one is revoked in a minute and the other two keep working.

What goes wrong

  • Revoking before deploying. The old key dies while servers still use it, and every request fails.
  • Rotating but not searching. The new key is safe, but the old one is still in old images, backups and Slack.
  • One shared key for everything. Rotation becomes so risky that nobody does it.
  • Deleting the commit and calling it fixed. Clones, forks and scanners have already copied it.

Examples

Zero-downtime swap

Monday: create chat-prod-2, store it in the secret manager and redeploy the three servers. Tuesday: the provider dashboard shows chat-prod-1 traffic falling to zero as old pods drain. Wednesday: revoke chat-prod-1. At no point did a request fail, because the old key was kept until nothing used it.

Key pushed to a public repo

A developer pushes a .env file by mistake. The secret scanner emails an alert within minutes. The runbook says: revoke the key at once, create a replacement, deploy it, then read the usage page for the past day. Any calls from unknown IP addresses or unexpected models confirm misuse. The commit is then removed, but only after the key is dead.

Key points

  • Use a separate key per environment and per service, so a leak is contained and usage can be traced.
  • Scope each key and set a workspace spend limit so a stolen key can do limited damage.
  • Rotate by overlap: create the new key, deploy it, watch traffic move, and revoke the old key last.
  • Treat any key that reached a repository, a log or a chat as exposed, even if you deleted it quickly.
  • In a leak, revoke first and investigate second, then check provider usage for calls you did not make.

Common mistakes

  • Revoking the old key before every server has the new one.
  • Using a single shared key for production, staging and a contractor's experiments.
  • Cleaning the git history and skipping the revocation.
  • Storing keys in a wiki page or chat so the secret manager copy is never the real one.

Check yourself · 2 of 3

A developer pushed a production key to a public repository and deleted the commit five minutes later. What should the team do?

Choose one answer

Check yourself · 3 of 3

Why give the batch job and the web app separate provider keys?

Choose one answer

Practice items, not counted towards your mastery.

Start here

More for this topic