LIVEReading: Foundation #05Topic: What are environment variables?Read time: 6 minSection: ENV LIVEReading: Foundation #05Topic: What are environment variables?Read time: 6 minSection: ENV
CBW
// FOUNDATION#05Beginner

What are environment variables?

The hidden settings every AI tool reads on startup. Why your install "works on Tuesday and fails on Wednesday" is usually one of these.

Read time4 min
LevelBeginner
Need beforeAPI key #04
UnlocksProduction-ready setup
AudienceFirst-timers
// THE SHORT ANSWER

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.

Terminal — list all environment variables
# Mac / Linux
$env
HOME=/Users/yourname
PATH=/usr/local/bin:/usr/bin:/bin
LANG=en_US.UTF-8
TERM=xterm-256color
... (dozens more)
# Windows PowerShell
$Get-ChildItem Env:
USERPROFILE C:\Users\yourname
PATH C:\Windows\system32;...

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.

// what breaks
Hardcoded in code
// what works
Environment variable
const db = "postgres://dev.local/mydb"
const db = process.env.DATABASE_URL
Works on your laptop.
Laptop: DATABASE_URL=postgres://dev.local/mydb
Crashes on the server.
Server: DATABASE_URL=postgres://prod.server/mydb
Edit the file every time you deploy.
Same code, different settings per environment.
Accidentally push the prod key to GitHub.
No secrets ever touch the code.

// 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.

// .env (in your project root)
# 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.

Terminal — using dotenv in Python
# first, install the library
$pip install python-dotenv
Successfully installed python-dotenv-1.0.0
# in your Python file
from dotenv import load_dotenv
import os
load_dotenv() # reads .env file
key = os.environ["OPENAI_API_KEY"]

// 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:

// reading env vars — language cheat sheet
# 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_KEY

When 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.

// TRY IT — 5 minutes

Set an environment variable and read it back

  1. Open a terminal.
  2. MACMac/Linux — set a variable inline: type export TEST_KEY=hello123 and press Enter. Now type echo $TEST_KEY. You should see hello123.
  3. WINWindows PowerShell — set a variable inline: type $env:TEST_KEY = "hello123" and press Enter. Now type echo $env:TEST_KEY. You should see hello123.
  4. Create a .env file: In any folder, create a file called .env. Add the line: TEST_KEY=hello123. Save it.
  5. Read it with Python: In the same folder, create test.py with these lines: from dotenv import load_dotenv; import os; load_dotenv(); print(os.environ.get("TEST_KEY")). Run it with python test.py. You should see hello123.
  6. Confirm .gitignore protection: If the folder has a git repo, open (or create) .gitignore and verify .env is listed. Run git status — the .env file should not appear in the untracked list.
You've set an environment variable, read it from a .env file, and confirmed git ignores it. That's the complete environment variable workflow for every AI project.
// READ NEXT3
ALL 5 →