Skip to main content

Wolfcore Cyber Security

API Key Rotation: How Often and How to Do It Safely

Written by Ruben Selby, founder and penetration tester at Wolfcore. Last reviewed 1 December 2026.

Part of the Wolfcore Resources library: Credentials, identity and exposure.

In one sentence: API key rotation means replacing the secret that lets one piece of software talk to another, on a schedule and straight away after any leak, so a stolen key stops working.

What it is

An API key is a long secret string that proves a program is allowed to use a service, for example a payment provider, a mapping service or your own back end. Anyone holding the key can usually act as your program.

Rotation means creating a new key, switching your systems to it, then revoking the old one. This page focuses on the practical routine. The wider topic is covered in secret-rotation, and finding keys that have already leaked is covered in api-key-exposure.

The NCSC notes that API keys have no built-in expiry, so a compromised key can be abused indefinitely unless someone revokes or rotates it. They are also prone to theft through phishing, leaked source code and poor storage, and they often grant broad access instead of tailored permissions.

How it works

OWASP’s Secrets Management guidance sets out the lifecycle: creation, rotation, revocation and expiration.

  1. Create the new key with the least access the job needs.
  2. Deploy it to the systems that use it, ideally from a secrets manager rather than from code.
  3. Switch traffic to the new key and confirm it works.
  4. Revoke the old key. OWASP says secrets that are no longer needed or may be compromised must be revoked so access stops.
  5. Record who had access, when it was used and when it was last rotated. OWASP lists these as the questions incident responders need answered.
Rotation without downtime: overlap, then revokeCreate newkey, leastaccess1>Deploy new keyfrom a secretsmanager2>Check trafficuses the newkey3>Revoke theold key4>Log who, when,last rotated5

Who it affects and how badly

Any business that connects systems with keys, from a sole trader with an online shop to a SaaS start-up with hundreds of integrations, in any country.

  • Small business: a leaked payment or email-sending key can be used to send mail as you or run up charges.
  • Mid-sized business: keys copied into code repositories and chat tools spread widely, and nobody knows where the live copies are.
  • Regulated business: a key that opens a customer database makes the leak a personal data breach.

Worked example (illustration, not a real case): a 12-person SaaS start-up put a cloud storage key in a public code repository by mistake. It deletes the file the same day but never rotates the key, because “it’s gone now”. The key was copied while public. Months later it is used to download customer exports. Deleting the file was never enough. Revoking and replacing the key the same day would have made the copy useless.

What we check on a test

A Wolfcore tester looks at how keys are handled, as method points:

  • Whether keys appear in front-end code, mobile apps, public repositories, build logs or error messages.
  • Whether a key grants more access than its job needs.
  • Whether the same key is shared across environments such as test and live.
  • Whether the API rejects a revoked key straight away.
  • Whether anyone can say when each key was last rotated.

Rules and regulators

  • UK: the NCSC API guidance names storing credentials in code and the lack of expiry as common failings, and says a compromised API can disrupt operations, damage reputation and may lead to fines from regulators.
  • EU: GDPR Article 32 requires appropriate technical and organisational measures for the risk, which includes controlling access keys to personal data.
  • US: no federal rule names API key rotation. Contracts, SOC 2 style controls and state laws often expect it in practice.

How to detect it

  • List every key you own, where it is stored and who can read it. If you cannot, that is the first finding.
  • Search your code repositories and build logs for key patterns using the secret scanning built into your code host.
  • Check the provider’s usage dashboard for calls from unfamiliar locations, unusual volume or unexpected endpoints.
  • Turn on auditing. OWASP says you should log who requested a secret, when it was used and attempts to reuse expired secrets.

How to fix or prevent it

  1. Revoke and replace any key you know or suspect has leaked, immediately, then delete it from the place it was exposed.
  2. Store keys in a secrets manager or your cloud’s native secret store, not in code, documents or chat.
  3. Give each key the least access it needs and use separate keys per system and per environment.
  4. Set an expiry date where the provider allows it, and rotate on a defined schedule suited to how sensitive the key is.
  5. Automate rotation so it is repeatable and does not rely on one person remembering.
  6. Prefer short-lived tokens or OAuth 2.0 over long-lived keys where you can, as the NCSC guidance recommends.
  7. Add rate limits and alerts on the provider side so misuse shows up fast.
  8. Keep a simple register of keys with owner and last-rotated date.

FAQ

How often should I rotate? OWASP says lifetime depends on the secret’s function and what it protects, from minutes to years. It does not give one number for API keys, so set it by sensitivity and always rotate after a suspected leak.

Is deleting the code enough after a leak? No. OWASP’s remediation steps are revocation, rotation and deletion, in that order of priority.

Does this apply to user passwords? OWASP says user credentials are excluded from regular rotation and should change when compromise is suspected.

Related terms

More in the Credentials and exposure hub.

Sources

How Wolfcore helps

Breachy checks whether your staff email addresses and passwords are already in known breaches and gives you the exact steps to close each exposure. See the Breachy.

Replacing keys is only half the job. If a key has already leaked, Breachy shows what is exposed, and our penetration testing looks for keys left in code and services. See our pricing.

Wolfcore

Would your staff click a phishing email?

Run a real, safe phishing test on your team with Phishy. Free for 14 days, up to 10 staff, no card. We help you set it up.

Start a free phishing testFree website security scanor book a 30 minute call