I Leaked My API Key on GitHub — What Now? A Containment Playbook for Developers and Small Teams
Revoke first, clean up later. The order of operations after an API key leak, how to find out what was used, and the habits that keep the next leak small.
1. The first fifteen minutes: contain, do not clean
The instinct after spotting a key in a public commit is to hide the evidence: delete the file, force-push, make the repository private. None of that stops anyone who already copied the key. The only action that ends the exposure is invalidating the key at the provider. So the first fifteen minutes look like this: open the provider console, disable or revoke the key, and accept that some service of yours will break until you deploy a replacement. A broken deployment costs you a redeploy; a live leaked key costs you money for as long as it stays live. If you are not sure whether the key was really exposed, revoke anyway — the cost of a false alarm is a few minutes of config work.
2. Why someone finds your key before you do
Usage-billed API keys are worth money the moment they are copied, so people run automated scanners across public code hosts, published packages, container images and paste sites, looking for strings that match known key formats. A scanner does not get tired and does not need your password; it only needs the key to appear somewhere public for a short time. Once a key is found, it tends to be used quickly, because whoever found it assumes you will revoke it soon. That is why the size of the damage comes down to two numbers you control: the time between exposure and revocation, and the maximum amount the key can spend. Detection shortens the first; small prepaid top-ups, separate keys, expiry and application-side limits reduce exposure. Accounts billed to a saved card with no prepaid ceiling carry the most risk, because spending continues until a bill arrives or a provider-side limit kicks in.
3. Where keys actually leak (it is not only git)
A committed .env file is the classic case, but it is far from the only one. CI logs: a build step that prints the environment, or an error that echoes a request with its headers. Container images: a key copied into an image layer stays there even if a later layer deletes the file. Notebooks: a key set in a cell and saved with its output, then shared or pushed. Client code: anything shipped to a browser, phone or desktop app can be read by the person running it; obfuscation only slows them down. Support threads and screenshots: a full curl command pasted into a forum post or an issue. Third-party tools: online playgrounds, editor extensions and shared config templates that ask you to paste a key — from then on, that key is only as safe as the tool. When you review an incident, check all of these, not only the repository.
No overseas card needed: cocodot's OpenAI-compatible API — switch your base_url to call the models on sale.
Top up via Alipay / WeChat Pay, USDT / USDC · billed per token · switch your base_url to connect
4. Scoping the damage after you revoke
Once the key is dead, work out what it cost. Pull usage for the window between the first possible exposure (the commit time, the post time, the build time) and the moment you revoked. Three signals separate your traffic from someone else's: calls at hours when none of your jobs were running, models you never use, and a sudden jump in volume on one day. Many consoles show a last-used time for each key, which also tells you whether a supposedly idle key has been active. Write down the time range, the amounts and the key name before you contact support — that is what a provider needs to look into it. Whether unauthorized usage is refunded is up to each provider's policy, so do not plan around getting it back; plan around never having much to lose.
5. Rewriting history is cleanup, not containment
After the key is revoked and replaced, it is still worth removing it from the repository so nobody stumbles on it later and so your scanners stop flagging it. Tools such as git filter-repo can rewrite history to drop the file or the string. Two things are easy to forget. First, every collaborator has the old history in their local clone; unless they re-clone or reset, the next push can bring the key straight back. Second, forks and caches made while the repository was public are outside your control, which is exactly why revocation has to come first. If the leak was in a container image or a published package, rebuild and republish without the key and remove the affected versions where the registry allows it.
6. Limit the blast radius before the next leak
If one key powers production, CI, your laptop and a contractor's script, revoking it takes everything down at once, and that cost is what makes people hesitate — which is how a small leak becomes a large one. Split keys by blast radius: one per production service, one for CI, one per person or project, each with a name that says what it is for. Anthropic's documentation is a useful reference here. It distinguishes personal keys, which act as you and stop working when you leave the organization, from service account keys, which it recommends for shared and automated workloads such as CI and production services. It also lets you choose an expiration when you create a key — presets of 3 hours, 1 day, 7 days or 30 days, a custom duration, or Never for keys you keep in a secrets manager and rotate yourself — and an expired key returns a 401 authentication_error and cannot be reactivated. The same documentation notes that disabling a key in its console is reversible while deleting it is permanent, so disabling is a fast first move when you are still investigating. The idea transfers to any provider: decide who uses a key and for how long at the moment you create it, and give demo or contractor keys a short life.
7. Keeping keys out of code, bundles and logs
Read keys from environment variables or your platform's secret settings, never from source. Ignore every real .env file and commit only a template with placeholder values. Add a pre-commit secret scan so a key is caught on your machine rather than on a public host, and run a full-history scan with a tool such as gitleaks or trufflehog in CI. Never ship a key to a client: have the browser or app call your own backend, and let the backend attach the key, apply rate limits and check who the user is. Configure your logger to redact the Authorization and x-api-key headers. When debugging, never echo or print a key to confirm it loaded — checking that it is non-empty and has the expected length tells you everything you need, and nothing ends up in your terminal history, your CI log or a screenshot.
# .gitignore: ignore every real env file, keep only the template
.env
.env.*
!.env.example
# Confirm the variable is set without printing its value
if [ -n "$OPENAI_API_KEY" ]; then
printf 'key loaded, length %s\n' "$(printf '%s' "$OPENAI_API_KEY" | wc -c)"
else
echo 'OPENAI_API_KEY is not set'
fi
# Scan full git history with gitleaks or trufflehog;
# check each tool's official docs for the current command8. How key management works on cocodot
On cocodot, a key is shown once when you create it and only a hash is stored, so a lost key cannot be recovered: revoke it and create a new one, and save the new value straight into your environment or secret store. You can give each key a name when you create it, and the key list shows each key's prefix, creation date and last-used time — which makes the idle-key check from section 4 a ten-second job. Revoking takes effect immediately and breaks any program still using that key, which is another reason to split keys first. The balance is prepaid and billed per token. New requests are refused when no credit is available, but requests already in flight may still incur charges. Small top-ups reduce exposure; they do not replace application-side step, concurrency and budget limits. If a replacement key still fails, the 401 checklist is at /hub/api-401-invalid-api-key-fix, and budget guardrails for agent loops are at /hub/agent-api-cost-runaway.
What you see → what it usually means → first move
| What you see | What it usually means | First move |
|---|---|---|
| Usage at hours when nothing of yours was running | Someone else is holding the key | Revoke now, then compare usage against your own job schedule |
| A .env or config file with a real key was pushed to a public repo | The key is in history and may already be copied | Revoke, then rewrite history and tell collaborators to re-clone |
| A key is embedded in a web bundle, mobile app or browser extension | Anyone with devtools or an unpacker can read it | Revoke, then move the call behind your own backend |
| A screenshot, issue or chat message shows a full request | Public text is indexed and forwarded | Revoke; use placeholders from now on |
| The new key still returns 401 | Something still reads the old value | Check env vars, .env files, CI secrets and deploy platform settings one by one |
| Revoking the only key took every service down | One key was shared by everything | Split keys by service and owner before the next incident |