Sub topic 4
Storing the key in an environment variable
This chapter assumes you can use a shell, read short Python or JavaScript snippets, and know what an HTTP API is. You have a key from your provider's console. The next step is to keep it out of your source files.
What an environment variable is
An environment variable is a named string that the operating system passes to a running process. Your program reads it by name, so the key lives outside the code. The same code can then run on your laptop, a test server, and production, each with its own key.
Hard-coded keys end up in Git history, screenshots, and shared snippets. An environment variable keeps the value out of the files you commit.
Setting the variable in your shell
In Bash or Zsh, use export. The command below stores the key and prints only its length, so the key is never echoed.
export LLM_API_KEY="paste-your-key-here"
echo "${#LLM_API_KEY} characters set"export marks the variable so that child processes inherit it. Without export, the variable exists only in the current shell, and a Python or Node program you start from it will not see it.
The variable lasts only for that terminal session. When you close the terminal, it is gone. This is a useful default, but it means you must set it again each time. The next chapter covers a .env file for that.
Reading the variable in code
Your code should read the variable by name and fail early if it is missing. In Node.js:
const apiKey = process.env.LLM_API_KEY;
if (!apiKey) {
throw new Error("LLM_API_KEY is not set");
}The Python equivalent uses os.environ:
import os
api_key = os.environ.get("LLM_API_KEY")
if not api_key:
raise RuntimeError("LLM_API_KEY is not set")Checking at startup gives you a clear message. Without the check, the program would send an empty key and you would see a confusing authentication error from the provider.
Common errors
- The variable is set in a different terminal. Each terminal window has its own environment. Set it in the one you run the program from.
- You forgot
export. Runexport LLM_API_KEY=..., not justLLM_API_KEY=.... - Quotes or spaces are wrong. Write
LLM_API_KEY="value"with no spaces around=. A space makes the shell treat the name as a command. - An IDE or service started before you set the variable. A process reads the environment when it starts. Restart the IDE, terminal, or service after you change it.
- The key is printed by accident. Commands like
env,printenv, or debug logging of the full environment will show it. Avoid pasting their output into chats or bug reports.
Limits of environment variables
Environment variables are not encrypted. Other processes run by the same user can often read them, and some tools and crash reports include them. They protect you from committing the key by mistake. They do not protect you from a compromised machine. For stronger protection, later chapters cover CI secrets and dedicated secret managers.
Exercise
You run LLM_API_KEY="abc" in your terminal, then start a Python script that calls os.environ.get("LLM_API_KEY"). The script reports that the key is not set. What is the likely cause, and how do you fix it?
Show answer
The assignment did not use export, so the variable exists only in the shell and is not passed to child processes such as the Python script. Run export LLM_API_KEY="abc" (or set it on the same line as the command, LLM_API_KEY="abc" python script.py) and start the script again.
Exercise
Why does the Node.js snippet throw an error at startup when the variable is missing, instead of continuing?
Show answer
A missing key would otherwise cause an empty or invalid value to be sent to the provider, producing a confusing authentication failure later. Failing at startup gives a clear message that names the missing variable.