cocodot
← Back to guides
Local card declined for overseas AI? cocodot: one card + one key
Security PracticesUpdated 2026-10

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.

TL;DR: If an API key has been exposed, disable or revoke it at the provider before you do anything else. Deleting the commit, making the repository private or force-pushing does not make the key safe, because anyone who already copied it can keep using it. A usage-billed key is a payment credential: whoever holds it can spend your balance or charge your card without your password. After revoking, issue a new key from a secret store, update every place that reads it, review usage for the exposure window, and only then rewrite history or delete posts. To keep the next incident small: one key per service or person so revoking breaks one thing, an expiry on short-lived keys, keys only on the server side, and small prepaid top-ups plus application-side spending controls. And when debugging, never print a key — check whether it is set and how long it is.

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.

Ignore real env files and confirm a key loaded without printing it
# .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 command

8. 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 seeWhat it usually meansFirst move
Usage at hours when nothing of yours was runningSomeone else is holding the keyRevoke now, then compare usage against your own job schedule
A .env or config file with a real key was pushed to a public repoThe key is in history and may already be copiedRevoke, then rewrite history and tell collaborators to re-clone
A key is embedded in a web bundle, mobile app or browser extensionAnyone with devtools or an unpacker can read itRevoke, then move the call behind your own backend
A screenshot, issue or chat message shows a full requestPublic text is indexed and forwardedRevoke; use placeholders from now on
The new key still returns 401Something still reads the old valueCheck env vars, .env files, CI secrets and deploy platform settings one by one
Revoking the only key took every service downOne key was shared by everythingSplit keys by service and owner before the next incident

FAQ

Can I get a refund for charges from a stolen API key?

That depends entirely on the provider's policy, so do not count on it. Revoke the key, collect the time range, amounts and key name, and submit them through the provider's support channel. The reliable protection is upstream: separate keys, small prepaid balances and expiry on short-lived keys.

I deleted the repository. Do I still need to rotate the key?

Yes. If it was ever public, assume the key was copied. Deleting the repository only stops people who arrive later. Revoke and replace first, then deal with the repository.

Is it safe to put an API key in frontend code if I obfuscate it?

No. The key travels in the request the browser sends, so anyone can read it in the network panel. Keep it on a server you control and have the frontend call that server.

How often should I rotate API keys?

There is no single standard. A regular schedule plus immediate rotation when someone leaves, a contract ends or a leak is suspected is the usual baseline. Anthropic's documentation recommends storing keys in a secrets manager, rotating them periodically and disabling or deleting any key you suspect has leaked.

Support asked for my API key to debug an error. Should I send it?

Do not send the full key. The key's name or prefix, the time of the failure and the complete error message are enough to investigate. Be suspicious of anyone who insists on the whole key.

A scanner flagged an old key in my history that I no longer use. Does it matter?

Check that it is actually revoked in the provider console rather than just unused. An unused key that is still active is a live credential. Once it is revoked, removing it from history is housekeeping.

About cocodot

cocodot is a payment and AI access service for developers and cross-border teams in mainland China. It provides US-BIN virtual cards issued by a licensed institution — used to pay for overseas subscriptions and ad accounts — and an OpenAI-compatible AI API gateway for calling Claude, GPT and Gemini from within mainland China. Both share one wallet, funded by Alipay / WeChat Pay, USDT / USDC and accounted in USD. Fees are listed on the pricing page.

Service scope, pricing and limits →
OpenAI-compatible API, pay in RMBGet API access