Env Var
You might say
I know a secret key shouldn't go in the code. Where should I put it?
Provide configuration outside the source codeEnvironment variables supply values such as API endpoints, feature settings, and secret keys when a program starts. Public browser variables are visible to visitors, even if their names come from an environment file. Keep real secrets on the server and document the required variable names with safe examples.
Know first
When to use it
- Set different values for development and production.env
OPENAI_API_KEY=sk-••••••
DATABASE_URL=postgres://••••Keep one local setting per line. Do not commit, screenshot, or forward sensitive values. - Provide server-side credentialsconst key = process.env.OPENAI_API_KEYCode reads the variable name, so you can usually change the configuration value without changing code.
- Configure an API endpoint.gitignore
node_modules
.envUntracked files that match the rule are skipped; tracked files need separate handling. - Turn deployment-specific features on or off💻 Local .env☁️ Platform settings
When NOT to use it
- Commit a file containing real secretsconst key = "sk-proj-8fk2…"A secret committed to the repository may appear in history, clones, or build artifacts.
- Put private keys in variables exposed to browser code.env→ Forward to a teammate / share a screenshot in the group chatSharing the file or a screenshot may expose sensitive values in it.
- Assume changing a value updates a running process automaticallyRight-click → View page source<script> key="sk-…" </script>Once written into frontend code, visitors may read it directly.
- Leave required variables undocumentedChanged .env, then just refreshed the page…401 Unauthorized (still using the old key)
Anatomy
OPENAI_API_KEY=sk-proj-•••••• # For local development, please do not submit
It is the convention to use all uppercase letters and underlines, so read this name in the code.
After the equal sign is the actual configuration value; when the key is included, it must be covered before taking the screenshot.
Notes starting with # will be ignored by the program.
Variants
.env
.env
For local development use, should usually be excluded by .gitignore
Platform Env
Vercel → Settings → Env
Add another copy to the deployment platform settings page.
.env.example
.env.example
Telling others what to wear is not true.
Typical use cases
Database connection
API credential
Site URL
Feature setting
