Lesson 14 of 30
The same code needs a different API URL in development, staging, and production — environment variables are how that value moves from 'hardcoded and wrong somewhere' to 'injected at build or run time.'
11 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 2
A secret database password is stored in an env var named `NEXT_PUBLIC_DB_PASSWORD`. Is this safe?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
An environment variable is a value supplied outside your source code — by the shell, a .env file, or your hosting platform — so the same code can behave differently per environment without an if (isProd) scattered everywhere:
# .env.production
API_URL=https://api.example.com
# .env.development
API_URL=http://localhost:4000
fetch(`${process.env.API_URL}/users`);
const apiKey = 'sk-live-abc123' hardcoded in source — now it's in git history forever, identical across every environment.
NEXT_PUBLIC_/VITE_-prefixed variable is visible to anyone who opens dev tools. It's compiled directly into the shipped JavaScript — never put a real secret (an API key with write access, a database password) behind this prefix; it's genuinely public the moment it's in the bundle..env files should never be committed to git — .env.example (with placeholder values, no real secrets) is the standard way to document what variables are needed without leaking the actual values; the real .env belongs in .gitignore."How would you handle an API key that different environments need different values for, without hardcoding it or leaking it?" is a practical, common question — knowing the build-time-vs-runtime distinction and the public/private prefix boundary is what separates "I know env vars exist" from actually having shipped with them safely.