← Blog

API Key Rotation Best Practices for Developers

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

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:

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:

  1. Generate a new key while the old one is still active.
  2. Deploy the new key to all consumers (servers, workers, CI secrets).
  3. Verify traffic is flowing on the new key (check logs or usage dashboards).
  4. 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:

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:

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:

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:

  1. Revoke the compromised key immediately — don't wait for the overlap window.
  2. Issue a new key and redeploy as fast as possible, accepting brief downtime over continued exposure.
  3. Audit logs for unauthorized usage during the exposure window.
  4. 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.

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 →