API Key Exposed in My App: What to Do in the Next Hour
Found a secret key in your AI-built app's code? Here's what to do in the next hour, in order, and how to keep keys safe from now on.
By the Prototilt team
You've just found a secret key in your app's code: maybe a Stripe key, an OpenAI key, or a database key. Maybe someone told you. Maybe you found it yourself. Either way, don't panic, and don't wait. Here's what to do, in order.
First: is it really a secret key?
Not every key is a problem. Some are designed to be public:
- Stripe publishable keys start with
pk_. They're meant to be in the browser. - Supabase anon (public) keys are meant to be in the browser too, as long as Row Level Security is set up. See Supabase security for beginners.
These must stay secret:
- Stripe secret keys (they start with
sk_) and restricted keys (rk_). - Supabase service role keys.
- AI model keys (OpenAI, Anthropic and others).
- Email service keys, and anything labelled "secret" or "private".
If it's one of these and it's in code that runs in the browser, or in a public code repository, treat it as exposed. Assume someone has already copied it. Automated bots scan public code for keys within minutes.
In the next hour
1. Make a new key and cancel the old one. This is called rotating the key. In the service's dashboard (Stripe, OpenAI, Supabase…), create a new key, then delete or revoke the exposed one. Deleting is what actually protects you. Moving the key somewhere else is not enough, because the old one still works.
2. Check for damage. Look at recent activity in that service:
- Stripe: unexpected charges, refunds, payouts or new connected accounts.
- AI providers: a spike in usage or cost.
- Database: data you didn't create, or tables that changed.
If you see something wrong, contact the service's support. Most have a process for leaked keys.
3. Set a spending limit. Many AI providers let you set a monthly cap. Do it now, so a future leak can't run up a large bill.
Then: put the new key where it belongs
A secret key must only live on the server. In practice, for an AI-built app, that usually means:
- Storing it as an environment variable or secret in your hosting platform, not in a file of code.
- Making the browser ask your server to do the action (charge a card, call the AI), and the server uses the key.
A prompt for your coding tool:
"Find every API key or secret in this project. Tell me which ones are public by design and which are secret. Then help me move each secret key to a server-side environment variable and make the browser call the server instead. Show me each change, and list anything you couldn't move."
Expect this to take a few rounds, not one prompt. AI tools are good at this kind of change, but they can miss a key or break a feature along the way, so test the features that use each key after it moves. Then check for yourself: open your live app, open the browser's developer tools, and search the loaded files for sk_, secret and service_role. Nothing should come up.
If your code is on GitHub
This one surprises almost everyone. GitHub keeps every version of every file you've ever saved there. Deleting the key from your code today creates a new version without it, but all the older versions still contain it, and anyone who can see your repository can open them.
Many AI tools can sync your project to GitHub, and it's easy to forget you connected it. Check whether your app has a connected repository, and whether it's public.
So:
- Rotate the key anyway. Once a key has been in a public repository, assume it's been copied. This is the main reason rotating is the real fix.
- Make the repository private if it doesn't need to be public.
- Don't spend hours rewriting history to scrub the old key. Once it's rotated, the old key is useless.
Hiding it isn't fixing it
Exposed keys are one of the most common gaps in AI-built apps, because the tool puts the key wherever it makes the feature work. It's not a sign you did something foolish. It's a sign nobody told you where keys go.
If you remember one thing from this page: an exposed key isn't fixed when you hide it. It's fixed when you replace it.
Secret keys are one item on a longer list. See the rest in the security checklist for vibe-coded apps.
Get your full launch plan. Prototilt asks you a few questions about the app you built and shows you the whole map from "it works" to "launched": every step, in order, marked launch blocker, should do or nice to have, with the prompts to use in your coding tool.
See your plan's steps for free →By the Prototilt team. Written with AI tools and reviewed by Aline Sadate, Prototilt's founder.