Most API key leaks are not breaches. Nobody breaks in. The key just gets copied somewhere you stopped thinking about, and that copy stays readable for months.
This list is the five places that happens most often, what the official docs say about each, and the fix. The pattern at the end is the part worth remembering: cleanup is always incomplete, so the fixes that work are rotation and restriction, not deletion.
1. Your frontend bundle
Anything shipped to the browser is public, at least for a secret key. That sounds obvious, but it is easy to rationalize: the key is in an environment variable, the variable gets inlined at build time, and now it is inside a JavaScript file served to every visitor.
Google's API key best practices say it directly: do not include API keys in client code. Stripe's key guide draws the line the same way from the other side. Only publishable keys (the pk_ ones) are safe to expose outside your backend, because they can create tokens but cannot perform sensitive operations like creating charges or reading account data. Secret keys have unrestricted permissions on every Stripe API.
The check is simple: search your built assets, not your source. If a key string shows up in the files your server sends to browsers, it is already public. The fix is to move the call server-side and let the browser talk to your API instead.
2. Git history, after you deleted the file
Deleting config.js and pushing the commit removes the key from the latest version. It does not remove it from history, and history is what matters.
GitHub's guide to removing sensitive data is blunt about the order of operations: if the sensitive data is a credential, your first step is to revoke or rotate it, and that alone may be sufficient. Even a full history rewrite with git-filter-repo leaves the old commits reachable in clones and forks, in cached views on GitHub, and through pull requests that reference them. GitHub Support will only help purge those cached views when rotation cannot mitigate the risk.
So the mental model is: a key that touched a shared repository is a key that is out. Rotate first, then decide whether the rewrite is even worth the coordination cost.
3. Docker image layers
A common Dockerfile pattern passes a token as a build argument so the image can fetch private dependencies. The token does its job during the build, and then stays visible in the image's history and metadata.
Docker's build secrets documentation warns about exactly this: build arguments and environment variables are inappropriate for passing secrets because they persist in the final image. Anyone who pulls the image can read them; build args remain visible in its build history even when they are no longer set at runtime. The documented fix is secret mounts, which make the value temporarily available inside the build container for one instruction and leave no trace in the result.
If you have ever built with --build-arg TOKEN=..., assume every image you shipped carries it. Rotate, switch to --secret, rebuild.
4. CI logs
Build systems try to protect you here, and the protection is narrower than it looks.
GitHub's Actions security hardening guide explains the mechanism: the runner's redaction largely relies on finding exact matches for the registered secret value. That means redaction can fail quietly. Structured data as a secret (a JSON blob, for example) reduces the chance of a clean match. A value derived from a secret, like a signed JWT or a Base64-encoded form, is a different string and will not be redacted unless you register it too. Their guidance if an unredacted secret reaches a log: delete the log and rotate the secret.
Related, from Google's best practices: do not pass API keys as query parameters. A key in a URL gets copied into access logs, browser history, and proxy logs everywhere the request travels. Use a header instead.
5. Your error tracker
Error monitoring tools capture the context around a crash: request payloads, headers, environment details. That context is exactly where keys live.
Sentry's data scrubbing docs say scrubbing is enabled by default and they recommend keeping it that way, but the mechanism matters: it works by redacting values that match known patterns and field names. A key sitting in an unusually named field or an odd format is a judgment call the scrubber can miss. The docs let you expand the scrubbed field list, and that is worth ten minutes after reading this.
The general habit: open your error tracker, look at a few real events, and check what your app is actually sending. Do the same for your logging pipeline if you have one.
The one habit that covers all five
Notice what every official guide converges on. GitHub: rotate first, deletion may not even be warranted. GitHub Actions: delete the log and rotate. Google: restrict keys so a leak does less damage, and delete keys you no longer use. Stripe: prefer restricted keys over all-powerful secret ones.
You cannot prove a key stayed private. You can make a leaked key worthless. Rotation turns every item on this list from an incident into a chore, and restriction means the chore is small. If you do one thing after reading this, rotate your oldest key and add restrictions to the rest.
I write about startups and AI at StartupWiki.
Cover photo: Jen Theodore on Unsplash.

Top comments (1)
"Cleanup is always incomplete, so rotate and restrict" is the right ending. I'd add a sixth place for anyone who publishes to npm: the package tarball.
npm decides what to ship from .npmignore, and only falls back to .gitignore when there is no .npmignore. So the classic leak is adding a small .npmignore to drop your tests, which silently switches off .gitignore, and the next npm publish ships your .env to the public registry.
Two cheap guards: use a "files" allowlist in package.json instead of ignore files, and run npm pack --dry-run before every publish to see exactly what goes out.