Your OpenAI or Gemini API key is in the frontend: what to do in the first hour
Short answer
If an OpenAI key (sk- or sk-svcacct-) or a Gemini key used with generativelanguage.googleapis.com is in a frontend bundle, treat it as public: revoke it in the provider dashboard, turn on a hard spend limit, check usage, and ship a new build that calls the model from a server. Deleting the line from git does not remove a key browsers already downloaded. A bare AIza key used for Maps, Firebase, or reCAPTCHA is a different object — restrict it by API and referrer, and do not rotate it in a panic unless it can also call the Gemini API.
Last updated · Sekrd Research
- OpenAI: never deploy an API key in a browser or mobile app. Route requests through your own backend. Source: OpenAI’s API key safety guide.
- OpenAI service-account keys use the prefix
sk-svcacct-. Project keys are commonlysk-proj-. Both are secret. - A Gemini key is an
AIzastring sent togenerativelanguage.googleapis.com. The same shape restricted to Maps or reCAPTCHA is a browser key, not a Gemini secret.
The first hour, in order
- Revoke the key before you clean the repo. In the OpenAI dashboard, delete the leaked key. For Google, delete or regenerate the key in Google AI Studio or Cloud Console → APIs & Services → Credentials. Creating the replacement comes after the old one is dead. A key that still works will keep getting used while you edit code.
- Set a hard spend limit on the project, not only an email alert. OpenAI’s docs say spend alerts do not stop traffic, and that hard enforcement is not instantaneous, so usage can slightly exceed the cap. Set the cap anyway. On Google Cloud, billing alerts are also alerts; quota and API restrictions are what stop a call.
- Look at usage for the window the key was public. Unexpected model names, odd hours, or a spike you do not recognize is the misuse. Note it. You do not need to publish it.
- Ship a server-side call. The browser sends the user’s message to your route. The route checks a session (or a CAPTCHA, if the feature is anonymous), rate-limits, and reads the new key from a server-only variable. No
NEXT_PUBLIC_, noVITE_, nodangerouslyAllowBrowser. How those prefixes leak is why the key ends up in the bundle and which env names are public. - Rebuild and search the artifact you deploy, then deploy. Searching the repository misses a key that the bundler already inlined.
npm run build
grep -R -n -E "sk-svcacct-|sk-proj-|sk-[A-Za-z0-9]|generativelanguage" dist/ .next/ build/ 2>/dev/null
Deploy only when that search is clean. Then run a free scan of the production URL to confirm the live bundle matches what you just built.
OpenAI: which prefix means what
Anything the OpenAI dashboard shows you once, at creation, is a secret. That includes user keys, project keys (sk-proj-), and service-account keys (sk-svcacct-). Service accounts are how a workload authenticates without a personal key; they are not a publishable token. If a service-account key is in the bundle, revoke that account’s key and create a new one that lives only on the server. OpenAI also disables keys it finds on the public internet; do not wait for that.
Gemini: do not treat every AIza key as a breach
Google issues AIza keys for browser APIs. A Maps or reCAPTCHA key in JavaScript is the documented setup, and it should be restricted to those APIs and to your HTTP referrers. Firebase web config includes an AIza key on purpose; Firebase security is Security Rules, not hiding that key.
The same string becomes a billed secret when the client calls generativelanguage.googleapis.com. That is the Gemini API. Restricting the key to the Generative Language API and to your server’s IP is the containment; moving the call server-side is the fix. Sekrd’s critical check requires both the key and that hostname. A bare AIza key stays a low, tentative note so a Maps key does not look like a stolen Gemini budget. If your leaked key is unrestricted — no API restriction, no referrer restriction — restrict it or replace it even when you only intended it for Maps, because an unrestricted key can be turned against any API the project has enabled.
What not to do
- Do not only rotate the git history. The key was served to clients.
- Do not “fix” it by moving the same variable to
NEXT_PUBLIC_under a new name. - Do not leave the old key valid “until the deploy finishes.” Revoke, deploy the server route with the new key, accept a few minutes of errors.
xAI (xai-), Perplexity (pplx-), Fireworks (fw_), and ElevenLabs (xi-api-key) are the same shape of incident: revoke at the vendor, call from the server. ElevenLabs keys have no public prefix, so a finding requires the header or env name next to the value; a random hex string is not one.
A key that never touched the bundle can still be stolen from the platform that stores it. Railway’s November 10, 2026 report describes that case for AI provider keys: at most 1.85% of workspaces, about $740,000 in unauthorized spend.
Frequently asked questions
What do I do if my OpenAI API key is in the frontend bundle?
Revoke the key in the OpenAI dashboard immediately, set a hard spend limit, review usage, and redeploy so the browser calls your server and the server holds a new key. Removing the key from GitHub does not unpublish a key the site already served.
Is an AIza key in my JavaScript a leaked Gemini key?
Not by itself. Maps, Firebase, and reCAPTCHA use AIza browser keys. It is a Gemini billing key when the app sends it to generativelanguage.googleapis.com. Restrict browser keys by API and referrer, and move Gemini calls to a server.
What is an sk-svcacct- key?
An OpenAI service-account API key. It is a secret, like a project key. It does not belong in a browser bundle. If it shipped, revoke it and create a replacement that stays on the server.
Sources
Don't ship until you're sekrd
Run a free scan to find the vulnerabilities your AI missed.
Scan Your App Free