Guide
OpenAI API key exposed in your frontend? What to do now
Updated November 11, 2026
TL;DR
If an OpenAI API key is in your site's JavaScript, it is already public. Revoke it in the OpenAI dashboard, set a project budget, and move the call to a server route that reads a server-only env var.
A NEXT_PUBLIC_ or VITE_ prefix publishes the key at build time. A budget limits the damage. It does not make a frontend key safe. The same pattern applies to Anthropic keys and Stripe secret keys.
How keys leak into the frontend
An OpenAI API key is a bearer token. Anyone who has it can call the API and spend the credit on that project. The browser is a public place to put it. Three patterns show up constantly in Lovable, Bolt, and Cursor apps:
- NEXT_PUBLIC_ and VITE_ environment variables. Next.js inlines every variable whose name starts with
NEXT_PUBLIC_into the client bundle at build time. Vite does the same forVITE_.NEXT_PUBLIC_OPENAI_API_KEYandVITE_OPENAI_API_KEYare public, even when the value lives in.env.local. The file is gitignored. The built JavaScript is not. - The bundled JavaScript. A key pasted into a component, a constant, or a client SDK constructor is in the file every visitor downloads. Search the built assets, not only the source. A source map makes the original file even easier to read.
- A client-side fetch to api.openai.com. If the page itself sends
Authorization: Bearer sk-...tohttps://api.openai.com, the key is in the browser. The official SDK flagdangerouslyAllowBrowser: trueis the same mistake with a clearer name.
A server-only name, OPENAI_API_KEY with no public prefix, stays on the server unless a client component imports it. Importing it from a file marked "use client" pulls the value into the bundle.
How to check whether yours leaked
Check the deployed site, not only your editor. The bundle is what visitors receive.
- Open the live page and use your browser's view-source. Follow the script tags to the JavaScript files and search each one for
sk-,sk-proj-, andsk-svcacct-. - Open DevTools → Network, reload, and filter for
openai. A request from the page straight toapi.openai.commeans the browser is calling OpenAI. TheAuthorizationheader on that request is the key. - In the repo, search for
NEXT_PUBLIC_andVITE_names that hold the key, and forapi.openai.comanddangerouslyAllowBrowser.
Do not paste the full key into chat, a ticket, or a screenshot while you look. Revoke it first, then talk about where it was.
What to do immediately
Editing the code does not stop a key that is already public. Do these in the OpenAI dashboard before you finish the patch. Menu names move around; the actions are the same.
- Revoke the key. Open the OpenAI platform dashboard, go to API keys, and revoke the key that appeared in the site. Rotate means the same thing here: the old value must stop working. If you create a replacement now and the next deploy still ships it to the browser, revoke that one too.
- Set a project budget and usage limits. In the project's limits or billing settings, set a cap you would actually notice. A budget does not make a frontend key safe. It stops an unlimited bill if another key leaks later.
- Review usage. Open the usage page and look for requests you did not make. Revoking the key stops further calls. It does not undo spend that already went through.
Create the replacement key only after the call site is server-side, and store it in a server-only env var. If the key was committed to a public git repo, rotating is what matters. Rewriting history does not un-spend credit, and it is easy to get wrong.
The fix: keep the key on the server
The browser should never see the key, and it should not call OpenAI itself.
- Move the OpenAI call to a server route, a server action, or an edge function. In Next.js that is a Route Handler or a Server Action. In other hosts it is the same idea: code that does not ship to the client.
- Read the key from a server-only environment variable. No
NEXT_PUBLIC_prefix, noVITE_prefix. - The browser calls your route. Your route calls
api.openai.comand returns the text the page needs. - If the feature is not meant for the whole internet, require a signed-in user. Add rate limiting, and a per-user or per-IP cap, so a bot cannot drain the budget through your own endpoint. A leaked key is one bill. An open route with a server-side key is the next one. Related: How to rate limit an OpenAI API route and stop a surprise bill.
Redeploy, then search the new JavaScript bundle for sk-. The key should be gone. Searching the source and forgetting the last deploy is how the old key stays public.
A prompt you can paste into Lovable or Cursor
Revoke the exposed key before you paste this. The prompt changes code. It cannot invalidate a key that is already public, and it should not put a new key in the frontend.
An OpenAI API key is reachable from the browser. Remove it from client components, from any NEXT_PUBLIC_ or VITE_ environment variable, and from any fetch or SDK call to api.openai.com that runs in the browser.
Move the call to a server route, server action, or edge function. Read the key from a server-only environment variable named OPENAI_API_KEY (no public prefix). The browser should call our route, not api.openai.com.
If this feature is not meant for anonymous visitors, require a signed-in user. Add rate limiting so one visitor cannot drain the account. Do not print the key, do not log it, and do not commit it.
When you are done, tell me which files used to contain the key so I can confirm it is gone from the bundle. I will revoke the old key in the OpenAI dashboard and set a usage budget. Do not create a new key in client code.The same leak: Anthropic keys and Stripe secret keys
Anthropic. Keys usually start with sk-ant-. They leak the same way: a NEXT_PUBLIC_ or VITE_ variable, a string in the bundle, or a browser request to api.anthropic.com. Revoke the key in the Anthropic console, set a spend limit if the console offers one, and move the call to a server route.
Stripe secret keys. Secret keys start with sk_live_, sk_test_, rk_live_, or rk_test_. They do not belong in frontend code. A secret key can create charges and read customer data, not just process a card you already intended. Revoke or roll it in the Stripe Dashboard under Developers → API keys, and keep the secret on the server. Publishable keys (pk_live_, pk_test_) are supposed to be in the browser. Don't rotate those by mistake, and don't treat them as proof the secret is safe.
HackOrBroke's free scan looks for these patterns in the public page and the scripts it fetches. It does not log into OpenAI, Anthropic, or Stripe, and it cannot tell you the amount that was spent. A key that only exists on a route the scan did not open will not show up. Search the bundle yourself if you have ever used a public env prefix.
Common questions
Is an OpenAI key in my JavaScript already public?
- Yes, if the key is in a file the browser downloads. Anyone who can load the site can open View Source or the JavaScript bundle and copy a key that starts with sk-. Treat it as compromised even if you have not seen strange usage yet.
Can I leave the key in the frontend if I set a budget?
- No. A project budget or usage limit caps the bill. It does not stop someone else from using the key up to that cap. Revoke the key and move the API call to a server route. The browser should call your route, not api.openai.com.
Should I rotate the key before I change the code?
- Revoke the exposed key now, so it stops working while you edit. Then move the call to a server route and store the replacement in a server-only env var, with no NEXT_PUBLIC_ or VITE_ prefix. If the new key is still in the bundle on the next deploy, revoke that one too.
Does this apply to Anthropic and Stripe?
- Yes for Anthropic API keys, which usually start with sk-ant-, and for Stripe secret keys, which start with sk_live_, sk_test_, rk_live_, or rk_test_. Revoke those and keep them on the server. Stripe publishable keys, which start with pk_, are meant to be in the frontend.
Run the free scan
Point it at a URL you own. The scan looks for key-shaped strings and for browser calls to api.openai.com or api.anthropic.com in the files it fetches. It does not log into OpenAI, and it will not see a key that never shipped to this page.
Related: How to check if Supabase RLS is on (Lovable, Bolt, Cursor apps)