Environment variables are named settings your computer keeps outside of your code.
They work like sticky notes attached to your terminal session: <code>OPENAI_API_KEY=sk-proj-...</code>. When your program starts, it reads those notes. This way, secret keys and settings live outside the code — so you can change a setting without editing a file, and so you never accidentally share a secret by uploading your code.
I once spent three hours debugging why a script that worked perfectly on my laptop threw an error on my friend's machine. Same code. Same Python version. Different result. The issue was a single environment variable my script read silently on startup — one that I'd set months ago and completely forgotten about.
Environment variables are the invisible settings layer between your code and the world. Every program you run inherits a collection of them from the shell that launched it. Most you'll never touch. But for AI projects, two or three variables — usually your API keys — become load-bearing: the program won't start without them.
Once you understand what environment variables are and where they live, a whole class of confusing errors disappears. "Works on my machine" stops being a mystery. Setup instructions stop feeling like magic spells.
// 01What they actually are
An environment variable is a key-value pair — a name and a value — stored in your operating system's running environment. Every terminal session starts with a set of them already loaded. You can see them all right now.
These variables are inherited by every program your terminal launches. So when a Node.js script calls process.env.OPENAI_API_KEY, or a Python script calls os.environ["OPENAI_API_KEY"], they're reading from this same pool. The program reads the setting; you never hard-code it.
// 02Why they exist (the reason makes everything else clear)
Environment variables exist to solve two problems at once:
Problem 1: Secrets in code
If you put your API key directly in a Python file, that file gets committed to git, pushed to GitHub, and read by every bot that crawls public repositories. Keys in code get stolen. Environment variables live outside the code — they never touch git history.
Problem 2: Config that changes between environments
Your code runs in multiple places: your laptop (development), maybe a friend's laptop, maybe a cloud server (production). The database URL is different in each place. The API key might be different. Rather than editing the code every time you switch contexts, you change one environment variable. The code stays the same.
// 03The .env file — how you set them for a project
Setting environment variables in your shell manually works, but it's annoying — you'd have to re-set them every time you open a terminal. The standard solution is a .env file: a plain-text file in your project root, with one variable per line.
# AI service keys OPENAI_API_KEY=sk-proj-abc123... ANTHROPIC_API_KEY=sk-ant-api03-xyz789... # App settings PORT=3000 NODE_ENV=development
Your code loads this file at startup using a library (dotenv for Node.js, python-dotenv for Python). After that, every variable in the file is available as an environment variable — exactly as if you'd set them in the shell.
// 04How to read them in code
Each language has its own way to read environment variables. You'll see one of these patterns in every AI guide on this site:
# Python
import os
key = os.environ["OPENAI_API_KEY"] # raises error if missing
key = os.environ.get("OPENAI_API_KEY", "") # returns empty string if missing
// JavaScript / Node.js
const key = process.env.OPENAI_API_KEY; // undefined if missing
// Shell (bash/zsh)
echo $OPENAI_API_KEY
# PowerShell (Windows)
$key = $env:OPENAI_API_KEYWhen a guide says "the script reads OPENAI_API_KEY from the environment," this is what's happening. The variable name is a contract between the code and the person running it: you set that name in your .env file, the code looks it up by that exact name.
// 05Now you. Set a variable, read it back.
Five minutes. We'll set an environment variable in the shell and in a .env file, then read it back both ways. No API key needed — we'll use a fake one for the exercise.
Set an environment variable and read it back
- Open a terminal.
- MACMac/Linux — set a variable inline: type
export TEST_KEY=hello123and press Enter. Now typeecho $TEST_KEY. You should seehello123. - WINWindows PowerShell — set a variable inline: type
$env:TEST_KEY = "hello123"and press Enter. Now typeecho $env:TEST_KEY. You should seehello123. - Create a .env file: In any folder, create a file called
.env. Add the line:TEST_KEY=hello123. Save it. - Read it with Python: In the same folder, create
test.pywith these lines:from dotenv import load_dotenv; import os; load_dotenv(); print(os.environ.get("TEST_KEY")). Run it withpython test.py. You should seehello123. - Confirm .gitignore protection: If the folder has a git repo, open (or create)
.gitignoreand verify.envis listed. Rungit status— the .env file should not appear in the untracked list.