Is NEXT_PUBLIC_ or VITE_ safe for API keys? Which env vars end up in the browser
· PlugLeak
No. Next.js inlines NEXT_PUBLIC_ into the browser bundle at build time, and Vite bundles VITE_ onto import.meta.env. What is safe to expose, and how to keep secrets on the server.
No. Not for secrets. Next.js inlines every NEXT_PUBLIC_ variable into the JavaScript bundle sent to the browser, at build time. Variables without that prefix stay in the Node.js environment and are not available to the browser (Environment variables). By default, Next.js keeps environment variables on the server and exposes only the NEXT_PUBLIC_ ones to the client (Data security).
Vite exposes variables prefixed with VITE_ on import.meta.env in client code. Vite's docs say VITE_* variables should not contain sensitive information such as API keys, because the values are bundled into your source at build time (Env variables and modes). In the example on that page, import.meta.env.VITE_SOME_KEY is the string from the env file, and import.meta.env.DB_PASSWORD is undefined.
Both prefixes freeze the value when you build. Next.js says that after the build, the app no longer responds to changes to NEXT_PUBLIC_ variables: they stay at the value from the environment where next build ran. Vite statically replaces import.meta.env at build time. Changing the host's dashboard later does not change a bundle you already shipped. You rebuild.
Which apps use which prefix
Lovable and Bolt projects are typically Vite, so the public prefix is VITE_. Cursor and v0 projects are typically Next.js, so the public prefix is NEXT_PUBLIC_. Confirm the repo in front of you. A vite.config file and import.meta.env means Vite. A next.config file and process.env.NEXT_PUBLIC_ means Next.js. The rule belongs to the bundler, not the builder. The Bolt Supabase API key guide is the case where the visible value is a Supabase key and you still have to tell a publishable key from a secret.
What can sit in a public variable
| Value | In NEXT_PUBLIC_ or VITE_ |
|---|---|
| Site URL | OK |
Stripe publishable key (pk_, including pk_live_ and pk_test_). Stripe says only publishable keys are safe in front-end code (API keys). | OK |
Supabase project URL, publishable key (sb_publishable_), or legacy anon key. Supabase marks the URL and publishable key safe to expose, and says the publishable key only reaches what Row Level Security allows (API keys). The key will still be visible. It is safe only because RLS protects the data. The RLS checklist is how to check that. | OK only with RLS |
| OpenAI or Anthropic API keys | Never |
Stripe secret keys (sk_, including sk_live_ and sk_test_) and restricted keys (rk_) | Never |
Supabase secret key (sb_secret_) or legacy service_role key. Either one bypasses Row Level Security. The service_role guide is the removal and rotation. | Never |
Database URLs and passwords. Next.js's own env example reads DB_HOST, DB_USER, and DB_PASS from a Route Handler, with no public prefix. | Never |
GitHub tokens (ghp_, gho_, ghu_, ghs_, ghr_, github_pat_) | Never |
AWS keys (access key ids start with AKIA or ASIA) | Never |
Private keys (-----BEGIN … PRIVATE KEY-----) | Never |
The safe pattern
Keep the secret in a name with no public prefix, and read it only from code that does not ship to the browser. In Next.js that is a Route Handler or a Server Action. Server Components can read server environment variables. Client Components must not, and passing a secret through to a Client Component puts it in the payload the browser receives (Data security). Supabase's env example uses NEXT_PUBLIC_ for the URL and the publishable key, and an unprefixed SUPABASE_SECRET_KEY for the server, and says never to prefix the secret or the bundler will ship it.
# .env
NEXT_PUBLIC_SUPABASE_URL=https://your-project.supabase.co
NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=sb_publishable_...
SUPABASE_URL=https://your-project.supabase.co
SUPABASE_SECRET_KEY=sb_secret_...
OPENAI_API_KEY=sk-...A Route Handler. The browser calls your URL. It never receives the key.
// app/api/complete/route.ts
export async function POST(request: Request) {
const prompt = await request.text();
const res = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.OPENAI_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: prompt }],
}),
});
return new Response(await res.text(), { status: res.status });
}The same read works in a Server Action. The directive keeps the function on the server:
"use server";
export async function complete(prompt: string) {
const res = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + process.env.OPENAI_API_KEY,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: prompt }],
}),
});
return res.json();
}That route or action is still a public URL until you check who is calling it. The prefix only keeps the key out of the bundle. It does not decide who may spend it.
A Vite app has no server of its own. Vite's docs say to use a backend server or a serverless/edge function for secrets. A Supabase Edge Function is one such place. The browser keeps the publishable key. The function reads the secret from its own environment, not from import.meta.env.
// src/lib/supabase.ts — this file is in the browser bundle
import { createClient } from "@supabase/supabase-js";
export const supabase = createClient(
import.meta.env.VITE_SUPABASE_URL,
import.meta.env.VITE_SUPABASE_PUBLISHABLE_KEY,
);// supabase/functions/complete/index.ts — not part of the Vite build
Deno.serve(async (req) => {
const key = Deno.env.get("OPENAI_API_KEY");
const prompt = await req.text();
const res = await fetch("https://api.openai.com/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer " + key,
"Content-Type": "application/json",
},
body: JSON.stringify({
messages: [{ role: "user", content: prompt }],
}),
});
return new Response(await res.text(), { status: res.status });
});Do not give that function secret a VITE_ name. An unprefixed name in a Vite .env stays out of the client bundle, and it also stays unused until some server reads it.
How to check your own live app
Open the deployed site. In DevTools, use Sources and search across the loaded files, or open a script in Network and search the response. Look for these prefixes:
sk-andsk_live_(alsosk_test_andrk_live_, which are Stripe secrets too)sb_secret_service_roleAKIAghp_-----BEGIN
A Stripe publishable key (pk_live_ or pk_test_), a Supabase publishable key, or a JWT whose role is anon can be there on purpose. A hit on the list above is a secret in the bundle. Check lazy-loaded chunks, not only the first file. The broader pass is the 10-minute self-check.
Renaming the variable does not remove a value that is already deployed. NEXT_PUBLIC_OPENAI_API_KEY renamed to OPENAI_API_KEY changes the next build. The previous string stays in the JavaScript that is live until you rebuild and redeploy. Then rotate any key that was ever shipped. Old caches and copies already downloaded can still hold it.
If a secret leaked
- Rotate it in the provider's dashboard first. The value is already in a bundle someone can save. Deleting the line from source does not revoke it.
- Put the new key in a non-prefixed variable and read it only from server code, as above. Do not put the replacement back into
NEXT_PUBLIC_orVITE_, or the new key ships the same way. - Rebuild and redeploy, then search the live JavaScript again.
Scan the live URL
The free scan is at https://plugleak.com/#scan. Every request is a GET. It fetches the page you submit and same-site JavaScript, and it does not start another batch of scripts once 25 files or 12 MB of JavaScript have been fetched. It requests /.env and /.git/config once each, and it checks security headers and the TLS certificate. In the HTML and those scripts it looks for Stripe secret keys (sk_live_, sk_test_) and restricted keys (rk_live_), OpenAI-style keys, Anthropic keys, AWS access key IDs, GitHub tokens, PEM private keys, sb_secret_ keys, and JWTs with role service_role.
It does not test Row Level Security or grants, query the database, log in, or crawl. A clean scan does not tell you whether the publishable or anon key is safe. That answer is the RLS checklist. How the free scan works lists every request. Paid fix reports include a full refund within 30 days of purchase, no questions asked (refund policy).
FAQ
Is NEXT_PUBLIC_ safe for API keys?
No, not for secrets. Next.js inlines every NEXT_PUBLIC_ variable into the JavaScript bundle sent to the browser, at build time. Use that prefix for a site URL, a Stripe publishable key (pk_), or a Supabase URL plus a publishable or anon key. Do not use it for an OpenAI or Anthropic key, a Stripe secret (sk_) or restricted (rk_) key, a Supabase secret (sb_secret_) or service_role key, a database URL or password, a GitHub token, an AWS key, or a private key.
Are VITE_ environment variables secret?
No. Vite exposes variables prefixed with VITE_ on import.meta.env in client code and bundles those values at build time. Vite's documentation says VITE_* variables should not contain sensitive information such as API keys. A variable without the VITE_ prefix is not exposed on import.meta.env. A Vite app with no backend still cannot use that unprefixed value, because nothing in the browser can read it.
Is the Supabase anon key safe in VITE_?
The legacy anon key, and the newer publishable key (sb_publishable_), are meant to be in the browser, including in a VITE_ or NEXT_PUBLIC_ variable. Supabase says a publishable key is safe to expose because it only reaches what Row Level Security allows. That is the only reason it is safe. A secret key (sb_secret_) or a legacy JWT whose role is service_role bypasses Row Level Security and does not belong in either prefix. The checks for policies are on /guides/supabase-rls-checklist.
Can I hide a key with obfuscation or by renaming the variable?
No. Base64, splitting the string, or a light encode still leaves the value in the file the browser downloads. The app has to recover it to use it. Renaming NEXT_PUBLIC_OPENAI_API_KEY to OPENAI_API_KEY, or VITE_STRIPE_SECRET_KEY to STRIPE_SECRET_KEY, does not remove a value that is already deployed. Rebuild, redeploy, and rotate any key that was ever shipped.
Does adding .env to .gitignore protect a NEXT_PUBLIC_ or VITE_ key?
No. .gitignore stops git from committing the file. Next.js still inlines NEXT_PUBLIC_ values, and Vite still bundles VITE_ values, into the JavaScript you deploy. Setting the same name in a host's environment screen does the same thing when the name has the public prefix. The ignore file also does not pull a key out of a bundle that is already live.
Sources
Check your app now — free, passive, about 20 seconds.
Same scanner as the homepage scan. Paste the live URL. It reads what a visitor already downloads. It does not log in or query your database.
Related guides
- GitHub token in the frontend
- Stripe secret key in the frontend
- CheckVibe alternatives
- OpenAI API key in the frontend
- Claude Code settings.local.json keys
- Bolt environment variables vs Secrets
- v0 app security checklist
- Cursor app security checklist
- Lovable app data exposure check
- Supabase 42501 permission denied
- Supabase RLS checklist
- Bolt Supabase API key visible
- Supabase service_role key exposed
- Lovable app security check
- Is my vibe-coded app secure?
Scan by builder