Usage & Billing

API Key Rotation Best Practices: Overlap Windows, Scoping and What to Do on a Leak (2026)

Grace Whitmore

Most teams adopt key rotation as a calendar ritual: someone remembers every ninety days, generates a new key, pastes it into the deployment config, and hopes nothing was still using the old one. That version of rotation produces outages without meaningfully reducing risk. The useful version is narrower and more boring — a rehearsed procedure that shrinks the blast radius of a leak and can be run on demand rather than on a schedule.

What API key rotation is actually for

A key is a bearer credential: whoever holds it is you. Rotation does not prevent a leak. It bounds the window during which a leaked key remains useful, and — more importantly in practice — it gives you a tested way to invalidate a credential right now, on the day you need it.

That framing changes the priorities. A team that rotates religiously every quarter but takes six hours of panicked debugging to revoke a compromised key is in worse shape than a team that rotates rarely but can cut a key over in four minutes with confidence.

The three situations that actually demand rotation are: a credential that may have been exposed, a person or vendor losing their need for access, and a key whose scope was always too broad. Everything else is hygiene.

API key rotation best practices, in order of value

Scope keys so that rotation is cheap. One key per service, per environment, per integration. A single shared key across staging, production and three internal tools guarantees that rotating it is a cross-team event, which guarantees it will be postponed. Narrow keys can be rotated by one person without a meeting.

Rotate with overlap, never with a swap. The pattern that avoids downtime is: issue a new key while the old one still works, deploy the new key, verify real traffic is using it, then revoke the old one. The overlap window is where safety lives. A hard swap — revoke and replace in one step — is how rotation earns its reputation for causing incidents.

Verify before revoking, using traffic rather than intent. "I updated all the configs" is not evidence. Where the provider exposes per-key usage or last-used timestamps, watch the old key go quiet before you revoke it. If it will not go quiet, something you forgot is still holding it, and that is exactly what you wanted to discover before the revoke rather than after.

Set an expiry at creation time. A key with an expiry date rotates itself, and it converts a silent indefinite risk into a scheduled, visible one. Quotas serve the parallel purpose of capping what a leaked key can cost you before anyone notices — the mechanics of both are covered in setting quotas and expiry on API keys.

Keep keys out of the places they leak from. Not in source control, not in client-side code, not in a shared document, not in chat history. Environment variables injected at deploy time are an acceptable floor; a secret manager with access logging is better, mostly because it answers "who read this and when" during an incident.

Write down the rotation procedure before you need it. Which key, which systems consume it, who can issue a replacement, how to verify, how to roll back. An incident is a bad time to discover that only one person knows where the key is set.

Choosing a cadence

Fixed calendars are popular because they are auditable, not because ninety days is a meaningful number. A defensible approach is risk-based:

  • Immediately, on any suspicion of exposure — a key in a commit, a screenshot, a support ticket, a departed contractor's laptop.
  • On access change, when someone leaves the team or a vendor relationship ends. This is the rotation most often skipped and most often regretted.
  • On a slow schedule for everything else — quarterly or semi-annually is fine, provided the on-demand path is rehearsed. If your only rotation muscle is the annual one, the emergency rotation will not go well.

Rotating a well-scoped key more often than you can verify it is not a security improvement. It is a source of outages that trains people to dread the procedure.

When a key leaks

Revoke first, investigate second. The instinct to understand the scope before acting is backwards for bearer credentials — every minute of analysis is a minute the key still works.

Then: check usage on the leaked key for calls you did not make, rotate any key that shared its storage location, and look for the systemic cause. A key in a public commit is rarely a one-off; it usually means keys live somewhere the repository can see them.

Automating rotation without new failure modes

Automation is worth it once you have more keys than you can track by hand, but it introduces its own risk: the automation itself holds credentials with the power to issue and revoke. Keep it narrow, log every action it takes, and make sure a human can perform the same rotation manually — an automated system that is the only path to rotation becomes a single point of failure during exactly the incident where you need it most.

Frequently asked questions

How often should API keys be rotated?

There is no universally correct interval, and the interval matters less than the trigger. Rotate immediately on suspected exposure and on access changes; for routine hygiene, a quarterly or semi-annual cadence is defensible provided keys are narrowly scoped and the procedure is rehearsed.

Does rotating a key prevent a breach?

No. It limits how long a leaked credential stays useful and gives you a practised way to revoke one. Prevention comes from scoping, storage, and keeping keys out of code and client-side bundles.

How do I rotate without downtime?

Use an overlap window: create the new key, deploy it, confirm through usage data that traffic has moved, then revoke the old key. Never revoke and replace in a single step.

What should I do the moment a key is exposed?

Revoke it before analysing. Then review its usage history for unfamiliar calls, rotate anything stored alongside it, and fix the path that let it escape.

Is an environment variable good enough for storing keys?

It is an acceptable baseline and far better than source control. A secret manager adds access logging and centralised revocation, which mostly pays off during an incident rather than day to day.

Should each service have its own key?

Yes, where the provider allows it. Per-service keys make rotation a local decision instead of a coordinated one, and they narrow what any single leak exposes.

The short version

Rotation is not a calendar entry; it is a capability. Scope keys narrowly so rotating one is cheap, always rotate with an overlap window and verify with traffic before revoking, set expiry and quotas at creation, and keep credentials out of repositories and client code. Then rehearse the on-demand path — because the rotation that matters is the unplanned one.