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

Coding with AICodex CLI·· 11 views· 2 min read

On Windows, the step that fails most is the key. setx saves the variable, but only the windows opened afterwards see it. This article shows the whole path, from PowerShell to the first answer.

1. Install Codex

The official README gives, in PowerShell:

powershell -ExecutionPolicy ByPass -c "irm https://chatgpt.com/codex/install.ps1 | iex"

If you already have Node.js, you can also use npm install -g @openai/codex.

2. Save the key with setx

setx MOZOUT_API_KEY "prod_..."

setx saves the variable in your Windows account. The window where you typed it does not get it: only the ones you open next.

3. Close and reopen

1 Close the PowerShell or Command Prompt window where you ran setx.
2 If you use the VS Code terminal, close VS Code completely: its terminals inherit the variables from when VS Code started.
3 Open a new terminal and check that the variable exists, without showing it on screen.

In PowerShell:

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

In Command Prompt:

if defined MOZOUT_API_KEY (echo OK) else (echo missing)

4. The config.toml file

The file goes in %USERPROFILE%\.codex\config.toml. In PowerShell:

New-Item -ItemType Directory -Force "$HOME\.codex"
notepad "$HOME\.codex\config.toml"

Paste the configuration from Codex CLI with All in One AI and save. If you use Save as, choose All files (*.*): otherwise Windows adds .txt to the name and Codex does not read the file.

For one window only

To test without saving anything, set the variable in the open terminal only. When you close the window, the variable is gone. In PowerShell:

$env:MOZOUT_API_KEY = "prod_..."

In Command Prompt:

set MOZOUT_API_KEY=prod_...

Changed your key? Run setx again with the new key and open a new terminal. The old window keeps the old key, and a revoked key gives 401.

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

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

Errors in Codex CLI: the OpenAI format and the code field

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

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 $6.60/mo (3-year plan, with coupon)

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