Environment variables and where secrets live
Explain how API keys and passwords usually reach a program: environment variables, `.env` files and config files. Explain why these files must stay out of git and out of anything you share.
Builds on: Terminal basics: where you are and running things
The script wants a key you don't have
You download a small weather script. Its README says: *Set `WEATHER_API_KEY` before running.* You sign up on the weather site, get a long key that starts `wk_live_9f2a`, and run `python weather.py`. It prints `Error: WEATHER_API_KEY is not set`.
Where is the key supposed to go? And why didn't the author just paste their own key into the code?
Programs read secrets from outside the code
Every program you start gets a set of named text values from the terminal that launched it. These are **environment variables**. The script reads one by name, e.g. `os.environ["WEATHER_API_KEY"]`. The code stays the same for everyone, and each person supplies their own key.
A `.env` file is a plain text file of `NAME=value` lines that a library loads into environment variables. **Config files** like `config.yaml` can hold secrets too.
Where secrets live, and where they never go
- **Secrets** are API keys, passwords, tokens and database addresses that contain passwords. Anyone holding one can act, and spend, as you.
- They live in environment variables, `.env` or a local config file. Never in the code.
- Many projects use **git**, a tool that records every version of the project's files. A folder tracked by git is a **repository**, often uploaded to GitHub for others to download. Git keeps old versions in its history even after you delete a file.
- So list `.env`, and any config file holding secrets, in `.gitignore` (a file naming what git must skip). Share a `.env.example` that has the names but no values.
- Keep secrets out of screenshots, pasted chats, zips and logs.
- Leaked once? Revoke the key at the provider. Deleting the file doesn't undo the leak.
Making the key stick, safely
`export WEATHER_API_KEY=<your-key>` sets the variable only for this terminal window and the programs started from it. Open a new tab and the variable is gone.
To make it last, copy `.env.example` to a new file named `.env` and fill in your key. This works when the script loads `.env` with a library, and the README usually says so. Then run `cat .gitignore` and check that `.env` is listed.
Files starting with `.` are hidden, so plain `ls` skips them. Use `ls -a` to see them.
The session, step by step
First the script fails. Then a one-off `export` makes it work in this window (the key is shortened here). Last, you check which files exist and that `.gitignore` excludes `.env`. (On Windows PowerShell, use `$env:WEATHER_API_KEY="…"` instead of `export`.)
$ python weather.py
Error: WEATHER_API_KEY is not set
$ export WEATHER_API_KEY=wk_live_9f2a
$ python weather.py
Berlin: 14°C, light rain
$ ls -a
. .. .env.example .gitignore weather.py
$ cat .env.example
WEATHER_API_KEY=
$ cat .gitignore
.env
Remember this
Secrets reach a program from outside the code: environment variables, a `.env` file or a local config file. Those files stay out of git (through `.gitignore`) and out of anything you share. A leaked key counts as stolen, so you replace it.
On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.