Agentic Research

Environment variables and .env

Also: 環境變數 · API key 放哪 · .env · environment variable

Configuration a program reads from outside itself, instead of values hardcoded in the source.

When you will meet it

Your API key reaches the tool as an environment variable. In the wrong place you get "unauthorised"; in the right place but leaked, you burn your quota.

An analogy

Like a hotel key card: the program is the room, the environment variable is the card handed over at check-in. New guest, new card — no need to rebuild the room.

Minimal example

# macOS / Linux
export OPENAI_API_KEY="sk-..."

# Windows PowerShell
$env:OPENAI_API_KEY = "sk-..."

# 或者放進專案目錄下的 .env 檔案
OPENAI_API_KEY=sk-...

export lasts only for the current window and is gone when you close it; .env persists in a file that the tool reads itself.

What people get wrong

  • Committing .env to git. It belongs in .gitignore. If it was ever committed, treat that key as leaked: revoke and reissue it.
  • Exporting in one window and running the program in another. Environment variables do not cross windows.

Related terms

Next