My API key leaked: a calm step-by-step plan

Published 2026-11-16, updated 2026-11-30 by Nordik. Written with AI from the sources listed at the end.

The short version

The risk
Anyone who saw the key keeps a copy and can use it on your account until you turn it off. Deleting the file does not help.
Does it affect you?
Yes, if a secret key showed up in public code, a chat, a shared document or an alert email.
What to do
Replace the key now, put the new one where only your server can read it, read the usage logs, then close the route it leaked through.

Rather not read it all? Copy this for your AI tool. Paste it into the tool you build with. It asks the tool to check your app for the problems in this guide and explain what it finds in plain words. Nothing is changed.

See the prompt
An API key from my app may have leaked. Help me respond and do not change anything yet. 1. List every secret key the app uses and where each one is stored. 2. Tell me which of them end up in code that every visitor downloads (for example a VITE_ or NEXT_PUBLIC_ variable, or a key written in a page or component) and which are in a committed .env file. 3. For each exposed key, tell me in plain words which service issued it, how to replace it there and turn the old one off, and which file must change so the new key stays on the server.
Jump to a part (12)
  1. Step 1: replace the key now
  2. Step 2: put the new key where only your server can read it
  3. Step 3: look at the usage
  4. If the leaked key is a Supabase key
  5. Step 4: do not bother rewriting history
  6. Step 5: find how it leaked
  7. Step 6: stop it happening again
  8. What to tell your users
  9. A worked timeline
  10. Questions people ask
  11. Keeping your head
  12. Check the rest of the app

You just noticed a secret key in your public code, in a chat, or in an alert email. A key is a long password your app uses to prove who it is to another service, and a secret one opens everything on your account there.

First: do not panic, and do not spend your time deleting the file. The key must be replaced. Hiding it afterwards does not help, because anyone who saw it keeps a copy. Here is the order that works.

Step 1: replace the key now

Log in to the service that issued the key and rotate it, which means make a new one and turn the old one off.

  • Stripe's documentation describes rotating as revoking the key and generating a replacement, with an optional grace period of up to seven days so your live app does not break while you update it.
  • Supabase advises rotating a leaked secret key once the cause is fixed.
  • GitHub's documentation says to rotate an affected credential immediately.

Step 2: put the new key where only your server can read it

The server is the part of your app that runs on the service's computers, where visitors cannot look. Use your host's secrets store or server-side environment variables, which are named settings kept there.

Do not give the variable a VITE_ or NEXT_PUBLIC_ prefix. The Vite and Next.js documentation both say those are included in code sent to the browser (the program people use to open websites, such as Chrome or Safari).

If your app was calling the service straight from the browser, first move that call into a server-side function, a small piece of code that runs on the server. Otherwise the new key will leak the same way.

Step 3: look at the usage

Most providers keep request logs, a record of each call made with your key. Stripe offers request logs for each key.

  • Look for calls you do not recognise: unusual volume, strange times, unfamiliar locations.
  • If you see any, tell the provider's support.
  • For payment services, check for refunds, payouts or new customers you did not create.

If the leaked key is a Supabase key

Supabase projects have two kinds of keys, and only one of them is an emergency.

  • **The publishable key** (starting with sb_publishable_, or the anon key on older projects) is made to be in the browser. Seeing it in your public code is normal. What protects your data then is Row Level Security, the database's own rules about who may read each record, so check those rules are on for every table.
  • **The secret key** (starting with sb_secret_, or the service_role key on older projects) goes around those rules and reaches everything. Supabase's documentation says it must never reach a browser, and that a copied one still works from other tools.

For a leaked secret key, Supabase's guide on rotating leaked keys sets the order: fix the cause first, so the new key does not leak the same way. Then create a new secret key, move your app to it, and delete or deactivate the old one.

Step 4: do not bother rewriting history

Many people lose hours trying to scrub a key from Git history, the saved record of every change to your code. GitHub's documentation says this is time-intensive and often unnecessary once the credential has been revoked. A revoked key is a harmless string.

Step 5: find how it leaked

The usual routes:

  • A secret in a VITE_ or NEXT_PUBLIC_ variable.
  • A .env file (a settings file where an app keeps its keys) committed to the repository, which is the folder that holds your app's code and its history. To commit is to save a change into that history.
  • A key pasted into a chat with an AI tool or a shared document.
  • A key hard-coded in a front-end file, which is part of the code every visitor downloads.

Knowing the route matters, because replacing the key without closing the route just creates a new leak. Fix the cause before you celebrate.

Step 6: stop it happening again

  • Turn on secret scanning if your host offers it.
  • Keep .env files out of Git.
  • Give each part of your app its own key. Supabase suggests this so a leak means replacing one key.
  • Prefer restricted keys, which can do only the jobs you allow, where the service offers them, as Stripe recommends.

Stripe also describes access policies that notify you when a key is used from somewhere unexpected.

What to tell your users

If the key could have exposed customer data, tell affected people plainly and soon: what happened, what you did, what they should do. If you are not sure whether data was reached, say what you know and what you are checking.

Rules about breach notice differ by place, so ask a professional if personal data may be involved.

A worked timeline

  • Minute zero: you see the key in a public file.
  • Minute five: you open the provider dashboard and replace the key with a short grace period.
  • Minute fifteen: you add the new key to your host's secrets and put the app online again.
  • Minute thirty: the old key is turned off, and you read the request logs for the last few weeks.
  • Hour two: you search the project for other secrets and fix the call that exposed the original key.
  • Day one: you write down what happened so the next person knows.

That sequence, from notice to fix, fits inside an afternoon.

Questions people ask

Do I need to delete the repository?

No. Replacing the key makes the old one useless.

Should I make the repository private?

It can help going forward, but it does not un-leak a key that was already public, so replace it regardless.

What if the key was only a test key?

Replace it anyway. It takes a minute and keeps good habits for launch day.

What if I cannot tell whether anyone used it?

Treat it as used, check logs, and watch your bills and usage closely for a few weeks.

Keeping your head

A leaked key feels like an emergency because it is one, but it is also one of the most fixable problems in app security. The steps are short and the tools are in your dashboards.

People who handle it well do three things in order: they replace the key first, they investigate second, and they write down what they learned third. If you replaced it within the hour, in most cases the practical risk is small. If the key sat exposed for weeks, spend longer on the logs and be ready to tell users.

Check the rest of the app

One misplaced key often means others. Search your whole project for other secrets and look at your database rules too (the database is where your app keeps its data, and its rules say who may read each record). Finding keys in the wrong place is part of every Nordik audit.

Sources

Each source below was accessed on the date shown. Documentation changes over time, so check the source for the current wording.

  • GitHub documentation, About secret scanning, accessed 2026-11-16
  • Supabase documentation, API keys, accessed 2026-11-16
  • Supabase documentation, API keys, accessed 2026-11-29
  • Supabase documentation, Rotate leaked API keys, accessed 2026-11-29
  • Stripe documentation, API keys, accessed 2026-11-16
  • Vite documentation, Env variables and modes, accessed 2026-11-16
  • Next.js documentation, Environment variables, accessed 2026-11-16

See what your live app shows strangers

Paste the link to your app: in under a minute the free scan shows what your live app hands to any visitor, and where to look first. No sign-up, and it never signs in or changes anything.

Run a free scan

Want the code itself read? Order the $49 audit or see a sample report. Your card is held and charged only when your report is delivered. It is not a pentest: the audit is done by AI, and a second AI checks each serious finding.