env_key in Codex: how it reads your key from an environment variable

Coding with AICodex CLI·· 19 views· 3 min read

In the Interweb provider, the env_key line does not hold the key: it holds the name of the variable where the key is. Codex reads that variable at start and sends the key in the Authorization: Bearer header. That way, the key is never written in config.toml.

How it looks in the file

model = "moonshotai/kimi-k2.7-code"
model_provider = "api"

[model_providers.api]
name = "Interweb"
base_url = "https://api.interweb.co.mz/v1"
env_key = "INTERWEB_API_KEY"
wire_api = "responses"

Note: env_key = "INTERWEB_API_KEY", not env_key = "prod_...". If you put the key where the name goes, Codex looks for a variable with that name, does not find it and the request fails.

Keep the key in the variable

On Windows, open Command Prompt or PowerShell and type (with your key in quotes):

setx INTERWEB_API_KEY "prod_..."

setx saves the variable for the windows you open afterwards. Close the terminal and the tool, then open them again.

On Linux and macOS, type in the terminal:

export INTERWEB_API_KEY="prod_..."

To have the variable in every new terminal, add the same line at the end of ~/.bashrc or ~/.zshrc.

If the variable does not exist

Codex only reads the variable when it starts. If you set it later, or in another terminal, Codex does not see it and has no key to send. If the request reaches the API, the answer is 401 unauthorized. Close Codex, open a new terminal and start Codex again.

To check that the variable exists, without showing it on screen. On Linux and macOS:

test -n "$INTERWEB_API_KEY" && echo OK || echo missing

In Windows PowerShell:

if ($env:INTERWEB_API_KEY) { "OK" } else { "missing" }

The other ways, and why we do not use them

Field What the Codex documentation says
env_key The environment variable that holds the provider key. It is the one we use.
env_key_instructions Optional text with instructions to get the key. You can write, for example, where the key is created.
experimental_bearer_token The key written in the file itself. The documentation advises against it and says to use env_key.
The same variable works for the MCP server in Codex, in the bearer_token_env_var field. See the All in One AI MCP server.

Want to code with AI in Codex? Visit the Coding with AI page.

See Coding with AI

SEE ALSO

Codex CLI with All in One AI: creating config.toml, the key and the model

Codex on Windows: saving the key with setx and opening a new terminal

An API key or token has leaked: what to do, in the right order

Errors when setting up a tool with All in One AI: 401, model not found, no credits, /v1 and rejected config files

Coding with AI in All in One AI

RECOMMENDED PRODUCT

Web hosting with cPanel

Domain and SSL included, daily backups and the panel you already know. from 321,75 MT/mo (3-year plan, with coupon)

See plans
Was this answer helpful? Your answer helps us improve this article.