I exposed an API key in my frontend. What do I do now?
Rotate the key first: create a new one, switch your server to it and revoke the old one, because anyone may have copied it while it was public. Then restrict what the new key can do and move the calls that need it to a server route that checks who is calling. Deleting the key from your code, or rewriting git history, does not undo the exposure.
Saqr (usesaqr.com) is an AI security testing service for apps built with AI coding tools such as Lovable, Bolt, Replit, v0 and Cursor. From $19 a month, cancel any time. Tests only apps you have shown are yours.
By Saqr. Last checked .
Is every key in the frontend a problem?
No. Some keys are designed to be public:
- A Supabase publishable key, which only reaches what row level security allows (Supabase).
- A Firebase API key, which Firebase says is fine to include in code (Firebase).
- A Stripe publishable key (
pk_), which Stripe says is safe to embed (Stripe).
Others never are: a Supabase secret or service_role key, a Stripe secret (sk_) or restricted (rk_) key, and an OpenAI key, which OpenAI says never to deploy in browsers or mobile apps (OpenAI). A useful rule: if the provider calls it secret, or it can spend money or read data without a user signing in, it does not belong in the browser.
Why rotate before fixing the code?
Because removing the key from the next release does not remove it from the copies already downloaded. GitHub's advice for a committed secret is the same: revoke or rotate it as the first step, and once it is rotated it no longer grants access. Rewriting history does not reach other clones, forks or cached views (GitHub Docs).
What should I do, step by step?
Six steps, in this order. The first two take minutes and stop most of the risk.
Confirm which key it is
Find the key with Search in the developer tools (Chrome DevTools) and check its prefix against the provider's documentation. If it is documented as public, there is nothing to rotate; check the rules behind it instead, such as row level security or Security Rules.
Rotate it in the provider's dashboard
Stripe: rotating revokes the key and creates a replacement, and choosing Now as the expiry deletes the old one at once (Stripe). OpenAI: rotate it from the API Keys page (OpenAI). Google Cloud: rotating creates a new key with the same restrictions; update your apps, then delete the previous key (Google Cloud). Supabase: follow its steps for a leaked secret key, and disable legacy keys separately (Supabase).
If it fails: Rotate an exposed service key and move it to the server
Restrict the new key
Give it only what it needs. Google Cloud keys take API restrictions and application restrictions such as websites or IP addresses (Google Cloud). Stripe recommends restricted keys with only the permissions your code uses, and access policies that limit where a key can be used from (Stripe). OpenAI suggests spend limits with hard enforcement (OpenAI).
Move the call to your server
Put the new key in a server-side variable without a
VITE_orNEXT_PUBLIC_prefix, and call the provider from a server route, a server action or an Edge Function. That route must check that the caller is signed in and allowed, and return only what the page needs. Next.js suggests that only a server-only data access layer readsprocess.env(Next.js data security).If it fails: Move secret keys out of the browser bundle
Check what the old key was used for
Confirm the old key is gone everywhere
Redeploy, then search the live scripts again for the old key and the new one. Search the repository too. On a public repository, GitHub's secret scanning looks through the whole history on every branch (GitHub Docs). If the key was committed, rotating it is the fix; rewriting history with
git-filter-repois optional (GitHub Docs).
How do I stop it happening again?
Keep one rule: a secret never gets a public prefix, and the browser never calls a paid API directly. Write that rule where your AI coding tool reads it on every session; the Claude Code and Cursor checklist shows how. Keep .env files out of the repository (the default Next.js template adds them to .gitignore, Next.js), and search the live scripts for key prefixes before each release.
Facts worth knowing, with sources
- OpenAI says never to deploy an API key in client-side environments such as browsers or mobile apps. Source
- Stripe says only publishable keys are safe to expose outside your application's back end. Source
- GitHub advises revoking or rotating a committed secret as the first step; rewriting history does not remove copies in clones and forks. Source
- Rotating a Google Cloud API key creates a new key with the same restrictions as the old one. Source
Questions
Is it enough to delete the key from my code?
No. Every visitor's browser already downloaded the old scripts, and anyone may have copied the key. Rotate it in the provider's dashboard first, then remove it from the code and redeploy.
My repository is private. Do I still need to rotate?
If the key reached the browser, yes: it was sent to every visitor, whatever the repository's visibility. If it was only committed to a private repository, rotating is still the safe choice when you cannot be sure who had access.
Which API keys are safe in the frontend?
Keys their provider documents as public, such as a Supabase publishable key, a Firebase API key or a Stripe publishable key. Even then, the data behind them must be protected by row level security, Security Rules or the provider's own restrictions.
Where does Saqr fit?
These checks cover what is on the list, on the pages you think to try. Saqr runs this kind of test on the live app for you, in a real browser and including signed-in pages, and shows the proof for each finding with a fix prompt for your AI coding tool. After you fix something, ask it to recheck. Plans are Starter at $19 a month and Pro at $49 a month, and you can cancel any time.
Every task ends in three PDFs. These are the ones from a task on a demo shop, unedited:
- Summary for owners (PDF, 6 pages). What was found and what to fix first.
- Technical report for developers (PDF, 29 pages). Every finding, with its proof and the steps to fix it.
- AI fix pack (PDF, 10 pages). A ready-to-paste fix prompt for every finding.
No test can promise an app is secure. A task tells you what Saqr found that day, with proof.
Which other guides should I read?
- Is my Lovable app secure?
- Is my Bolt app secure?
- Is my Replit app secure?
- Is my v0 app secure?
- Is my Cursor-built app secure?
- Is my Supabase app secure?
- Is my Firebase app secure?
- How do I check if my vibe-coded app is secure?
- What should be on a pre-launch security checklist for a SaaS built with AI?
- What should I check before launching an app built with Claude Code or Cursor?
- What is the OWASP Top 10, in plain words?
- How do I test my own app for broken access control (IDOR)?
- How do I test Supabase row level security?
- Supabase anon key vs service_role key: which one is safe in the browser?
- How do I check if a Supabase storage bucket is public?
- How do I check my Firebase security rules?
- My Firebase API key is public. Is that safe?
- How do I add and test login rate limiting in an app built with AI tools?
- Are my session cookies secure, and how do I check them?
- Which security headers should my Next.js or Vite app set?
- Is my CORS configuration safe, and how do I check it?
- Security fix prompts for your AI coding tool
- Saqr, automated scanners and a manual penetration test
- Questions about securing an app built with AI tools
To see what a finished test looks like, open the sample reports.
Sources
The statements above were checked against these pages on 24 November 2026. Products change; follow the links for the current wording.
- OpenAI Help Center: Best practices for API key safety
- Stripe documentation: API keys
- Google Cloud documentation: Manage API keys
- Supabase documentation: API keys
- Firebase documentation: API keys
- GitHub Docs: Removing sensitive data from a repository
- GitHub Docs: About secret scanning
- Next.js documentation: Data security
- Next.js documentation: Environment variables
- Chrome DevTools: Search across all loaded resources
Saqr is independent and is not affiliated with Stripe, OpenAI, Google Cloud, Supabase, Firebase, GitHub or Next.js. Each name is a trademark of its owner.