Guide

Leaked API Key or Secret? What to Do Right Now

Short answer: a leaked API key is an emergency. If a key, token or password has been shared, committed or pasted anywhere, rotate it first: revoke it at the service that issued it and create a new one. Only after that, clean up the code. Deleting the line does not make the secret safe, because copies may already exist in Git history, logs and backups. The Secret Scanner helps you find what else is lurking in a file and gives you a redacted copy you can share safely.

The order matters

Most people do this the wrong way round: they delete the line, push a fix and relax. The old commit is still there, and anyone who cloned, forked or scraped the repository in the meantime has the key. GitHub's documentation on removing sensitive data is blunt about it: treat the secret as compromised and revoke or rotate it as the first step. Rewriting history is a separate, more involved job, and once the secret is rotated it may not be necessary at all.

So, in this order:

  1. Revoke or rotate the key at the provider (AWS, GitHub, Stripe, Slack, your database and so on). Create a new one.
  2. Check for use you did not make. Look at the provider's access logs, billing and recent activity for the period the key was exposed.
  3. Replace the secret in your code with a reference to an environment variable or a secrets manager, and deploy with the new value.
  4. Clean the history if you still need to (see GitHub's guide below), knowing that this does not un-leak anything.
  5. Find the next one. Where there was one secret there is often a second. Scan the rest of the file and the rest of the project.

What a scanner can and cannot do

A scanner looks for strings with the shape of a secret. The Secret Scanner checks for:

  • AWS access key IDs (AKIA, ASIA) and AWS secret keys written next to a name such as aws_secret;
  • GitHub tokens (ghp_, gho_, ghu_, ghs_, ghr_, github_pat_) and Slack tokens (xoxb-, xoxp-, xoxa-, xoxs-);
  • Stripe live keys, Google API keys, Anthropic keys (sk-ant-) and OpenAI-style keys (sk-proj-, sk-);
  • JSON Web Tokens, PEM private key blocks and passwords inside URLs such as scheme://user:password@host;
  • assignments such as password, secret, token, api_key or access_key set to a string of eight or more characters;
  • high-entropy strings (20 to 200 characters of base64 or hex alphabet with Shannon entropy of 4.0 or more), always marked low confidence.

It never contacts any service, so it cannot tell you whether a finding is real or still active. A clean result is not proof that the text is safe. It cannot see secrets without a recognisable shape or prefix, short passwords, secrets split across lines, or ones encoded another way (a base64-encoded key, for example). Random-looking IDs and hashes can come up as low-confidence false alarms, and real secrets with a long run of lowercase letters can slip past the entropy check.

Read the confidence levels

In the tool's example .env file, the AWS keys, the GitHub token and the password inside the database URL are high confidence, a db_password assignment is medium, and a random-looking string is low: it might be a hash, an ID or encoded data. Work through high first, but do not ignore the rest.

Sharing code without sharing secrets

You often need to paste a config into a chat, a ticket or an AI assistant. Use the scanner's redacted copy: every finding is replaced by a label like [REDACTED:aws-access-key-id], and values in the results table are masked (the first 4 and last 2 characters for values of 16 or more characters, only the first 2 for shorter ones, and just the BEGIN line for a private key). You can also copy the findings as JSON with masked values only.

A related habit: scan text for hidden characters before you trust it, because a pasted command or config can hide more than it shows (see How to detect hidden Unicode characters).

How to find a leaked API key with the Secret Scanner

  1. Open the Secret Scanner.
  2. Paste the text, code or config file. Up to 2,000,000 characters are scanned.
  3. Read the findings: type, line and column, a masked preview and specific advice for that kind of secret. The table lists the first 500 findings, while redaction and the JSON export cover all of them.
  4. Switch on Show values only if you need to see the full text on screen.
  5. Copy the redacted text or the JSON (masked).
  6. To silence a known false alarm, put dte-ignore on that line. Obvious placeholders such as your_api_key, example, xxxx and changeme are skipped automatically.

Keep the next one out

  • Keep secrets in environment variables or a secrets manager, never in source files.
  • Make sure local .env files are ignored by Git (see what to do when a .env file is already tracked). The Gitignore Generator can build the file.
  • Use short-lived, least-privilege credentials where the provider allows it.
  • Add an automated secret check to your commit or CI process, and keep a scanner like this as a quick manual second look.

Limits to know

  • The scanner checks the shape only. It does not prove a secret is valid or active.
  • Anything not recognisable by pattern is missed, including secrets split or encoded.
  • It is a helper, not your only protection.
  • Everything runs in your browser and nothing is uploaded.

Frequently asked questions

I pushed an API key to GitHub. What now? Revoke or rotate it immediately, check the provider's logs for unexpected use, then replace it in your code. Cleaning Git history comes after.

Is deleting the commit enough? No. Forks, clones, logs and backups may keep a copy. Rotation is what makes the old value useless.

How do I find secrets in code? Paste the file into the Secret Scanner, then scan the rest of the project with an automated tool as well.

Is it safe to paste code with keys into an AI assistant? Not if the keys are real. Redact first and share the redacted copy.

Why does the scanner flag something that is not a secret? High-entropy strings can be hashes or IDs. They are marked low confidence for that reason.

Related tools and guides

Sources and further reading