In Next.js, environment variables are server-only unless their name starts with NEXT_PUBLIC_, in which case Next.js inlines them into the browser bundle at build time. Keep secret keys in unprefixed variables, read them only in server code such as Route Handlers and Server Components, and never add NEXT_PUBLIC_ to a secret.
Read the key from the environment
// app/api/charge/route.ts (runs on the server only)
export async function POST(request: Request) {
const apiKey = process.env.STRIPE_SECRET_KEY; // no NEXT_PUBLIC_ prefix
if (!apiKey) {
return new Response("Server misconfigured", { status: 500 });
}
// call the provider with apiKey; never return it to the client
return Response.json({ ok: true });
}Local development
Next.js loads .env, .env.local, and .env.development / .env.production files into process.env. Put real secrets in .env.local, which the default .gitignore of new projects excludes, and commit only .env.example. Variables prefixed with NEXT_PUBLIC_ are replaced with literal values in the client bundle when you run next build, so they are visible to every visitor.
Production
Set secrets in your hosting platform's encrypted environment settings, and make sure server-only variables are marked as such. Values read at build time are baked into the output, so keep secrets read at request time in server code. Check that client components do not import modules which read secrets; the data security guide describes ways to keep sensitive code server-only.
Mistakes to avoid
- Naming a secret
NEXT_PUBLIC_...: it is inlined into the JavaScript sent to browsers at build time, and changing the value later requires a rebuild. - Importing a server module that reads
process.env.SECRETinto a Client Component ("use client"), which pulls sensitive logic towards the browser bundle. - Returning an entire config object, or an upstream response containing a token, from a Route Handler or Server Action to the client.
- Committing
.env.localor.env.productionfiles with real values; only.env.exampleshould be committed.
When an AI agent works on your Next.js project
Coding agents run inside your project folder, so they can read the same .env file your app reads. BotPassword injects keys at run time instead: the agent calls the run_with intent through the MCP server and the process gets the variables it needs while the agent sees only redacted output. Planned: bpw run, a CLI form of the same.
Frequently asked questions
What does NEXT_PUBLIC_ do in Next.js?
It makes the variable available in the browser by inlining its value into the client bundle at build time. Use it only for values that are safe to publish.
How do I keep an API key private in Next.js?
Store it in a variable without the NEXT_PUBLIC_ prefix and read it only in server code such as Route Handlers, Server Components or Server Actions, then return results to the client, not the key.
Should I keep API keys in a .env file for Next.js?
A .env file that is git-ignored is fine for local development, but every process and every AI coding agent running in that folder can read it. Load secrets into the environment at run time from a password manager or secrets manager instead, so no plaintext file sits in the project.