# Are NEXT_PUBLIC_ environment variables secret? What leaks, and how to fix it

Canonical: https://caelo.sh/guides/next-public-env-variables-secrets
Published: 2026-11-27 · Updated: 2026-11-27 · Arthi Arumugam, Caelo by Irth Labs

**Short answer:** No. Next.js inlines every `NEXT_PUBLIC_` variable into the JavaScript it sends to the browser at build time, so anyone can read the value. Use the prefix only for public values, such as a Supabase or Stripe publishable key. Keep secrets in unprefixed variables, read them only in server code and rotate any that were ever prefixed.

## What does NEXT_PUBLIC_ actually do?

The Next.js docs say Next.js can "inline" a value, at build time, into the js bundle that is delivered to the client, and that all `NEXT_PUBLIC_` variables are frozen with the value evaluated at build time. The value is written into a JavaScript file that any visitor can open.

Variables without the prefix "are only available in the Node.js environment, meaning they aren't accessible to the browser." In a Client Component, Next.js replaces an unprefixed variable with an empty string. That is why an AI assistant, asked to fix an empty `process.env.OPENAI_API_KEY` in a Client Component, often adds `NEXT_PUBLIC_` to the name. That makes the error go away and puts the key in the bundle.

## The vulnerable pattern and the fix

Vulnerable: a secret read in a Client Component

```tsx
// app/upgrade/UpgradeButton.tsx
"use client";
import Stripe from "stripe";

const stripe = new Stripe(process.env.NEXT_PUBLIC_STRIPE_SECRET_KEY!); // in the bundle
```

Fixed: the secret lives in a server-only module

```ts
// lib/stripe.ts
import "server-only"; // importing this file from a Client Component fails the build
import Stripe from "stripe";

export const stripe = new Stripe(process.env.STRIPE_SECRET_KEY!);
```

The browser calls a route handler instead

```ts
// app/api/checkout/route.ts runs on the server
import { stripe } from "@/lib/stripe";

export async function POST() {
  const session = await stripe.checkout.sessions.create({
    mode: "subscription",
    line_items: [{ price: process.env.STRIPE_PRICE_ID!, quantity: 1 }],
    success_url: "https://example.com/thanks",
  });
  return Response.json({ url: session.url });
}
```

The Next.js docs say that once a module imports `server-only`, importing it into a Client Component causes a build-time error. That turns a silent leak into a failed build.

## Which values are fine behind the prefix?

| Value | Behind NEXT_PUBLIC_? | Why |
| --- | --- | --- |
| Supabase URL, anon key, `sb_publishable_` key | Yes | Designed for the browser. Row level security protects the data. |
| Supabase service_role or `sb_secret_` key | No | Bypasses row level security. |
| Stripe publishable key (`pk_live_...`) | Yes | Stripe says to embed publishable keys in client-side code. |
| Stripe secret or restricted key (`sk_live_`, `rk_live_`) | No | Can create charges, refunds and read customer data. |
| OpenAI, Anthropic, Resend, Twilio API keys | No | These providers have no browser-safe key. Anyone with it spends your money. |
| Firebase web config (`apiKey`, `authDomain`) | Yes | Public by design. Security rules protect the data. |
| Analytics IDs (PostHog, Sentry DSN, GA measurement ID) | Usually yes | Meant to identify your project, not grant access. |

## Fix it, step by step

1. List every public variable your code uses: `grep -rhoE "NEXT_PUBLIC_[A-Z0-9_]+" --exclude-dir=node_modules --exclude-dir=.next . | sort -u`
2. For each one, look up the value and decide from the table above whether it is publishable or secret.
3. Rename each secret one without the prefix and move the code that uses it into a route handler, server action or a module that imports `server-only`.
4. Rotate every secret that was ever built with the prefix. It is inside every bundle you have deployed since, so renaming it is not enough.
5. Update the variables on your host and redeploy. A `NEXT_PUBLIC_` value is frozen at build time, so changing it in the dashboard does nothing until the next build.
6. On Vercel, store the secrets as the Secret type. Vercel says Secret values are write-only after saving, are allowed in Development, and values of 32 characters or more are redacted in build logs.

## Check it automatically

Caelo flags a browser-prefixed variable name in your code when the name looks like a secret (`SECRET`, `SERVICE_ROLE`, `PRIVATE`, `PASSWORD`) or belongs to a provider that only has server keys (OpenAI, Anthropic, Resend, Stripe secret and others). The rule is `browser.public-secret-name`. It skips names that are public by design, such as `ANON`, `PUBLISHABLE` and `FIREBASE`, and it ignores names that only appear in documentation. A generic name such as `NEXT_PUBLIC_API_KEY` is reported as medium, because Caelo cannot tell from the name whether the key is publishable.

Caelo output for a file that uses NEXT_PUBLIC_STRIPE_SECRET_KEY

```text
- Secret-named value exposed to the browser [needs review]
    src/ai.ts:2  (browser.public-secret-name)
    value: NEXT_PUBLIC_STRIPE_SECRET_KEY
    why: NEXT_PUBLIC_STRIPE_SECRET_KEY has a browser-public prefix and a secret-sounding name. Anything behind that prefix is bundled into the JavaScript every visitor downloads.
```

- [Install Caelo: npx caelo . or the MCP server](/)
- [How Caelo compares with gitleaks, TruffleHog, Semgrep and others](/compare)

## Check your own app

1. Run the free local check: `npx caelo .` Caelo is a free, open source (MIT) command line tool and MCP server from Irth Labs that checks the files of an AI-built app for exposed API keys, Supabase and Firebase misconfigurations and `.env` files with real values. It runs on your machine, uploads nothing and uses no AI model. Each finding comes with a plain-English fix.
2. Follow the fix in the report and rerun Caelo. For an untracked, unignored file containing secrets, preview the US$19 quick fix. https://caelo.sh/fix
3. For an engineer's assessment, book the US$490 App Health Check. Irth Labs delivers a report and fixed repair quote, with credit toward the repair. https://www.irthlabs.com/start?service=app-health-check

## Questions

### Is NEXT_PUBLIC_SUPABASE_ANON_KEY safe?

Yes, as long as row level security is on for every table and the policies are narrow. The anon key is meant to be public. Our anon key guide covers how to check it.

### Does marking a variable as a Vercel Secret protect a NEXT_PUBLIC_ value?

No. The Secret type controls who can read the value in the Vercel dashboard. If the name starts with `NEXT_PUBLIC_`, Next.js still writes the value into the browser bundle at build time.

### Why is my unprefixed variable undefined in the browser?

Because Next.js only sends `NEXT_PUBLIC_` variables to the browser. That is working as intended for secrets. Read the variable in a route handler or server action and send the browser only the result.

### I removed the prefix. Am I safe now?

Not until you rotate the key. Every deployment built while the prefix was there contains the old value, and anyone who loaded your site could have copied it.

## Sources

- [Next.js docs: Environment variables (bundling for the browser).](https://nextjs.org/docs/app/guides/environment-variables)
- [Next.js docs: Server and Client Components (server-only and environment poisoning).](https://nextjs.org/docs/app/getting-started/server-and-client-components)
- [Vercel docs: Sensitive and Secret environment variables.](https://vercel.com/docs/environment-variables/sensitive-environment-variables)
- [Stripe docs: API keys best practices.](https://docs.stripe.com/keys-best-practices)
- [Supabase docs: Understanding API keys.](https://supabase.com/docs/guides/api/api-keys)
- [Caelo source: every rule in lib/rules.js.](https://github.com/arthi-arumugam-git/caelo/blob/master/lib/rules.js)

## Related guides

- [Is my v0 app secure? A check for v0 apps on Vercel](https://caelo.sh/guides/is-my-v0-app-secure)
- [I leaked an API key or the Supabase service_role key. What do I do now?](https://caelo.sh/guides/leaked-api-key-what-to-do)
- [Is it safe to expose the Supabase anon key in my frontend?](https://caelo.sh/guides/is-supabase-anon-key-safe-to-expose)
- [Are VITE_ environment variables safe for API keys?](https://caelo.sh/guides/vite-env-variables-exposed)
- [OpenAI API key exposed in client-side code: how to fix it](https://caelo.sh/guides/openai-api-key-exposed-frontend)
