API Key Rotation Best Practices for Developers
Rotating API keys regularly is one of the simplest ways to limit the damage a leaked credential can cause. If you're searching for best practices, the short answer is: rotate on a fixed schedule (not just after incidents), support overlapping keys so rotation doesn't cause downtime, automate the process end-to-end, and scope keys as narrowly as possible so a single leaked key has limited blast radius.
The rest of this guide covers how to actually implement that — the rotation schedule to use, how to avoid breaking production during a swap, how to automate rotation with scripts or CI, and how to respond when a key leaks despite your best efforts.
Why Key Rotation Matters
API keys are long-lived bearer tokens. Anyone who has one can act as your application until the key is revoked. Keys end up in places they shouldn't: committed to git history, pasted into Slack, logged in plaintext, cached in CI artifacts, or exposed in client-side bundles. Rotation doesn't prevent leaks, but it caps how long a leaked key stays useful.
Treat rotation as routine infrastructure maintenance, not a reaction to a breach. Teams that only rotate keys after an incident are, by definition, always one leak behind.
Set a Rotation Schedule
A reasonable default for most teams:
- Production service keys: every 90 days
- CI/CD and build-system keys: every 30–90 days
- Personal developer/test keys: every 30 days, or on offboarding
- Third-party integration keys: whatever the shortest cadence your vendor supports
If a key has broad permissions or touches billing, shorten the interval. If it's scoped to a single read-only endpoint, you can stretch it. The exact number matters less than having a number your team actually follows — put it in a runbook, not just a Slack message from six months ago.
Design for Overlapping Keys
The single biggest reason teams avoid rotation is fear of downtime. The fix is to never do a hard cutover. Instead:
- Generate a new key while the old one is still active.
- Deploy the new key to all consumers (servers, workers, CI secrets).
- Verify traffic is flowing on the new key (check logs or usage dashboards).
- Revoke the old key only after you've confirmed zero traffic on it for a safe window (e.g., 24–48 hours).
This requires your provider to support multiple simultaneously valid keys per account or project — most API platforms, including SubToAPI, let you issue several sub_live_... keys at once so you can roll one out while the other winds down. Check your dashboard or docs for how many concurrent keys your plan allows before you build a rotation script around an assumption.
# Step 1: create the new key via API or dashboard (example, not SubToAPI-specific)
NEW_KEY=$(curl -s -X POST https://your-provider.example/v1/keys \
-H "Authorization: Bearer $ADMIN_TOKEN" | jq -r .key)
# Step 2: push to your secret manager
vault kv put secret/app/api-key value="$NEW_KEY"
# Step 3: restart or redeploy consumers to pick up the new secret
kubectl rollout restart deployment/api-consumer
# Step 4: after verifying traffic, revoke the old key
curl -X DELETE https://your-provider.example/v1/keys/$OLD_KEY_ID \
-H "Authorization: Bearer $ADMIN_TOKEN"
Automate, Don't Rely on Memory
Manual rotation gets skipped. Automate it with one of these patterns:
- Scheduled job: a cron job or GitHub Action that generates a new key, updates your secret manager, and sends a notification with a revoke-by date for the old key.
- Secret manager integration: tools like Vault, AWS Secrets Manager, or Doppler can version secrets and trigger redeploys when a new version is written.
- CI pipeline step: rotate CI-only keys as part of a scheduled pipeline run, separate from your production rotation.
Keep a simple log (even a spreadsheet or a dated entry in your runbook) of when each key was created, where it's used, and when it's due to be revoked. Without that, you'll end up with "zombie" keys nobody remembers creating.
Scope Keys Narrowly
Rotation reduces the time a key is dangerous. Scoping reduces the damage it can do. Where your provider supports it:
- Issue separate keys per environment (dev, staging, production) instead of reusing one everywhere.
- Issue separate keys per service or team, so a leak from one app doesn't expose every integration.
- Use read-only or limited-scope keys for anything that doesn't need write access.
- Avoid embedding keys in client-side code — proxy requests through your own backend instead.
If you're building on top of a model API, this also means keeping your application's API keys distinct from the underlying provider credentials. For example, with SubToAPI you issue application-level sub_live_... keys per app or team member rather than sharing one Claude-backed credential across every service — see the quickstart for how keys map to your dashboard. That separation makes rotation and revocation much less risky, because killing one key only affects one consumer.
Monitor Usage After Rotation
A rotation isn't done when you issue the new key — it's done when you've confirmed the old key is unused and safely revoked. Check:
- Request logs or usage dashboards for activity on the old key ID.
- Error rates after deployment (a spike usually means some consumer still has the old key hardcoded).
- Alerts for any request authenticated with a key past its scheduled revocation date.
If your platform exposes usage metadata per key (SubToAPI does this for sub_live_... keys in the dashboard), use it specifically for this — it's the fastest way to confirm a rotation is safe to finish.
Respond Fast When a Key Leaks
Scheduled rotation handles the routine case. For a confirmed leak:
- Revoke the compromised key immediately — don't wait for the overlap window.
- Issue a new key and redeploy as fast as possible, accepting brief downtime over continued exposure.
- Audit logs for unauthorized usage during the exposure window.
- Trace how the leak happened (git history, log file, client bundle) and fix the root cause, not just the symptom.
Questions
How often should I rotate API keys? 90 days is a reasonable default for production keys, 30–90 days for CI keys, and immediately for any key suspected of being leaked. Adjust based on the key's permissions and how exposed it is.
Will rotating a key break my production app? Not if you overlap old and new keys during the transition. Issue the new key, deploy it everywhere, confirm traffic has moved over, then revoke the old one — never revoke before verifying the new key is live.
What's the difference between rotation and scoping? Rotation limits how long a key stays valid; scoping limits what a key can do. Use both — narrowly scoped keys on a regular rotation schedule minimize both the duration and severity of any leak.