Skip to main content
Vibe Code Forum

Article: Environment variables and .env files: where secrets go

Your app needs secrets: API keys, database passwords, payment keys. They must never be written into your code, because code gets shared, committed to GitHub, pasted into chats and shipped to browsers. Environment variables keep them separate.

What they are

An environment variable is a named setting that lives outside your code, such as OPENAI_API_KEY=sk-.... Your code reads it by name (process.env.OPENAI_API_KEY in Node, os.environ["OPENAI_API_KEY"] in Python), so the value never appears in the files.

On your computer: the .env file

Most projects use a file called .env in the project folder:

DATABASE_URL=postgres://...
STRIPE_SECRET_KEY=sk_test_...

Two rules:

  1. .env must be listed in .gitignore before your first commit, so Git never saves it.
  2. Commit a .env.example with the same names and empty or fake values, so you (and your AI tool) know what the project needs.

On your host

Hosting platforms have an "Environment variables" settings page. Add the same names and real values there. After changing them, redeploy, because most platforms only read them when building or starting. See Vercel, Netlify and friends.

Browser variables are not secret

Front-end frameworks let some variables reach the browser on purpose, usually ones whose names start with a prefix such as NEXT_PUBLIC_, VITE_ or EXPO_PUBLIC_. Anyone can read those by opening the page source. Use them only for values that are meant to be public, such as a Supabase anon key or an analytics ID. If the AI puts a secret key in one of these, stop and ask it to move that call to the server.

Quick checks

  • Search your project for sk-, secret and key before pushing.
  • Ask your AI tool: "Is any secret key used in front-end code or committed to Git?"
  • If a key ever leaks, revoke it at the provider immediately. See I pushed my API key to GitHub.

More in Tools explained