Incident response guide

How to rotate a leaked API key — step-by-step rotation checklist

The most important step after discovering a leaked API key is not cleaning up git history — it is revoking the key at the provider. A rotated key is useless to an attacker even if it stays in every commit forever. An unrotated key is live even after you delete it from every file and rewrite every commit in your repository.

Rotate first, clean history second

A common mistake when discovering a key in a public repository is to immediately focus on git history cleanup — running git filter-repo, force-pushing, and contacting GitHub support to purge cached views. These are all correct steps, but they come second.

The reason: once a key appears in a public repository, automated scanning bots (GitHub’s own secret scanning, TruffleHog, and commercial scanners) may capture it within minutes of the push. Rewriting history does not invalidate a credential that has already been copied. Revoking the key at the provider does.

First action: Open your provider’s dashboard right now and revoke the key before reading the rest of this guide. The provider-specific steps below show exactly where to go.

The 5-step rotation checklist

This sequence applies regardless of which provider issued the key or how the leak happened:

  1. Revoke the old key immediately. Go to your provider dashboard and delete or disable the exposed key. Do not wait until you have a replacement ready — a brief service interruption is preferable to continued exposure. See the provider-specific steps in the next section.
  2. Generate a replacement key. Create a new key in the same provider dashboard. If the provider supports scoped keys or fine-grained permissions, use the minimum permissions the consuming service actually needs — do not clone the broad permissions of the compromised key.
  3. Find every consumer of the old key. Search for the first 8–12 characters of the old key (enough to be unique) across: local .env files in all environments, CI/CD secrets (GitHub Actions, GitLab CI, Bitbucket Pipelines), deployment platform env vars (Vercel, Netlify, Railway, Render, Heroku), and any shared secrets vault or password manager your team uses.
  4. Deploy the new key everywhere. Update each location found in step 3. For production services, update env vars through the platform dashboard and trigger a redeploy if the application caches the key at startup. Confirm each consumer is working with the new key before moving on.
  5. Verify the old key is dead. Make a test API call using the old key value and confirm it returns a 401 Unauthorized or equivalent error. This confirms revocation took effect end-to-end, not just in the dashboard.

Provider-specific rotation steps

The exact location of key management differs by provider. Here are the steps for the most commonly leaked key types:

OpenAI

  1. Go to platform.openai.com → API keys
  2. Click the trash icon next to the compromised key and confirm deletion
  3. Click Create new secret key; give it a descriptive name and set an optional expiry
  4. Copy the new key immediately — it is only shown once
  5. Check the Usage page to review activity by key before deleting

Stripe

  1. Go to dashboard.stripe.com → Developers → API keys
  2. For a live key: click Roll key — this generates a new key and keeps the old one live for a transition window so you can update consumers without downtime, then expires the old one
  3. For immediate revocation without a grace period: click the key row → Delete
  4. For restricted keys, delete the key and create a new one with the same permission set
  5. Check Developers → Logs for API calls you did not make

AWS IAM

  1. Go to IAM → Users → [user] → Security credentials
  2. Click Make inactive on the compromised access key — this blocks use immediately without deleting it, so you can audit CloudTrail with the key ID still intact
  3. Click Create access key to generate a replacement; update all consumers
  4. Once consumers are migrated, click Delete on the old key
  5. Check CloudTrail for API calls from the compromised access key ID in the past 90 days

Google Cloud

  1. For API keys: Cloud Console → APIs & Services → Credentials → click the key → Delete
  2. For service account keys: IAM & Admin → Service Accounts → [account] → Keys → Delete key
  3. Create a replacement in the same location
  4. Check Cloud Audit Logs → Data Access logs for the service account for unauthorized calls

GitHub personal access tokens

  1. Go to Settings → Developer settings → Personal access tokens
  2. For classic tokens: click the token name → Delete
  3. For fine-grained tokens: same path under Fine-grained tokens
  4. When creating a replacement, prefer fine-grained tokens scoped to specific repositories; set expiry to 90 days (the maximum for fine-grained tokens)
  5. Check Settings → Security log to review recent activity for the old token

Twilio

  1. Twilio uses an Account SID + Auth Token pair. Go to console.twilio.com → Account → General settings
  2. Click Rotate auth token to generate a secondary token. Update all consumers to the new token, then click Promote secondary token to make it primary and invalidate the old one
  3. For API Key + Secret pairs (preferred for production), delete the key under Account → API keys & tokens and create a new one

Check whether the key was used maliciously

After rotating, check your provider’s audit logs for activity you did not generate. What to look for:

ProviderWhere to lookWhat to check
AWSCloudTrail → Event historyFilter by access key ID; look for IAM changes, EC2 launches, S3 GetObject on sensitive buckets
Google CloudCloud Audit Logs → Data AccessFilter by service account email; look for resource creation or data exports
StripeDevelopers → LogsFilter by API key prefix; look for charge creation, payout changes, customer data reads
OpenAIPlatform → UsageCheck token usage by key and date; look for spikes on dates you did not run any jobs
GitHubSettings → Security logFilter by token; look for repo clones, secret reads, or org-level actions
TwilioMonitor → Logs → DebuggerLook for message sends or voice calls you did not initiate

If you find evidence of unauthorized use — calls to endpoints you do not use, activity from unfamiliar geographic regions, or billing spikes — treat this as a security incident: document what was accessed and assess whether any user data was exposed.

Set expiry to prevent future incidents

Long-lived credentials without expiry are the most common root cause of key leaks turning into breaches. When creating replacement keys:

  • GitHub fine-grained tokens — set expiry to 90 days or less (the maximum supported). The renewal reminder builds the habit of regular rotation.
  • AWS — prefer IAM roles with short-lived STS temporary credentials over long-lived IAM access keys, especially in CI/CD pipelines. GitHub Actions supports OIDC with AWS natively, which eliminates stored credentials entirely.
  • Google Cloud — service account keys are long-lived by default; prefer Workload Identity Federation for CI/CD to eliminate stored JSON key files.
  • Stripe — use restricted keys with only the permissions your application actually calls, scoped to the minimum required read/write access per endpoint.
  • All providers — add a calendar reminder at 90-day intervals for any key that does not support expiry. Treat it as a recurring maintenance task, not a one-time setup.

After rotation: clean up git history

Once the key is rotated, the value in git history is a dead credential. Clean it up anyway — to avoid confusing future developers, to satisfy automated scanners, and to prevent someone from accidentally copying a pattern from an old commit.

The full process — removing a secret from git history with git filter-repo and BFG Repo-Cleaner, force-pushing, and coordinating with GitHub’s cache purge — is covered in the companion guide:

Remove a secret from git history →

Use LeakCheck to confirm exactly which files contain the pattern before starting the history rewrite — it avoids incomplete rewrites that leave a copy behind:

Paste and scan with LeakCheck →

Frequently asked questions

Do I still need to clean git history if I already rotated the key?

Yes, but it is lower urgency. Once you rotate, the value in history is a dead credential — an attacker who finds it cannot use it. You should still rewrite history to avoid confusing future developers, prevent automated scanners from continuing to flag the repo, and reduce risk if the key is ever accidentally re-enabled. The history cleanup guide covers the full process with git filter-repo and BFG Repo-Cleaner.

How do I know if my key was used before I rotated it?

Check your provider’s audit logs immediately after rotating. AWS has CloudTrail; GCP has Cloud Audit Logs; Stripe shows all API calls in Developers → Logs; OpenAI shows usage by key on the Usage page. Look for calls from unfamiliar IPs, geographic regions you do not operate in, or to endpoints you do not use. See the audit log table above for provider-specific paths.

What is the difference between revoking and rotating a key?

Revoking permanently invalidates a key with no replacement. Rotating means revoking the old key and issuing a replacement in a coordinated operation — the service continues with the new key. For an incident response, rotation is almost always the right approach: you need a working replacement, not just an invalidation.

Can I keep the old key live while I migrate consumers to the new one?

Only if you must avoid downtime and the provider supports parallel keys — Stripe’s “Roll” feature and Twilio’s secondary token model are designed for exactly this. Otherwise, accept a brief outage rather than extending exposure. The longer both keys are live simultaneously, the longer the window in which the compromised key can be used. Aim to revoke the old key within minutes of confirming the new one is deployed everywhere.

How do I find every place my API key is deployed?

Search the first 10–12 characters of the key (specific enough to be unique) across: all .env files in every environment, CI/CD secret stores (GitHub Actions, GitLab, Bitbucket), deployment platform dashboard env vars (Vercel, Netlify, Railway, Render, Heroku), any shared team secrets vault, and external integrations that relay the key to third-party services. Use grep -r "<first-12-chars>" . in your workspace as a quick first pass.

Does LeakCheck detect whether a key is still live?

No — LeakCheck identifies patterns that look like API keys in pasted code; it does not make outbound calls to providers to verify whether a key is active or revoked. It is most useful before keys leave your machine: scan your .env, a config file, or a staged diff before committing. For post-incident verification, check the provider’s own audit logs to confirm no new activity on the old key after rotation.

How often should I rotate API keys that were not leaked?

Security frameworks like SOC 2 and CIS Controls recommend rotating long-lived credentials at least annually; quarterly is more common for high-privilege keys. The most effective preventive measure is setting short expiry on new keys whenever the provider supports it — GitHub fine-grained tokens and AWS IAM temporary credentials via STS both support expiry by design, making rotation automatic rather than requiring a manual calendar reminder.

Also in the Copper Bay Labs ship-safety suite

Key rotation is one layer of a broader pre-deploy security posture. These tools cover the rest: