Guides · Updated Oct 15, 2026 · By Lucy at KeySnare

NEXT_PUBLIC_ and VITE_ variables: why your secret keys are public

Putting a key in an environment variable feels safe. But if the variable name starts with a public prefix, your framework copies its value into the JavaScript that every visitor downloads.

How public prefixes work

The prefix exists so you can opt in to exposing a value. It's a label meaning "this is safe for everyone to see," not a hiding place.

How to check, step by step

  1. Open every .env* file and your hosting dashboard's environment settings.
  2. List every variable with a public prefix. For each one ask: would I be OK posting this value on X?
  3. Red flags: names containing SECRET, SERVICE_ROLE, PRIVATE, or values starting with sk_, sk-, sk-ant-.
  4. Confirm on the live site: open DevTools, search all loaded scripts for part of the value.

How to fix it

# before (leaks to every visitor)
NEXT_PUBLIC_OPENAI_API_KEY=sk-proj-...
VITE_STRIPE_SECRET_KEY=sk_live_...

# after (server only)
OPENAI_API_KEY=sk-proj-...
STRIPE_SECRET_KEY=sk_live_...

Renaming alone isn't enough. Move every place that uses the key into server code (an API route, server action or edge function), because a pure Vite single-page app has no server. Then rotate the key, since the old value is already in deployed bundles.

KeySnare checks this automatically. It flags secrets stored in browser-public env variables and API keys hardcoded in source code.

Copy-paste prompt for your AI coding tool

Paste this into Cursor, Claude, Lovable, Bolt or whichever AI built your app:

Find every environment variable in this project that uses a browser-public prefix (NEXT_PUBLIC_, VITE_, REACT_APP_, EXPO_PUBLIC_). For each one, tell me if the value is safe to be public. For any secret: rename it without the prefix, move all code that uses it into server-only code (API route, server action or edge function), update my hosting env settings, and list the keys I must rotate because they were already exposed.

Check your repo in about a minute

Paste a public GitHub repo into KeySnare for a free grade and your top 3 issues, each with a copy-paste AI fix prompt.

Run a free scan →

FAQ

Are NEXT_PUBLIC_ variables secret?

No. Next.js inlines NEXT_PUBLIC_ values into the JavaScript sent to the browser, so anyone can read them.

Are VITE_ variables visible in the browser?

Yes. Vite exposes VITE_-prefixed variables to client code through import.meta.env, and they are included in the built bundle.

Can I hide a secret in a Vite single-page app?

Not in the browser code. You need a server-side function (for example a Supabase Edge Function or serverless function) to hold the secret and make the API call.

Do I need to rotate a key after removing the prefix?

Yes. The old value is already in deployed bundles and possibly cached copies, so create a new key and revoke the old one.

More guides: Leaked an API key? How to find, rotate and verify it · Is my Supabase app safe? How to check Row Level Security · How to hide API keys in Lovable, Bolt and Cursor apps · How to check your Firebase security rules before launch · Vibe-coded app security checklist: 12 checks before you launch