Next.js Environment Variables: Server vs. Browser
Next.js environment variables separate server secrets from public browser config. Use them for API keys or database strings. The key footgun is exposing secrets by forgetting to prefix browser-accessible variables with NEXT_PUBLIC_.
Why it exists
To separate configuration from code. Hardcoding API keys, database connection strings, or other secrets is insecure and makes it difficult to manage different environments like development and production. Environment variables allow you to inject these values at build time or runtime without changing your application code.
The mental model
Think of two separate buckets for your variables. The first bucket is for the server, accessible only in backend code like API Routes or getServerSideProps. This is the default and it's secure. The second bucket is for the browser, and anything you put in it MUST be explicitly marked public by prefixing its name with NEXT_PUBLIC_.
How it works
Next.js loads variables from .env files in your project root, with .env.local overriding others for local development (and should not be committed to git). A variable like DATABASE_URL is only available on the server. To expose a variable to the browser, you must prefix it, like NEXT_PUBLIC_ANALYTICS_ID. Next.js then embeds these public variables into the client-side JavaScript bundle, making them accessible via process.env.NEXT_PUBLIC_ANALYTICS_ID.
When to use it
Use environment variables for any value that is sensitive or changes between deployment environments. This includes private API keys, database credentials, authentication secrets, and third-party service tokens. Also use them for non-secret, environment-specific settings like a public API endpoint URL that differs between development and production.
When not to use it
Do not store sensitive information in any variable prefixed with NEXT_PUBLIC_. These variables are not secure; they are fully visible to anyone who inspects your website's client-side code. If a value is static, non-sensitive, and the same across all environments, it can simply be a constant in your code.
One canonical example
A common use case is connecting to a payment provider. You would store your secret key as STRIPE_SECRET_KEY in .env.local for use in server-side API routes to process payments. You would store your publishable key as NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY to be used in your client-side React components for initializing the Stripe.js library. This pattern keeps the secret key safe on the server while exposing only the necessary public key to the browser.
Interview question
To securely handle both a public client-side API key and a private server-side API key in Next.js, how should they be defined?
- a.Define CLIENT_KEY and SERVER_KEY in .env.
- b.Define NEXT_PUBLIC_CLIENT_KEY and SERVER_KEY in .env.Correct
- c.Define CLIENT_KEY in a client-side file and SERVER_KEY in .env.
- d.Define both as NEXT_PUBLIC_CLIENT_KEY and NEXT_PUBLIC_SERVER_KEY in .env.
Why? this is the answer
Variables prefixed with NEXT_PUBLIC_ are exposed to the browser, making them suitable for public client-side keys. Variables without this prefix are server-only and secure, ideal for private server-side keys. Option D is wrong because NEXT_PUBLIC_SERVER_KEY would expose the private key to the browser.
Just read this? Test yourself on what you have been reading.
Read the original → nextjs.org
- #nextjs
- #environment-variables
- #security
- #configuration
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on nextjs — each one lists the topics its interview covers.
See open roles