# How to rotate API keys without downtime

Source: https://botpassword.com/guides/how-to-rotate-api-keys

To rotate an API key without downtime, create a second key, deploy it everywhere the first is used, confirm the first is no longer in use, and only then revoke it. The key idea is overlap: at no point should a consumer be left holding only a dead key.

[Key rotation](/learn/key-rotation) is routine hygiene and also the core of incident response, so a rotation you have practiced is a leak you can survive. If you are rotating because a key has already been exposed, skip the overlap and go to [what to do when an API key leaks](/guides/what-to-do-when-an-api-key-leaks).

## When should you rotate a key?

1. **On a schedule** that fits the key's power. Broad, long-lived keys deserve shorter intervals.
2. **After a person with access leaves** a team or project.
3. **After any suspected exposure:** a key in a repository, a log, a chat, a screenshot, or on a machine that ran compromised software. In the 2025 compromise of the `tj-actions/changed-files` GitHub Action, the guidance was to treat every secret available to the affected jobs as exposed and rotate it ([incidents](/incidents)).
4. **When you cannot say where a key is used.** Rotating is how you find out, deliberately and with a rollback.

## What does a safe rotation look like?

1. **List the consumers.** Find every service, script, CI variable, container and teammate that holds the key. This is the step that fails most often, because copies spread ([secret sprawl](/learn/secret-sprawl)).
2. **Create the new key with the same or narrower scope.** Do not copy an over-broad old key. Use the rotation as a chance to apply [least privilege](/learn/least-privilege).
3. **Store the new key in your secrets store first.** Make sure the new value is saved before you do anything that could lose it.
4. **Deploy the new key to every consumer.** Roll it out gradually where you can.
5. **Verify.** Make an authenticated request with the new key, and watch your error rates.
6. **Check that the old key is idle.** Use the provider's last-used information or request logs.
7. **Disable, then delete the old key.** Where the provider allows it, deactivate first and delete after a waiting period, so you can reverse the change.
8. **Record the date** and the next due date.

## How does rotation work at specific providers?

The steps below are summarized from each provider's documentation, linked on the pages named. Confirm against the source before relying on them.

- **Stripe.** Open the API keys page, choose **Rotate key** for the key and select an expiration for the old one. The old and new keys both work for up to 7 days; choosing **Now** deletes the old key immediately. Stripe also lets you view request logs per key. See [/keys/stripe](/keys/stripe).
- **AWS.** An IAM user can hold two access keys, which is what makes zero-downtime rotation possible. Create the second key, update your applications, check **Last used** with `aws iam get-access-key-last-used`, deactivate the old key with `aws iam update-access-key` and status `Inactive`, then delete it after a waiting period. AWS recommends deactivating before deleting. See [/keys/aws](/keys/aws).
- **Cloudflare.** On **My Profile**, **API Tokens**, choose **Roll** from the token's menu and confirm. Rolling invalidates the old secret, and the new one keeps the same access, so there is no overlap: deploy quickly. See [/keys/cloudflare](/keys/cloudflare).
- **GitHub.** GitHub does not document an in-place regenerate flow on the token page, so rotation is create, update, delete. Generate a new token with the same owner, repositories and permissions, update every system using the old one, then delete the old token under **Developer settings**. For automation, GitHub App installation tokens expire on their own after one hour, which avoids the problem. See [/keys/github](/keys/github).
- **Anthropic.** Create a new key scoped to the workspace, deploy it where `ANTHROPIC_API_KEY` is read, confirm requests succeed, then delete the old one. Expiration is set at creation and cannot be changed afterwards. See [/keys/anthropic](/keys/anthropic).
- **npm.** Generate a new granular token with the same permissions, update CI secrets and `.npmrc` entries, then delete the old token. npm notes revocation can take up to an hour to take effect, and for CI publishing it recommends trusted publishing over bypass-2FA tokens. See [/keys/npm](/keys/npm).

Not every provider has a documented rotation flow. The OpenAI row in the dataset, for example, has no confirmed rotation steps, so check the dashboard ([/keys/openai](/keys/openai)).

## How do you avoid downtime in practice?

- **Overlap, don't swap.** Use the provider's two-key or grace-period feature when it exists.
- **Read keys at start-up or on a refresh**, not baked into images, so a rotation does not need a rebuild.
- **Change one environment at a time**, starting with staging.
- **Have a rollback.** Keep the old key deactivated, not deleted, until you are sure.
- **Beware of caches.** Long-running workers and connection pools may keep using the old key until restarted.

## Can rotation be automated?

Yes, partly. The most reliable pattern is to avoid long-lived keys: use short-lived, automatically issued credentials such as AWS IAM roles or OpenID Connect from CI, so there is nothing to rotate ([secrets in CI/CD pipelines](/guides/secrets-in-ci-cd-pipelines)). Where you must keep a static key, a script can create the new key through the provider's API, write it to your secrets manager, trigger a redeploy and revoke the old one later. Two cautions: make each step idempotent so a retry does not create a second key, and persist the new value before you revoke anything. Anything you cannot automate end to end, such as a dashboard-only flow, should have a written runbook.

## What about keys used by AI agents?

If an AI coding agent could read a key, treat that key as exposed to the tool's logs and provider, and rotate it on a tighter cycle. Better, stop giving the agent the value at all: see [giving AI coding agents API access safely](/guides/give-ai-coding-agents-api-access-safely).

## Where does BotPassword fit?

BotPassword runs rotation as a durable sequence: record the plan, create the new credential, store it as a pending vault revision, verify it, promote it, and revoke the old one after a grace period, with the default for personal credentials set at 10 minutes. An agent can request the rotation without being handed either key. Where a provider has no rotation API, BotPassword falls back to a dashboard-assisted or human-assisted flow. The reasoning is described in [agent-driven key rotation](/blog/agent-driven-key-rotation).

## Frequently asked questions

### How often should I rotate API keys?

Rotate on a schedule that matches the key's risk, and always after a suspected exposure, a departing team member or a tool compromise. A regular rotation also proves your process works before you need it in an emergency.

### Will rotating an API key break my application?

Only if the old key is revoked before every consumer has the new one. Run both keys in parallel, confirm the old one is no longer used, then remove it.

### What is the difference between rotating and revoking a key?

Rotating replaces a key with a new one and retires the old one on a scheduled basis. Revoking just disables a key, which is the right first move in an emergency.

### Can I rotate a key automatically?

Yes where the provider has an API or a built-in roll feature, and the safest setups avoid long-lived keys by minting short-lived credentials instead. Many dashboards still require a manual step.
