Environment Variables Deep Dive
Keep settings and secrets for your computer, a preview site, and the live site apart, without leaking a key into the browser.
Content date: October 15, 2026.
Plain English
Environment variables are named settings handed to a running program, such as a database address or an API key (a password that lets software use a service). Locally they usually live in a file named .env. A value there isn't automatically secret: build tools copy variables with certain names (VITE_ in Vite, NEXT_PUBLIC_ in Next.js) into code every visitor downloads.
Use this when
Do this when you connect a service, and whenever you move the same code between your computer, a preview site, and the live site.
If you are just starting
Make a .env.example listing each variable name with a harmless description, never a real value. Put .env in .gitignore (the file telling Git what not to save), and run `git check-ignore .env`, which prints the name if Git ignores it. Keep secrets out of browser-visible variables.
When your project grows
Store credentials in your hosting platform's secret settings, with different ones for preview and live sites. Write down who owns each and how to revoke it. If a key leaks, revoke or replace it right away. Deleting the file or commit doesn't help, because copies and Git history still hold it.
Worked example
A school-trip signup page shows weather from a paid service. The key stays on the server: the browser asks the site for a forecast, and the server adds the key. Searching the built files for the key finds nothing, and a failed request shows "weather unavailable."
Practice
Trace a configuration value
- List every setting your practice project needs and which part of the app reads each.
- Create .env.example with names and one-line descriptions, then run `git check-ignore .env`.
- Put the fake value FAKE_SECRET_MARKER_123 in a variable starting with VITE_ or NEXT_PUBLIC_, run a production build, and ask your AI tool to search the built folder for that text. Move it to a server-only variable and search again.
- Remove one required variable and start the app. Confirm the error names it and prints no other values.
- Write how you would handle a leaked key: where to disable it, who to tell, how to replace it.
Deliverable: Your variable table, .env.example, both search results, the error message, and your revocation notes.
Common mistakes to watch for
- A payment secret in a VITE_ or NEXT_PUBLIC_ variable is downloaded by every visitor.
- A preview site uses the live database password, and a tester edits real customer records.
- A missing setting falls back to a default that skips a safety check.
- A committed key is deleted next day but still works, and sits in Git history.
Prompting
A weak prompt
Put all my API keys in the frontend env file so the app can use them.
A better prompt
List every environment variable: where it is set, which part of the app reads it, and whether it is public or secret. Check which ones the build copies into browser code. Create a .env.example with names only and confirm .env is in .gitignore. Make the app fail at startup naming any missing variable, without printing values. Search the built site for a fake secret marker.
Most leaked keys escape through a build setting or a commit nobody checked, not through a hacker.
Check before you move on
- Each variable is labeled public or secret.
- You know which part of the app reads each.
- .env.example holds names, never real values.
- `git check-ignore .env` confirms .env is ignored.
- Preview and live sites use different credentials.
- You wrote down how to revoke each secret.
You are done when
- A fake secret marker appears nowhere in the built site or the logs.
- Another person could set up the variables and revoke each secret from your notes.
Questions to think about
A key sits in your .env file. Does that mean visitors can't see it?
Not necessarily. Some build tools copy prefixed variables into the page's code, so search the built files.
You committed a key by accident and deleted the file within minutes. Are you done?
No. The key remains in Git history and may be copied. Revoke or replace it and check the service's logs.
What should the error say when a required setting is missing?
Name the missing setting and how to add it, nothing more. Printing other settings could leak passwords into logs.