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

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

  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. Step 4: do not bother rewriting history
  5. Step 5: find how it leaked
  6. Step 6: stop it happening again
  7. What to tell your users
  8. A worked timeline
  9. Questions people ask
  10. Keeping your head
  11. 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 browser is the program people use to open websites, such as Chrome or Safari.) If your app was calling the service straight from the browser, move that call into a server-side function, a small piece of code that runs on the server, first. 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, and for payment services check for refunds, payouts or new customers you did not create.

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

  • 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), and 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: 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). Nordik App Audit is an AI-performed audit of your app's code. It explains what it found in plain words and gives you a fix instruction for each finding. It is not a pentest (a test that tries to break into the running app), and like any review it can miss things. It supports every platform: any app can order, whatever it is built with. The current price and terms are on the Nordik home page. Finding keys in the wrong place is part of every audit. The free scan accepts apps from any platform and points you at where to look first. It is a starting point, not a verdict.

Sources

Each source below was read on 2026-11-16. 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
  • 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