Thomas Osayi

November 22, 2026 · 8 min read · Thomas Osayi

API key exposed in your frontend? Which keys are safe and how to fix the rest

Which keys are public by design (Firebase config, Supabase publishable key), which are secret (OpenAI, Stripe secret keys), and how to rotate them and move calls.

Someone runs a security scanner on your app, or a friend opens the browser dev tools, and the message comes back: "Your API key is exposed in the frontend." Sometimes that is a real emergency that could cost you thousands of dollars by morning. Sometimes it is a false alarm, because the key was designed to be public all along.

The difference depends entirely on which key it is. Below I sort the common keys in AI-built apps into public and secret, explain why, and walk through what to do in the next hour if a secret one leaked.

Is an API key in my frontend always a security problem?

No; a key in the browser is only a problem if the key grants access on its own, without rules on the server deciding what each user may do.

Everything your frontend uses ends up in the JavaScript that every visitor downloads. Open any site, press F12, look at the Sources or Network tab, and you can read it. Minifying does not hide anything, and neither does putting the key in an environment variable if that variable is bundled into the client.

That is why some services issue keys specifically for the browser. These public keys identify your project, and the real protection lives elsewhere: Firebase Security Rules, Supabase row level security, or Stripe refusing to let a publishable key create charges. Other services, like OpenAI, issue only secret keys. Anyone holding one can make requests that are billed to your account.

So the question is never "is the key visible?" It is "what can someone do with this key, and what stops them?"

Which keys are public by design?

Firebase web config, the Supabase publishable or anon key, and the Stripe publishable key are designed to sit in frontend code.

Firebase config

The apiKey in your firebaseConfig object is fine in the browser. Google's Firebase documentation says Firebase API keys do not need to be treated as secrets when they are restricted to Firebase services, because access is controlled by Security Rules, App Check and IAM. Google still calls restricting those keys to the APIs you use critical, and it warns that keys for non-Firebase services, and especially Gemini API keys, must be separate and kept out of client code.

Supabase publishable or anon key

Supabase's docs describe the publishable key (starting sb_publishable_) as safe to expose in web pages and source code. The legacy anon key, a long JWT starting with eyJ, is its low-privilege predecessor. Both are only safe if row level security is enabled on every table. With RLS off, the anon key can read and write everything, which is the leak I describe in "Is your Lovable or Bolt app leaking data? Check Supabase row level security".

Stripe publishable key

Stripe keys starting pk_test_ or pk_live_ are meant for Stripe.js and Elements. Stripe's documentation says they can create tokens and payment methods but cannot create charges or read account data.

Which keys must never be in frontend code?

Any key that acts with full power by itself: OpenAI and other AI provider keys, Stripe secret and restricted keys, the Supabase secret or service_role key, email and SMS provider keys, and database connection strings.

The common offenders I look for in AI-built apps:

  • OpenAI, Anthropic, Gemini and similar AI API keys. These are billed per use with no per-user rules, so a leaked key can be drained by anyone.
  • Stripe secret keys (sk_live_, sk_test_) and restricted keys (rk_live_). With these, someone can issue refunds, read customer data or create charges.
  • The Supabase secret key (sb_secret_) or legacy service_role key. These bypass row level security completely. Supabase now returns 401 Unauthorized if a secret key is used from a browser, but that does not stop someone using it from a script, and the docs say to rotate any leaked key.
  • Resend, SendGrid, Twilio and similar keys that send messages as you.
  • Postgres connection strings or Prisma DATABASE_URL values.
  • Admin credentials such as a Firebase service account JSON file.

If any of these appear in a file under src, components or app that runs in the browser, treat it as leaked.

How do I check whether my app exposes a secret key?

Search your repository for key prefixes and public environment variable names, then search the built site the way an outsider would.

Start in the code. Search the repo for these strings: sk_live, sk_test, rk_live, sb_secret, service_role, sk-proj, sk-ant, OPENAI_API_KEY and DATABASE_URL. Then look at every environment variable with a public prefix. In Next.js, anything starting NEXT_PUBLIC_ is bundled into the browser. In Vite, which most Lovable and Bolt projects use, anything starting VITE_ is. A variable named VITE_OPENAI_API_KEY is a leak by definition.

Next, check the live site. Open dev tools, go to the Network tab, use the app, and look at outgoing requests. If you see your browser calling api.openai.com or api.stripe.com directly with an Authorization header, the secret key is in the bundle.

Finally, check Git history. A key removed from the code last week still sits in older commits. If the repository was ever public, assume it was scraped.

What should I do in the first hour after a secret key leaks?

Rotate or revoke the key at the provider first, then check for misuse, then fix the code.

The order matters. Deleting the key from your code does nothing to the copies that already exist. Here is the sequence I follow:

  • Revoke or rotate the key in the provider dashboard. In Stripe, open the API keys page, use the overflow menu on the key and choose "Rotate key", and pick an expiration of "Now" if the key is compromised. Stripe's docs note that rotation otherwise lets the old key keep working for up to 7 days, which is useful for planned rotations but not for a leak. In Supabase, create a new secret key and delete the old one, or disable the legacy keys under Settings, API Keys.
  • Check usage and logs for activity you did not cause: AI usage spikes, Stripe refunds or new API requests in Workbench request logs, unexpected database writes.
  • Set spending limits and alerts on usage-billed services while you work.
  • Put the new key in your host's environment variables only, never in code.
  • Deploy the code fix described below, then confirm the old key is dead by checking that requests with it fail.

If customer data could have been read, think about whether you have an obligation to notify anyone. That depends on where you and your users are, and this is general information, not legal advice.

How do I move API calls to the server?

Create a server function that holds the secret key, checks who is calling, and does the work; the frontend calls your function instead of the provider.

Where that function lives depends on your stack:

  • Next.js: a route handler in app/api/chat/route.ts or a server action, reading process.env.OPENAI_API_KEY with no NEXT_PUBLIC_ prefix.
  • Supabase (most Lovable apps): an edge function in supabase/functions, with the key saved through Supabase secrets.
  • Firebase: a Cloud Function with the key stored in Secret Manager.

Two rules make the function safe. First, verify the user. Read the session or ID token, reject anonymous calls, and know whose request it is. Second, limit what it does. An endpoint that forwards any prompt to OpenAI is just your key with extra steps; an endpoint that only summarizes the signed-in user's own notes, with a length limit and a per-user rate limit, is much harder to abuse.

Here is an illustration with a made-up app. A meal-planning app built in Lovable calls OpenAI straight from the browser using a VITE_OPENAI_API_KEY. The fix: revoke the key, move the call into a Supabase edge function named generate-plan that checks the user's JWT, caps requests at 20 a day per user, and reads the new key from Supabase secrets. The frontend changes one line, from calling OpenAI to calling the function.

How do GitHub secret scanning and push protection help?

They catch many known key formats before or after they reach GitHub, but they are a safety net, not a strategy.

According to GitHub's documentation at the time of writing, push protection for user accounts is on by default on GitHub.com and blocks you from pushing recognized secrets to public repositories. For repositories, push protection is off by default and has to be enabled by an admin in the repository's security settings. When it triggers, the push is blocked with a message explaining which secret was found, and you remove it and push again.

Turn it on for your repository now, and pay attention to any secret scanning alerts GitHub sends. Partner providers can be notified when their key format is found in a public repo, and some will revoke the key automatically. But push protection only recognizes known formats, it does not cover keys that are already in the bundle of your live site, and anyone with write access can bypass a block by giving a reason.

When should I get help?

Get help if you found a secret key in the frontend and are not sure how to move the logic server-side safely, or if customer data may have been exposed.

Rotating a key is a five-minute job. Rebuilding the feature properly, with authentication, rate limits and the right rules on the database, is where AI-built apps tend to need a developer. If an AI tool put the key in the browser once, prompting it again may put it back.

My Quick Fix starts at $60 for a single issue like moving one exposed API call into a secure server function, done in about two days. For an app with several problems, Fix Plus Features starts at $200. If you are getting ready to launch, Finish and Launch starts at $600 and includes a pass over keys, database rules and payments. "Firebase best practices before launch: the security rules checklist" covers the Firebase side in more depth.

The bottom line

A key in your frontend is fine if it is a public key backed by real rules: Firebase config with Security Rules, a Supabase publishable or anon key with row level security, or a Stripe publishable key. It is an emergency if it is a secret key such as an OpenAI key, a Stripe secret key or a Supabase service_role key. Rotate the secret key at the provider first, check for misuse, move the call into a server function that verifies the user, and turn on GitHub push protection so the next one is caught before it ships.

Questions, answered in one line

Is it safe to have my Firebase API key in frontend code?

Yes, Google says Firebase API keys restricted to Firebase services do not need to be secret, as long as Security Rules and App Check protect your data.

Is the Supabase anon key safe to expose?

The anon key and the newer publishable key are low-privilege keys meant for the browser, but they are only safe if row level security is enabled and correct on every table.

What should I do if my OpenAI API key was in my frontend?

Revoke it in the OpenAI dashboard and create a new one, move the call to a server function that reads the key from an environment variable, and check usage for charges you did not make.

Does deleting the key from my code fix the leak?

No, the old key stays in Git history and in any copy someone already took, so you must rotate or revoke it at the provider.

Can someone audit my AI-built app for exposed keys?

Thomas Osayi fixes a single issue like an exposed key from $60, and his Finish and Launch package from $600 includes a security check before launch.

Sources

About the author

Thomas Osayi is a full-stack developer in Los Angeles studying computer science at Loyola Marymount University. He founded and built SQWAD, a SaaS platform Shopify brands use to run creator programs, and builds custom Next.js websites, SaaS MVPs and fixes for AI-built apps for businesses and founders.

See his work · Services and prices

Related articles

Fix, finish and launch your AI-built app

Built your app with Lovable, Bolt, v0, Replit or Cursor and got stuck? I fix the bugs, connect auth and payments, clean up the code and deploy it to production.

See the service