Skip to content

All posts

I leaked an API key in my frontend: what to do, in order

Found a secret key in your app's browser code or on GitHub? Revoke it first, then rotate, remove it from the frontend and check for abuse. Step-by-step.

5 min readapi keys, security, openai, stripe

Short answer: treat the key as stolen. Revoke it right now, create a new one, move the code that uses it to your server, then check your usage for anything you do not recognise. Do these in that order. Rotating and cleaning up code is pointless while the old key still works.

This guide covers the common case: you built an app with an AI tool, and a key for OpenAI, Stripe, Supabase, SendGrid or similar ended up in code that runs in the visitor's browser, or in a public code repository.

First, is it actually secret?

Not every key is a problem. Keys come in two kinds.

  • Public keys are made to be seen: Stripe keys starting pk_, a Supabase anon or publishable key, a Google Maps key you have restricted by website. These are fine in the browser, provided the rules behind them are correct.
  • Secret keys can spend money or read private data: Stripe sk_ and rk_, OpenAI sk-, the Supabase service_role or secret key, SendGrid SG., AWS AKIA, and database connection strings with a password in them. These must never reach the browser.

If yours is a secret key, carry on.

Step 1: revoke the key (first 10 minutes)

Do this before anything else. Deleting the file or fixing the code does not help, because anyone who already copied the key can keep using it.

  • OpenAI: delete the key in the API keys page of your account. Create a new one afterwards. OpenAI may disable keys it finds on the public internet, but do not rely on that.
  • Stripe: in the API keys page, roll the secret key. Stripe lets you set a short expiry on the old one so you can switch over without downtime.
  • Supabase: in the project's API key settings, create a new secret key and delete the old one. On older projects that still use the legacy keys, replacing the secret may involve the project's JWT secret, which affects every key, so read the dashboard notes before you click.
  • SendGrid, Resend, Mailgun and others: delete the key and make a new one.
  • GitHub or other code tokens: revoke the token in your account settings.
  • Database passwords: reset the password and update the connection string.

Store the new key straight into your hosting provider's secret settings. Most dashboards show a key only once.

Step 2: remove it from the frontend (next 30 minutes)

A new key placed in the same spot will leak the same way. The browser should never hold a secret.

  1. Find where the key is used. Search your project for the key's prefix, such as sk_, sk-, service_role.
  2. Move the call to the server: a server function, an API route or a Supabase Edge Function. The page asks your server; your server holds the key and calls the other service.
  3. Check the setting name. In Vite and Next.js, names starting VITE_ or NEXT_PUBLIC_ are copied into the browser on purpose. A secret stored under one of those names is public. Rename it and set it only on the server.
  4. Redeploy, then open the live site's source and search it again for the prefix to confirm it is gone.

Step 3: check whether anyone used it

Look at the provider's usage or logs for the whole time the key was exposed.

  • Spikes in requests or spending.
  • Models, endpoints or locations you never use.
  • On Stripe, charges, refunds or new customers you did not create.
  • On your database, rows or users you do not recognise.

If you find misuse, contact the provider's support with the dates and details. Many will review charges.

Step 4: do you need to clean the git history?

If the key was committed to a public repository, deleting the file leaves it in the history. Once the key has been revoked, the old value no longer works, so rewriting history is usually not needed, which is the position GitHub's own guidance takes. If you want the repository tidy anyway, you can rewrite history, but treat that as housekeeping, not as the fix.

Step 5: stop it happening again

  • Set a spending limit and an alert on any pay-per-use service.
  • Use a separate key for each project, with the narrowest permissions the key allows.
  • Turn on secret scanning in your code host if it offers it.
  • Keep a short list of every key your app uses and where it lives.
  • Before each release, search the built JavaScript for sk_, sk-, service_role and Bearer.

Ask your AI tool

"Find every secret key, token and connection string in this project. For each one, tell me where it is used, move any call that needs it into server-only code, read it from a private environment variable, and give me the list of keys I must rotate. Do not print the key values."

FAQ

Is a Supabase anon key a secret?

No. It is meant to be public, but it is only safe if row-level security is on for every table. The service_role key is the one that must stay private.

Can I just delete the commit?

Deleting a commit does not remove copies that already exist. Revoke the key.

How fast do leaked keys get abused?

Automated scanners look for exposed keys constantly, so assume minutes to hours, not days.

Will my provider refund charges?

Sometimes. Contact them quickly with the usage logs. Do not assume it.

Want us to look?

Exposed secrets are one of the first things our audit checks, in your frontend code and on the server side. Send us a link, or read your Stripe key is sitting in the browser for how it happens.

Can we watch how you use this site?

We use Microsoft Clarity to see where visitors click and scroll, so we can fix confusing pages. It sets cookies and records your visit. Nothing loads unless you say yes.

Read the privacy notice