How to give the Image Generation System a key, safely
The render scripts need an OpenAI API key to call the gpt-image endpoint. They look for it in this order:
- The
OPENAI_API_KEYenvironment variable - A
.envfile — the explicit path you pass (-EnvFile/--env-file), then.envin the current directory, then.envnext to the script - If none is found, the script stops with a clear error
You only need one of these.
- Get an API key from https://platform.openai.com/api-keys.
- Copy the example file:
cp .env.example .env - Open
.envand replace the placeholder with your real key:OPENAI_API_KEY=sk-your-real-key-here - Add
.envto your.gitignore(critical — see below).
Place .env either in the folder you run renders from, next to the render script, or pass its path
explicitly with -EnvFile (PowerShell) / --env-file (bash).
PowerShell:
$env:OPENAI_API_KEY = "sk-your-real-key-here" # current session
# Persist across sessions:
[Environment]::SetEnvironmentVariable("OPENAI_API_KEY", "sk-your-real-key-here", "User")Bash:
export OPENAI_API_KEY="sk-your-real-key-here"
# Persist by adding that line to ~/.bashrc or ~/.zshrcA leaked API key can cost you real money. These practices keep it safe:
- Gitignore the secret — add
.envto.gitignoreso it can never be committed. Only.env.example(with the placeholder) belongs in version control. - Never
git add -Ablindly — stage files by name, or confirmgit statusfirst, so a stray.envdoesn't slip in. - The scripts never echo the key — and they strip any
sk-...pattern out of error messages before printing, so a failed render won't leak it to your terminal or logs. - The key is never returned in the scripts' structured output — only the saved path, cost, and timing.
- Rotate if exposed — if a key ever lands somewhere public, revoke it immediately at https://platform.openai.com/api-keys and issue a new one.
Run a small render after setup:
& Invoke-ImageGen.ps1 -Prompt "a sunset over mountains" -OutFile ".\test.png" -Quality "low"invoke-image-gen.sh --prompt "a sunset over mountains" --outfile ./test.png --quality lowIf the key is missing or still the placeholder, the script tells you exactly which sources it tried. On success it writes the PNG and prints the saved path + estimated cost.