Vibe Code Guardian
← All posts

Exposed API Keys in AI-Generated Code: How to Find Them and What to Do Next

AI coding tools are quick to wire up Stripe, OpenAI and Supabase, and slow to mention where the keys end up. Here is how to check your app for exposed API keys and clean up properly.

Aaron Allen·

A founder builds a small app with Lovable or Cursor over a weekend, adds an AI feature, and pastes an OpenAI key into the chat when the tool asks for it. The feature works. Three weeks later there is a £2,000 bill from OpenAI and no idea where the usage came from.

The usual answer is that the key was sitting somewhere public. Either it shipped to the browser inside the app's JavaScript, or it was committed to a GitHub repo, or both. AI coding tools are good at getting a third-party service connected and much less careful about where the credentials end up.

This post shows you where to look, how to tell a harmless key from a dangerous one, and the order to do things in once you find one.

Some keys are meant to be public

Not every key you find is a problem, and it helps to know which is which before you start panicking.

Services usually give you two kinds. A publishable or public key is designed to sit in the browser. Stripe's pk_live_... key is the standard example, and so is the Supabase anon key. On their own they can't do much damage.

A secret key can act as you. Stripe's sk_live_..., an OpenAI sk-... key, a Supabase service_role key, a SendGrid or Resend key, a database connection string with a password in it. Anyone holding one can spend your money, read your data or send email from your domain.

A simple rule: if the service's own docs say "never expose this in client-side code", it is a secret. If you aren't sure, assume secret.

How keys end up in the browser

Most front-end frameworks have a convention for environment variables that are allowed to reach the browser. In Vite apps it is the VITE_ prefix. In Next.js it is NEXT_PUBLIC_. Anything with that prefix gets baked into the JavaScript that every visitor downloads.

AI tools know this convention, and they sometimes use it for the wrong thing. You ask for an AI chat feature, the tool writes a call to the OpenAI API directly from a React component, and to make it work it stores the key as VITE_OPENAI_API_KEY. The app runs perfectly. The key is now readable by anyone who opens their browser's developer tools.

The other common route is simpler. The tool writes the key straight into a source file as a string, because that was the fastest way to make your error message go away.

Check your live app first

This takes five minutes and needs no coding.

  1. Open your deployed app in Chrome. Press F12 to open developer tools and go to the Sources tab.

  2. Press Ctrl+Shift+F (Cmd+Option+F on a Mac) to search across all loaded files.

  3. Search for the beginnings of keys you know you use: sk_live, sk-, service_role, Bearer , api_key, secret. Search for the name of every service you've connected too.

  4. Open the Network tab, reload the page, and click through a few requests. Look at the request headers. If an Authorization header holds a secret key, it is being sent from the browser, which means the browser has it.

Expect noise. Public keys and harmless strings will match. What you are looking for is anything that looks like a secret from the list above.

Then check the repo and its history

If the project lives on GitHub, even in a private repo, look at two things.

Search the code for the same strings. Then check whether a .env file was ever committed. AI tools sometimes create one and forget to list it in .gitignore.

The part people miss is history. Deleting a key from a file and committing the change does not remove it from the repo. Every earlier version is still there, and anyone with access to the repo (or to a public repo, anyone at all) can read it. Automated scanners watch public repos for fresh keys and, by most accounts, can find them within minutes of a push.

A free tool called gitleaks will scan the whole history for you:

gitleaks detect --source . --verbose

TruffleHog does a similar job. If you can't run either, ask a developer friend to, because it only takes a couple of minutes for them.

If you find a real secret, do these in order

Rotating the key comes first, and it comes before any cleanup. This catches people out. They delete the key from the code, feel better, and leave the old key working.

Go to the service's dashboard, create a new key, and revoke the old one. Once the old key is revoked, it doesn't matter who copied it.

Next, check the usage logs for that service. OpenAI, Stripe, Twilio and most others show recent activity per key. Look for requests you don't recognise, odd countries, or a spike in volume. If the key touched customer data, such as a Supabase service role key or a database password, treat it as a possible data breach and note the dates. If you have UK or EU users, whether you need to tell anyone is a question for a professional, but a clear timeline of when the key was exposed makes that conversation much easier.

Then move the new key where it belongs. Secret keys should only exist on a server: in your hosting provider's environment variable settings, read by backend code that the browser never sees. The browser should call your own backend endpoint, and that endpoint calls OpenAI or Stripe. For the chat example above, that means a small server function sits between your React component and the OpenAI API.

Finally, stop it happening again. Add .env and .env.local to .gitignore. Switch on GitHub's secret scanning and push protection if your plan includes them, so a commit containing a recognisable key is blocked before it lands.

A warning about asking the AI to fix it

You can paste "my API key is exposed, fix it" into your coding tool, and it will often make a change. Check what it actually did. A common result is that the key moves from a source file into a VITE_ or NEXT_PUBLIC_ variable, which looks tidier and is still public. Search your live app again after every fix, using the same steps as above.

The same goes for keys you've pasted into the chat itself. Depending on the tool, that text may be stored in your project history or sent to a third party, so rotate any key you've shared that way.

What an outside check adds

If you'd rather have someone else look, Vibe Code Guardian offers a free surface check that reviews what is publicly visible on a domain you own, and a launch-readiness audit where a developer goes through your app point by point and hands you a ranked list of fixes. The free check is not a full security audit, so it is a starting point rather than a clean bill of health.

If you do only one thing today, run the developer tools search on your live app and look for the name of every paid service you use. If a secret turns up, rotate it before you do anything else.

Want a developer to look at your app?

Built your app with AI tools? Run the free surface check yourself in a few minutes. Nothing to buy, and we never look at your app unless you run it.