Sub topic 7
Reading the Key in Your Code
This chapter assumes you can run short Python or JavaScript snippets, that you have set LLM_API_KEY as an environment variable or in a .env file, and that .env is already in .gitignore. Here you will read the key into your program without writing it in the source.
The working model
Your code never contains the key. It asks the process environment for a variable by name. The operating system gives each process a copy of the environment when the process starts. A real environment variable, set with export or by a deployment system, is therefore available the moment your program begins.
A .env file is not part of the environment. It is a plain text file. A loader library reads it and copies its lines into the environment of your running process. After that, your code reads the key the same way whether it came from the shell, a .env file, or a CI system.
Python
Install python-dotenv, then load the file once at startup.
import os
from dotenv import load_dotenv # pip install python-dotenv
load_dotenv() # reads .env into os.environ; real environment variables are not overridden by default
api_key = os.environ.get("LLM_API_KEY")
if not api_key:
raise RuntimeError("LLM_API_KEY is not set")Two details matter here:
load_dotenv()does not override a variable that is already set. A key exported in your shell wins over the one in.env. This is useful in production, where the host supplies the real value.os.environ.getreturnsNonewhen the variable is missing. Theif not api_keycheck also catches an empty string, so the program fails at startup with a clear message instead of failing later with a confusing authentication error.
Node.js
In Node.js, process.env holds the environment. Check for the key once at startup.
const apiKey = process.env.LLM_API_KEY;
if (!apiKey) {
throw new Error("LLM_API_KEY is not set");
}Node does not read .env files on its own in older versions. You can use the dotenv package, or on recent Node versions start the program with node --env-file=.env app.js. Check which Node version you run before relying on the flag.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `LLM_API_KEY is not set` | The variable was exported in a different terminal, or `.env` is not in the directory you ran from | Export it in the same shell, or check your working directory |
| `.env` value seems ignored | An older value is already exported in the shell, and the loader does not override it | Run `unset LLM_API_KEY`, or update the exported value |
| `401 Unauthorized` | Extra spaces or quotes in the value, or a revoked key | Check the `.env` line has no trailing spaces, and confirm the key is still active in the console |
| Key works locally, fails when deployed | The host has no variable set, and `.env` was never deployed (correctly) | Set the variable in the host's secret settings |
Keep the key out of output
Read the key once, pass it to the client, and never print it. Avoid logging whole request headers or configuration objects, because they often contain the key. If you need to confirm the value loaded, print its length or a status code, not the value.
Exercise
You export LLMAPIKEY=old in your shell, and your .env file contains LLMAPIKEY=new. You call loaddotenv() with default settings and then read os.environ["LLMAPI_KEY"]. Which value do you get, and why?
Show answer
You get "old". By default load_dotenv() does not override variables that already exist in the environment, so the exported shell value takes precedence over the .env line.