An API key leaked: what to do in the first hour
Short answer: If an API key has been exposed, in a public repository, a screenshot, an email or front-end code, revoke or rotate it first, before investigating anything else. Then replace it everywhere it's used, check the provider's usage logs and billing for activity you don't recognise, and find out how it leaked so it doesn't happen again. Deleting the file or the commit isn't enough on its own: once a key has been seen, assume it's compromised.
API keys connect your website or app to payment providers, email services, maps, AI tools and more. They're easy to paste into the wrong place. The first hour after you discover a leak matters most, so here is a step-by-step response plan, followed by how to stop it recurring.
Why speed matters
Google Cloud's best-practice page on API keys is blunt: compromised keys can lead to unexpected charges on your account or unauthorised access to your data. It warns specifically against putting keys in client code or committing them to code repositories, because keys hardcoded in source code or stored in a repository are open to interception or theft.
GitHub's documentation on secret scanning makes a related point: the priority when a credential leaks is to revoke it. It notes you can remove secrets from Git history, but that this is time-consuming and often unnecessary once the credential has been revoked.
Step 1: revoke or rotate the key (minutes 0–10)
- Log in to the provider's dashboard (payment, email, mapping, AI or cloud service).
- Rotate if the provider supports it (create a new key, then disable the old one), or revoke the old key outright if you can tolerate a brief outage.
- If you don't have access yourself, contact whoever does, now. Many providers also have a security or support contact for compromised credentials.
Accept that something may break for a few minutes. A short outage is cheaper than an abused key.
Step 2: put the new key in place (minutes 10–30)
- Update the new key wherever the old one was used: hosting environment variables, server configuration, automation tools, mobile app backends.
- Store it as an environment variable or in your hosting provider's secrets settings, not in code.
- Restart or redeploy the services that use it and test the affected features (checkout, contact form emails, maps, chatbot).
Step 3: check for misuse (minutes 30–60)
- Review the provider's usage dashboard and logs for the period since the key was exposed: unusual volume, unfamiliar regions, odd endpoints.
- Check billing for unexpected charges and set or tighten spending alerts and limits.
- If the key could access customer data, note what it could reach and when. Check your rules where you operate about notifying anyone if personal data may have been accessed.
- Keep a short written record: what leaked, where, when you found it, when you revoked it, what you saw in the logs.
Step 4: find and close the leak
Common sources:
- A public code repository. Committed accidentally in a config file, often long ago.
- Front-end code. Keys embedded in JavaScript can be read by anyone who views the page source. Google's guidance recommends the client pass requests to your server, which adds the credential and makes the call.
- Screenshots and screen shares of dashboards or config files.
- Emails, chat messages and tickets where keys were pasted to "send it over".
- Former staff or contractors who still hold copies.
Remove the key from wherever it appeared. If it's in a repository's history, decide with your developer whether to rewrite history; GitHub's point stands that revocation is the essential step.
Step 5: prevent the next one
Use secret scanning and push protection
GitHub's documentation explains that secret scanning checks a repository's entire Git history on all branches for hardcoded credentials such as API keys, passwords and tokens, and raises alerts. It runs automatically for free on public repositories; for organisation-owned private repositories, availability depends on your GitHub plan and add-ons. GitHub also works with partner service providers: when it detects a partner's secret, it notifies the provider so they can act. Push protection goes further, blocking pushes that contain secrets before they reach the repository.
Other code hosts offer comparable features; check yours.
Restrict every key
Google's guidance recommends adding restrictions to keys, limiting which APIs, websites, IP addresses or apps can use them, so a leaked key is less useful. Many other providers offer similar scoping, such as restricted or read-only keys.
One key per person and app
Google suggests giving each team member their own key per application, which helps control access, provides an audit trail and limits the impact of a compromise.
Rotate and prune
Rotate keys periodically and delete ones you no longer use, as Google's page advises. Fewer live keys means fewer things to leak.
Agree on how keys are shared
Use a password manager's sharing feature or your host's secrets settings, never email or chat.
A one-page checklist to keep
- [ ] Revoke/rotate the exposed key
- [ ] Deploy the replacement from environment variables
- [ ] Test affected features
- [ ] Review usage logs and billing
- [ ] Record what happened
- [ ] Remove the key from where it leaked
- [ ] Turn on secret scanning/push protection
- [ ] Restrict and scope remaining keys
- [ ] Review who has access
FAQ
I deleted the commit with the key. Am I safe?
Not necessarily. The key may already have been copied, and it may remain in the repository's history or forks. Revoke it regardless.
Is it okay to put an API key in JavaScript on my website?
Only for keys designed to be public and properly restricted, such as some browser map keys restricted to your domain. Secret keys belong on your server.
How do I know if the leaked key was used?
Check the provider's usage logs, dashboards and billing for the exposure period. If logs are limited, ask the provider's support.
Who should hold admin access to our provider dashboards?
At least two people in the business, using company email addresses, so a key can be revoked quickly even if your developer is unavailable. Contractors can have their own scoped access that you remove when the work ends.
Found a key somewhere it shouldn't be and want a second pair of eyes on the clean-up? Get in touch and we'll help you rotate it and close the gap.