← All posts

Which API keys are safe in your frontend?

NEXT_PUBLIC_ and VITE_ variables end up in the browser. Here's which keys can be public, which must stay on a server, and how to check your build.

Tommy Pham

Short answer: any environment variable with a public prefix (NEXT_PUBLIC_ in Next.js, VITE_ in Vite, EXPO_PUBLIC_ in Expo, PUBLIC_ in Astro) is copied into your JavaScript at build time and anyone can read it. Publishable keys are fine there. Secret keys (OpenAI, Stripe secret, Supabase service role, database URLs, webhook secrets) are not. If one ever shipped to the browser, rotate it. Deleting it from the code isn’t enough.

This is part of my production checklist for vibe-coded apps.

Why does this happen so often in AI-built apps?

When you ask an AI builder to “add GPT to my app”, the fastest path is to call the OpenAI API straight from the React component. That needs the key in the browser, so the tool puts it in a VITE_OPENAI_API_KEY variable, and it works. It also hands your key, and your bill, to everyone who opens dev tools.

The prefix exists precisely to make this explicit: frameworks only expose variables you mark public. Unprefixed variables are stripped from the browser bundle. The mistake is marking a secret public to make an error go away.

Which keys can be public and which can’t?

Key Public OK? Why
Supabase anon / sb_publishable_ Yes Designed for browsers. Protected by RLS
Supabase service role / sb_secret_ No Bypasses RLS: full database access
Firebase web config (apiKey, projectId) Yes Identifies the project. Security comes from Firebase Security Rules
Stripe publishable key (pk_live_) Yes Made for Stripe.js in the browser
Stripe secret key (sk_live_) and webhook secret (whsec_) No Can charge, refund and read customers
OpenAI, Anthropic, Gemini API keys No Anyone can spend your credits
Database connection strings (postgres://…) No Direct database access with a password inside
Resend, SendGrid, Twilio keys No Can send email and SMS as you
Google Maps browser key Yes, if restricted Lock it to your domain in Google Cloud console
Analytics IDs (GA4 G-…, PostHog project key) Yes Write-only ingestion keys

How do I check if a secret is already in my build?

Build the app locally, then search the output:

npm run build
grep -rEo "sk_live_[A-Za-z0-9]+|sk-[A-Za-z0-9_-]{20,}|service_role|whsec_[A-Za-z0-9]+|postgres(ql)?://[^\"' ]+" dist .next/static build 2>/dev/null | head

Then check the live site: open it, view source and the JavaScript files in dev tools, and search for sk_, sk-, service_role and eyJ (the start of a JWT). On a Supabase app, the only Supabase key you should find is the anon key (an eyJ… JWT) or a new sb_publishable_ key.

Also check:

  • Your hosting dashboard. Vercel, Netlify, Lovable, Bolt and Replit all have environment variable screens. Any secret with a public prefix there is in your bundle.
  • Git history. If a .env file was ever committed, treat every key in it as compromised, even if you deleted the file later.

How do I fix an exposed key?

  1. Rotate it first. Create a new key in the provider’s dashboard and revoke the old one. Removing it from code doesn’t remove it from old builds, caches or git history.
  2. Rename the variable without the public prefix: OPENAI_API_KEY, not VITE_OPENAI_API_KEY.
  3. Move the call to a server. A Next.js route handler, a Supabase or Firebase function, a Cloudflare Worker. The browser calls your endpoint, and your endpoint calls the API with the secret.
  4. Add a limit to that endpoint. Require a logged-in user, and rate-limit it, or your server endpoint becomes a free proxy to your AI credits.
  5. Set a spending cap at the provider (OpenAI and others support usage limits) as a backstop.

In StayFlow, for example, everything sensitive happens in a Cloudflare Worker behind HttpOnly cookie sessions. The browser never sees a credential it could reuse.

FAQ

Is the Firebase API key a secret?

No. Firebase’s web apiKey identifies your project and is meant to be in client code. What protects your data is Firestore and Storage Security Rules, so check those instead.

My key is in .env.local. Is that safe?

The file itself isn’t sent anywhere. What matters is the variable name: if it starts with a public prefix, its value is baked into the browser bundle at build time.

Can I hide a key with obfuscation or base64?

No. If the browser can use the key, a person can read it. The only fix is to keep it on a server.

Do I need to rotate a key that was only public for a day?

Yes. Bots scan public repos and JavaScript bundles for keys continuously, and a day is plenty of time.


If you’ve found a key in your bundle and want help moving the calls server-side, I can do that. See my work or get in touch below.

Sources

Contact

Let's work together. Say hi.

Open to freelance projects and remote roles. The easiest way to reach me is Upwork or email.

PaymentsUpworkpreferredWisePayoneer
© 2026 Tommy Pham