The idea
Keys leak through logs, screenshots, laptops and ex-employees. Rotation limits how long a leaked key is useful. Rotate on a schedule (for example every 90 days) and immediately when a leak is suspected.
Make rotation boring: support two valid keys at once (old and new), deploy the new one, confirm traffic uses it, then revoke the old one. Automate it with your secret manager where possible.
Use separate keys per environment and per service, so you can revoke one without taking everything down, and so usage dashboards show who used what.
If rotation is painful, nobody does it, and a leak stays exploitable for months.
Words to know
- Zero-downtime rotationOverlap old and new keys so nothing breaks during the switch.
Try it without code
Practise a rotation
- Create a second API key in your provider console.
- Swap it into your app’s environment and redeploy.
- Revoke the old key and confirm nothing broke.
Use it in code
Rotation runbook with GCP Secret Manager (similar in AWS).
# 1. Create the new key in the provider console, then add it as a new secret version
echo -n "$NEW_KEY" | gcloud secrets versions add anthropic-api-key --data-file=-
# 2. Redeploy so services read the latest version
gcloud run services update rag-api --update-secrets=ANTHROPIC_API_KEY=anthropic-api-key:latest
# 3. Watch traffic on the new key in the provider dashboard, then revoke the old key there
# 4. Disable the old secret version
gcloud secrets versions disable 3 --secret=anthropic-api-keyWrite a ROTATION.md runbook for your project and rehearse it once end to end.
Common mistakes
- One shared key for dev, staging and prod.
- Revoking the old key before the new one is live.