---
title: "API Key Rotation: How Often, and a 5-Step Zero-Downtime Plan"
description: "API key rotation works best with old and new keys running in parallel. Learn how often to rotate, a 5-step zero-downtime process, and a checklist."
url: https://cdnping.org/en/blog/api-key-rotation-how-to
language: en
published: 2026-11-30
category: "Developer tools"
tags: ["API key","secrets management","key rotation","environment variables","developer security"]
source: cdnping.org
license: Free to quote and summarize with a link to the url above.
---
# API Key Rotation: How Often, and a 5-Step Zero-Downtime Plan

There is no universal rotation interval for API keys. A common practice is to rotate on a schedule (for example every 90 days, which is just a typical internal policy number, not a hard requirement from any platform), and to rotate immediately when someone leaves the team, a leak is suspected, or the key shows up somewhere it shouldn't. The one rule for zero downtime: run the old and new keys in parallel for a while, and only revoke the old key once you've confirmed it no longer receives traffic.

The api key rotation process in this article applies to most AI API platforms. Where to create, disable, and delete keys differs by provider, so check each provider's official docs for menu locations and limits.

## Why rotate API keys on a schedule?

A key is a long-lived credential: whoever holds it can spend your quota. It ends up in many places: a local `.env`, CI variables, a coworker's chat history, a screenshot, a script nobody cleaned up. You can hardly be sure it hasn't been copied somewhere else. Rotating on a schedule caps the lifetime of that kind of invisible exposure to one cycle.

Rotation and leak response are two different things:

- Rotation is planned, so you can switch over calmly with both keys active.
- A leak is unplanned. Revoke first, clean up afterward. If you're dealing with a leak right now, go straight to [OpenAI API Key leaked: revoke it now, check usage, prevent a repeat](/en/blog/openai-api-key-leaked-response).

## How often should you rotate?

There is no official answer. Use these rules of thumb:

| Scenario | Suggested approach |
|---|---|
| Personal local development, small quota | Review every six months or once a year; rotate when you change computers or reinstall your OS |
| Production service with real user traffic | Rotate on a schedule set by your team policy, for example quarterly, and put it on the ops calendar |
| Shared by several people, someone leaves | Rotate on the day they leave, don't wait for the cycle |
| Suspected leak | Revoke immediately, skip the parallel process |
| Large quota or broad permissions | Shorten the cycle, and narrow the permissions and limits first |

Rather than shrinking the interval, it's more effective to make each key serve only one project or one environment, with a usage cap. Then if one key goes wrong, the blast radius is small. See [Managing API keys across multiple projects as a solo developer](/en/blog/manage-multiple-api-keys-projects) and [How teams manage API keys: one key per person, split budgets, and rotation](/en/blog/team-shared-api-key-management).

## The 5 steps for zero-downtime rotation

This assumes the platform allows several active keys under the same project or account at once. Major platforms usually do, but check the official docs for limits and rules.

### Step 1: Inventory where the key is used

Before you touch anything, list every consumer:

- Environment variables or the secrets manager of your production services
- CI/CD secrets
- Local dev machines, including coworkers' machines
- Keys entered into third-party tools, such as editor plugins and client apps
- Scheduled jobs, scripts, and monitoring probes

If you miss any one of them, it will start failing when the old key is revoked. If the inventory is unclear, label the old key and use the method in Step 4 to watch who is still using it.

### Step 2: Create the new key with a descriptive name

Put the project, environment, and creation month in the name, for example `shop-backend-prod-2026-11`. It makes checking usage and tracing the source much easier later. Set permissions and limits as needed; don't hand out full permissions just because it's "only a replacement."

### Step 3: Store the new key in a secrets store, not in code

Put the new key in an environment variable, a CI secret, or a secrets management service, not in the code repository. For local storage, see [How to store an API key safely: local development and team collaboration](/en/blog/store-api-key-securely). Windows users can check [Setting an environment variable for your API key on Windows](/en/blog/windows-set-env-variable-api-key).

### Step 4: Switch gradually, small to large

Don't swap everything at once. A suggested order:

1. Switch one dev machine or a test environment first, and send a minimal request to confirm it works.
2. Then the staging environment.
3. In production, use a canary or batched restarts: switch one instance first and watch the error rate.
4. Finally, switch the remaining instances, CI, and other consumers.

A minimal request to verify the new key works (using an OpenAI-compatible endpoint as an example; replace the address and model with your own):

```bash
curl https://your-endpoint/v1/models \
  -H "Authorization: Bearer $NEW_API_KEY"
```

If it returns a model list (HTTP 200), the key is valid. Auth headers differ between providers; see [Authorization: Bearer vs x-api-key](/en/blog/api-auth-header-bearer-vs-x-api-key).

### Step 5: Confirm the old key has no traffic, then revoke it

The console of many platforms shows each key's last-used time or usage. Wait until the old key's "last used" stops updating and you've watched for one full business cycle (for example a full week, covering the weekend and any weekly scheduled jobs), then revoke it.

If the platform offers "disable first, delete later," prefer disabling: if a consumer was missed, you can restore the key as soon as errors appear, so there's no long outage. After a few days with no problems, delete it.

## Rotation checklist

Copy this into your own ops docs:

- [ ] All consumers of the old key are listed (services, CI, scripts, personal tools)
- [ ] The new key is named, with permissions and limits set
- [ ] The new key is stored in an environment variable or secrets manager, not in code or commit history
- [ ] A minimal request with the new key succeeded in the test environment
- [ ] One production instance was canaried and the error rate did not rise
- [ ] All consumers have been switched
- [ ] The old key's last-used time has stopped updating, observed for one full business cycle
- [ ] The old key is disabled (if supported), and deleted a few days later with no errors
- [ ] The rotation date is recorded and the next one scheduled

## Getting 401 errors after rotation?

First work out which consumer is failing, then look at the cause:

- Do the first and last few characters of the key in your logs match the old key? Then some consumer wasn't switched.
- The environment variable didn't take effect: the process wasn't restarted, or the terminal is still reading the old value.
- The key was copied with a space or newline: check both ends after pasting.
- Wrong auth header: platforms use either `Authorization: Bearer` or `x-api-key`. For the Anthropic-specific differences, see [ANTHROPIC_AUTH_TOKEN vs ANTHROPIC_API_KEY](/en/blog/anthropic-auth-token-vs-api-key).

If the key checks out but requests still fail, you can look at cdnping's [API status page](/en/status) to see whether the official API has had recent problems, then decide whether it's your configuration or the server side.

## What changes when you use a gateway?

If several tools use keys from the same gateway, the benefit is that you only have to rotate the key at one source and then sync the configuration in each tool. Take AGI Plan as an example (AGI Plan is a commercial partner of cdnping): it is a monthly-subscription, CNY-billed gateway that aggregates models from multiple providers and needs no overseas credit card. Tools such as Claude Code and Codex only need one endpoint address changed, and you can switch between capability groups using the same quota. The site states clearly that it is not an officially authorized channel, and some models are marked as overseas-only; check the [models page](https://agiplan.dev/models) for the models and regions actually available. Onboarding is: sign up with an emailed verification code, pick a plan and create a key, change one setting. See the [getting-started page](https://agiplan.dev/startup) for details.

The same rotation principle applies: create a new key, change that one setting in each tool, and delete the old key once you've confirmed it's no longer used. The exact steps are in the AGI Plan console.

## Next steps

1. Do Step 1 today: list every key you have and where it's used, in one table.
2. Find keys with unclear purpose, overly broad permissions, or more than a year without rotation, and deal with those first.
3. Pick one low-risk key and walk through the full 5-step process before rolling it out to production services.
4. Put the next rotation date on your calendar.
