Your OpenAI API key is in your frontend. Anyone can spend your credits.
Last reviewed November 27, 2026 against the providers' current documentation.
The short version
Delete the leaked key, create a new one in a project used only by this app, put it on a server, and have your app call that server instead of OpenAI directly. Then turn on Enforce a hard limit for the project's monthly spend: a spend alert alone doesn't stop anything. The same applies to Gemini and other AI provider keys; for Claude keys, see Anthropic API key exposed.
Why it matters
Any key in browser code can be copied in seconds from the browser's developer tools. People actively look for AI keys because they can resell the usage or run their own projects on your bill. OpenAI's own security article warns that shipping a key inside an app to avoid running a server "makes your API key vulnerable to misuse". OpenAI says that when it detects a key on the public internet or leaked inside an app in an app store, it disables the key immediately, so a leak can also take your feature offline without warning.
Fix it, in this order
Delete the leaked key
On the OpenAI platform's API keys page, delete the key that was in your frontend. OpenAI's advice for a compromised key is to delete it and contact them through the Help Center chat. Removing it from your code isn't enough: old copies of your JavaScript stay in browser caches, CDN caches and archives, and the key works until it's deleted.Create a new key in a project for this app, with only the access it needs
Create the new key in a project used only by this app, so its spend, limits and keys are separate from everything else. Under the key's permissions choose Restricted and give access only to the endpoints your feature calls; OpenAI's options are All (the default), Restricted (None, Read or Write per endpoint) and Read Only. For a live app, OpenAI recommends a service account key, because a normal project key is tied to one person's account and is disabled if that person leaves the project. In the project's Limits → Model Usage, allow only the models your app uses.Add a small server endpoint
Create one server-side route (a Next.js Route Handler, a Supabase Edge Function, a Cloudflare Worker) that receives the user's request, calls OpenAI with the key from a server-only environment variable such asOPENAI_API_KEY, and returns only the result. Keep the model name, token limit and system prompt on the server, so the browser can't change them.Protect that endpoint
Require a logged-in user, limit how many requests each user can make per minute, and only accept the inputs your app actually uses. Otherwise your new endpoint becomes a free AI proxy for anyone who finds it, which costs you the same as a leaked key.Turn on a hard spend limit
In Project settings → Limits, under Spend, choose Edit spend limit, set a monthly amount and switch on Enforce a hard limit (do the same for the organization on the Organization limits page). Per OpenAI's docs, spend alerts do not enforce a cap; with the hard limit on, requests return a429error withproject_spend_limit_exceededonce it's reached. Enforcement isn't instant, so recorded spend can go slightly over.Switch the app over and check your usage
Point the frontend at your endpoint, remove the key from every client file and from anyVITE_orNEXT_PUBLIC_variable, deploy, and confirm it works. Then look at the organization's Usage page (OpenAI also supports tracking usage by API key) for spikes that don't match your app's traffic. If you see abuse, contact OpenAI support.
Or ask your AI tool to do it
Paste this into Lovable, Bolt, Cursor, Claude Code or whichever tool built your app. Review what it changes before you deploy.
My OpenAI API key is exposed in frontend code. Create a server-side endpoint that calls the OpenAI API using a key read from a server-only environment variable OPENAI_API_KEY. Keep the model, the token limit and the system prompt on the server. The endpoint must require an authenticated user, rate-limit each user, accept only the specific inputs the app needs, and return only the text the UI shows. Update the frontend to call this endpoint instead of api.openai.com, and remove the key from all client code and from any VITE_ or NEXT_PUBLIC_ variables. Finally, remind me to delete the old key on the OpenAI API keys page, create the new key with Restricted permissions in a project for this app, and turn on Enforce a hard limit for the project's monthly spend.
How this happens
Demo code often calls the AI API straight from the page because it's the fastest way to see a result. Or the key goes into a VITE_ or NEXT_PUBLIC_ variable because a plain one came out empty in the browser. When the demo becomes the product, the key goes live with it.
Check that it's really fixed
After you deploy, run the free check. It reads your live site the way an attacker would and tells you if any secret key is still visible.
Topic check: Exposed API key checker
Find the other leaks before someone else does
The free check reads your home page and its scripts. The website security report goes through up to 30 pages and 40 JavaScript files, plus .env and .git files, security headers, cookies and CORS, domain and email records, and libraries with known vulnerabilities. Every finding comes with the fix for your host and a prompt for your AI tool.
$19 once, including 20 runs over 30 days so you can fix things and check again.

Questions
I put the key in a .env file. Isn't that hidden?
Not if the variable name starts with VITE_, NEXT_PUBLIC_ or similar. Those prefixes tell the build tool to copy the value into the browser code.
Can I just obfuscate the key?
No. If the browser can use it, anyone can extract it.
OpenAI disabled my key. Was it leaked?
Probably. OpenAI says it disables a key immediately when it detects it on the public internet or leaked inside an app in an app store. Don't just paste the new key back where the old one was: move the calls to a server first, or the new key will be found the same way.
Is a spend limit enough on its own?
No, but it caps the damage. With Enforce a hard limit on, OpenAI blocks requests once the monthly amount is reached, so someone using a leaked key can still use up your budget and take your feature offline until next month. A spend alert without the hard limit only sends a notification.
How does your check spot an OpenAI key?
Our free check looks for OpenAI secret key patterns (keys starting with sk-, including sk-proj-, sk-svcacct- and sk-admin-) in your page and the JavaScript it loads, and flags them as critical. It shows only the start of the key and never tries to use it.
Sources
- OpenAI API docs: Spend limits (alerts vs. hard limits)
- OpenAI Help: Managing projects in the API platform (key permissions, service accounts, model usage)
- OpenAI Help: Keeping your OpenAI account secure (leaked keys are disabled, what to do)
- OpenAI API docs: Production best practices
- OpenAI Help: Best practices for API key safety
- Vite: Env variables and modes
- Next.js: Environment variables (NEXT_PUBLIC_)