Troubleshooting · Codex CLI
Codex CLI: Missing OPENAI_API_KEY — Custom Provider Setup
Codex refuses to start when the environment variable its provider expects is unset. How env_key works in config.toml, why exports vanish between shells, and a working profile.
What you see
Missing OPENAI_API_KEYWhy it happens
- The environment variable named by the provider's env_key is not set in the shell that runs codex. With a custom provider the variable can be any name — the error names whichever one is missing.
- The key was exported in a different terminal or added to a profile file that this shell never sourced.
- The variable is set but empty, which counts as missing.
- The profile in use points at a different provider than you think, so Codex looks for a different variable than the one you exported.
How to fix it
- Declare the provider in a profile and export the exact variable its env_key names, in the same shell, before running codex.
- Print the variable right before launching to confirm it survived: an export is per-shell, not global.
- Run codex with the profile named explicitly so there is no ambiguity about which provider — and which env_key — is active.
Working profile for this gateway
# ~/.codex/apitoken.config.toml
model = "gpt-5.6-sol"
model_provider = "apitoken"
[model_providers.apitoken]
name = "apiToken.sale"
base_url = "https://openai.api.apitoken.sale/v1"
wire_api = "responses"
env_key = "APITOKEN_API_KEY"
# then, in the same shell:
export APITOKEN_API_KEY="sk-pool-•••"
codex --profile apitokenRelated searches
OPENAI_API_KEY environment variable not setcodex api key not found
FAQ
Do I need an OpenAI account key to run Codex CLI?+
No. With a custom model provider, env_key names any variable you choose, and the key comes from that provider — OPENAI_API_KEY itself is only the default provider's variable.
I exported the key but Codex still says it is missing — why?+
Exports are per-shell. If codex runs in another terminal, a multiplexer pane, or an IDE task, that environment never saw your export. Set it where codex actually runs.
Where should the key live, the TOML or the shell?+
The shell. config.toml stores only the variable's name via env_key, so the secret never sits in a config file.