Guide 路 Environment variables 路 Updated
Is NEXT_PUBLIC_ Safe for API Keys? Which Env Vars End Up in the Browser (Next.js, Vite and Expo)
Putting a key in a .env file or in Vercel settings does not always hide it. If the variable name starts with NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_, your build tool copies the value straight into the code every user downloads. Here is what the official docs say, which keys are safe to show, and how to check your live app in 5 minutes.
1. The short answer
The word PUBLIC in NEXT_PUBLIC_ and EXPO_PUBLIC_ means what it says: the value is public to every visitor. Vite's VITE_ prefix works the same way.
- Safe in a public variable: your site URL, a Stripe publishable key (
pk_), a Supabase publishable or anon key (with Row Level Security on), or a Firebase webapiKey(with Security Rules on). - Never in a public variable: OpenAI or Anthropic keys (
sk-, see is my Anthropic API key safe?), Stripe secret keys (sk_orrk_, see which Stripe keys are safe), the Clerk Secret Key (CLERK_SECRET_KEY, see is my Clerk secret key safe?), Supabase secret orservice_rolekeys, or database passwords.
Why do AI builders get this wrong? When a frontend component tries to read process.env.OPENAI_API_KEY, it gets undefined in the browser. To make the error go away fast, an AI tool or tutorial often renames it to NEXT_PUBLIC_OPENAI_API_KEY or VITE_OPENAI_API_KEY. The feature starts working, and your secret key is now in every visitor's browser.
2. How NEXT_PUBLIC_, VITE_ and EXPO_PUBLIC_ work
Each framework uses a special prefix to decide what stays on the server and what gets pasted into client code at build time:
| Framework | Public prefix | How code reads it | What happens at build time |
|---|---|---|---|
| Next.js (v0, Cursor, Vercel) | NEXT_PUBLIC_ | process.env.NEXT_PUBLIC_* | Inlined into the browser JS bundle at build time (next build). Variables without this prefix stay on the Node.js server. |
| Vite (Lovable, Bolt, Replit, Cursor) | VITE_ | import.meta.env.VITE_* (and %VITE_*% in HTML) | Bundled into client-side code at build time. Variables without VITE_ are undefined in the browser. |
| Expo (React Native / web) | EXPO_PUBLIC_ | process.env.EXPO_PUBLIC_* | Replaced in plain text in the compiled app bundle (including EAS Build and EAS Update). |
- Next.js: Next.js docs say non-
NEXT_PUBLIC_variables are only available in the Node.js server environment. Prefixing a name withNEXT_PUBLIC_tellsnext buildto inline the value into the JavaScript bundle sent to the browser. Once built, those values are frozen in the bundle. - Vite: Vite docs say only variables starting with
VITE_are exposed onimport.meta.envin client code (and in HTML files with%VITE_...%). Vite explicitly warns thatVITE_*variables should not hold API keys, and tells you to use a backend server or serverless function for secrets. - Expo: Expo docs say the Expo CLI (and EAS Build / EAS Update) replaces
process.env.EXPO_PUBLIC_*with the value in plain text inside the compiled application, and warns never to store private keys inEXPO_PUBLIC_variables.
Sources: Next.js: Environment variables 路 Vite: Env variables and modes 路 Expo: Environment variables
3. Which keys are safe in public env vars (and which are not)
Before you add NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_ to a variable name, check this table:
| Key or value | OK with a public prefix? | Why |
|---|---|---|
| Stripe publishable key (pk_live_, pk_test_) | Yes (public prefix OK) | Made for checkout in the browser. Cannot refund or read private data. |
| Supabase URL + publishable / anon key | Yes (if RLS is on) | Meant to be public. Row Level Security on every table is what protects your data. |
| Firebase web config (apiKey, projectId) | Yes (if Security Rules are on) | Identifies your Firebase project. Firestore, Realtime DB and Storage rules protect your data. |
| OpenAI / Anthropic key (sk-, sk-proj-, sk-ant-) | No, server only | Anyone can spend your credits. Keep in a variable with NO public prefix. |
| Stripe secret or restricted key (sk_live_, rk_live_) | No, server only | Can read customer data, create charges and issue refunds. |
| Supabase secret or service_role key (sb_secret_, service_role) | No, server only | Skips Row Level Security and can read or delete every table. |
| Database URLs and passwords (DATABASE_URL, POSTGRES_URL) | No, server only | Gives direct access to your database. |
Not sure what a key in your project does? See how to check if your API key is exposed for the full list of key prefixes. Using Supabase or Firebase? Read Supabase anon vs service_role key, the Supabase RLS check or Firebase Security Rules check to make sure your database rules protect your data.
Sources: Stripe: API keys 路 Supabase: API keys 路 Firebase: API keys
4. 3 common traps with .env and Vercel
- "It is in my .env file, so it is hidden." Next.js (
create-next-app), Vite and Expo all use.gitignoreto keep.env*.local(or.env*) out of GitHub. That only stops the file from being pushed to git. When you build and publish the app, any variable in that file with a public prefix is still copied into the live JavaScript bundle. - "I marked it Secret in Vercel, so visitors cannot see it." Vercel encrypts environment variables at rest, and marking a variable as Secret makes it write-only in the Vercel dashboard so teammates cannot read it back. It does not change how Next.js or Vite builds your code. If the name starts with
NEXT_PUBLIC_orVITE_, the value is still baked into the browser bundle. - "I removed NEXT_PUBLIC_ in Vercel, so the live site is fixed." Two things are still needed. First, Vercel docs note that environment variable changes only apply to new deployments, not old ones, so you must redeploy. Second, the old key was already public, so you must rotate the key at the provider.
Sources: Next.js: Environment variables 路 Vercel: Environment variables
5. How to check your live bundle in 5 minutes
Only check apps you own or have permission to test. Run this on your live production link, not localhost.
- Open your live app and open DevTools. In Chrome, right-click the page and pick Inspect.
- Search all loaded files. Press Ctrl+Shift+F (Cmd+Option+F on a Mac). Search for
sk-,sk_live,rk_live,sb_secret_,service_roleandAKIA. - Understand why the variable name disappears. Next.js, Vite and Expo replace
process.env.NEXT_PUBLIC_...andimport.meta.env.VITE_...with the literal string value at build time. So in your live bundle you usually won't see the wordsNEXT_PUBLIC_OPENAI_KEY. You will just see"sk-proj-...". Search for the start of the key itself, not only the variable name. - Check your project settings side by side. Open your
.envfile and your host's Environment Variables page (Vercel, Netlify, Bolt, Lovable or Replit). Read every variable that starts withNEXT_PUBLIC_,VITE_orEXPO_PUBLIC_. If any one of them holds a secret, move that code to the server, redeploy, and rotate the key.
6. Copy-paste fix prompts
Paste the matching prompt into v0, Cursor, Lovable, Bolt or Replit. After you deploy the change, rotate every secret key that was ever in a public variable.
Search this Next.js project for every NEXT_PUBLIC_ environment variable. Keep NEXT_PUBLIC_ only on values meant to be public (like the app URL, Stripe pk_ key, or Supabase URL and publishable/anon key). For any secret key (OpenAI, Anthropic, Stripe sk_ or rk_, Supabase sb_secret_ or service_role, or database URLs), remove the NEXT_PUBLIC_ prefix and move every call that uses it into a Route Handler or Server Action. Add a login check inside each handler. Then list every key you renamed so I can rotate it and update my Vercel environment variables.
Find every import.meta.env.VITE_ variable and every %VITE_% tag in HTML in this project. If any VITE_ variable holds a secret API key (like OpenAI, Anthropic, Stripe secret keys, or a Supabase service_role or sb_secret_ key), move the API call into a server-side function (like a Supabase Edge Function or backend route) and store the key as a server secret without the VITE_ prefix. Update the frontend to call that server function instead. List every key you moved so I can rotate it.
Search this Expo project for every process.env.EXPO_PUBLIC_ variable. Keep EXPO_PUBLIC_ only for public config (like my backend API URL or Supabase publishable key). Move any secret API key call to my backend server or edge function so the secret key is never compiled into the app binary. List every key you moved so I can rotate it at the provider.
7. Check it for me
Want a fast outside check after you deploy? VibeCheck checks your live app the way a stranger would. Paste your link and it searches the JavaScript your homepage loads for leaked secret keys (like OpenAI, Anthropic, Stripe secret keys, AWS, GitHub and Supabase service_role keys), even when the variable name was stripped out at build time. It also checks for open Supabase tables, security headers, Google basics and broken links.
Put AWS keys in a NEXT_PUBLIC_ or VITE_ variable for S3 uploads? See Is my AWS access key safe in my frontend? for the presigned URL fix.
It only looks at what is public. It never asks for your code or login, and it masks any key it finds so the report never leaks it.
Pricing: the problems list is free (just add your email). A fix report with a copy-paste prompt for every problem is $9. Or get 10 fix reports for $29. No subscription.
No tool can promise your app is safe. VibeCheck catches common, costly mistakes so you can fix them first.
8. FAQ
Is NEXT_PUBLIC_ safe for secret API keys?
No. Next.js docs say any variable that starts with NEXT_PUBLIC_ is inlined into the JavaScript bundle sent to the browser at build time. Every visitor downloads that value. Keep secret keys in variables without the NEXT_PUBLIC_ prefix and use them only on the server.
What happens to VITE_ and EXPO_PUBLIC_ variables?
They also go to the user. Vite exposes variables starting with VITE_ in client code and says VITE_* variables should not hold API keys. Expo replaces process.env.EXPO_PUBLIC_* with the plain-text value in the compiled app and warns never to store private keys there.
Does putting a key in a .env file or Vercel settings keep it out of the browser?
Only if the variable name does not use a public prefix. A .env file keeps keys out of git when .env is in .gitignore, and Vercel encrypts environment variables at rest. If the name starts with NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_, your build tool still copies the value into the public bundle.
Which keys ARE safe in NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_?
Keys that are made to be public: a Stripe publishable key (pk_live_ or pk_test_), a Supabase URL and publishable or anon key (when Row Level Security is on), a Firebase web apiKey (when Firebase Security Rules are on), or a public analytics ID.
I removed NEXT_PUBLIC_ from my secret key. Am I safe now?
Not until you rotate the key and redeploy. If the key was ever in a public build, assume a bot or visitor copied it. Make a new key at the provider, turn off the old one, and deploy again. On Vercel, changes to environment variables only apply to new deployments.
Why is my NEXT_PUBLIC_ or EXPO_PUBLIC_ variable undefined in my app?
Next.js and Expo only inline static dot lookups like process.env.NEXT_PUBLIC_KEY or process.env.EXPO_PUBLIC_KEY. Dynamic lookups like process.env[name] or destructuring const { NEXT_PUBLIC_KEY } = process.env are not inlined. Also remember that Next.js freezes NEXT_PUBLIC_ values at build time, so you must rebuild after changing them.
More questions? See the VibeCheck FAQ. Related guides: How to check if your API key is exposed 路 I leaked my API key. What do I do now? 路 Is my v0 app secure? 路 Is my Cursor app secure? 路 Is my Lovable app safe? 路 Vibe coding security checklist.