codex icon indicating copy to clipboard operation
codex copied to clipboard

Codex App (WSL mode): Missing environment variable `AZURE_OPENAI_API_KEY` although variable exists in Windows and WSL

Open ckorherr opened this issue 8 months ago • 13 comments

What version of the Codex App are you using (From “About Codex” dialog)?

26.304.1528.0

What subscription do you have?

Azure OpenAI

What platform is your computer?

Microsoft Windows NT 10.0.26100.0 x64

What issue are you seeing?

After the latest update I can successfully start the Codex App with:

runCodexInWindowsSubsystemForLinux: true

However, every prompt returns the error:

Missing environment variable: `AZURE_OPENAI_API_KEY`

My config.toml:

model = "gpt-5.3-codex"
model_provider = "azure"
model_reasoning_effort = "medium"

[model_providers.azure]
name = "Azure"
base_url = "https://xyz.openai.azure.com/openai"
env_key = "AZURE_OPENAI_API_KEY"
query_params = { api-version = "2025-04-01-preview" }
wire_api = "responses"

The environment variable is set in both environments:

WSL:

printenv AZURE_OPENAI_API_KEY

Windows PowerShell:

echo $env:AZURE_OPENAI_API_KEY

Both commands return the correct key.

Despite this, Codex still reports the variable as missing.

What steps can reproduce the bug?

  1. Create ~/.codex/config.toml on Windows with the Azure provider configuration
  2. Set the environment variable AZURE_OPENAI_API_KEY
  3. Enable WSL mode (runCodexInWindowsSubsystemForLinux=true)
  4. Start the Codex App
  5. Open a project and send a prompt

Result:

Missing environment variable: AZURE_OPENAI_API_KEY

What is the expected behavior?

Codex should detect the existing environment variable and authenticate successfully.

Additional information

Environment:

Codex App version: 26.304.1528.0
Windows: Microsoft Windows NT 10.0.26100.0 x64
WSL: Ubuntu (WSL2)
Subscription: Azure OpenAI

The variable is available in both the Windows and WSL environments but is still reported as missing when Codex runs in WSL mode.

ckorherr avatar Mar 05 '26 08:03 ckorherr

Are you sure the environment variable is exported? Can you see its value VS Code's built-in terminal?

etraut-openai avatar Mar 05 '26 14:03 etraut-openai

yes, in VSCode connected to the same repo in WSL and executing "printenv AZURE_OPENAI_API_KEY" -> I see the key.

Image

ckorherr avatar Mar 05 '26 15:03 ckorherr

Fixed ? same for the ZenMux provider

Atlantic83 avatar Mar 06 '26 13:03 Atlantic83

Also seeing this issue. A related issue is that the environment dropdown (cog in the top right) is not visible for new chats and that a newly created environment has to be manually selected using the environment dropdown, which only appears once you start the session, then still doesn't seem to work once turned on.

Image Image

kendonB avatar Mar 06 '26 18:03 kendonB

+1 on this. Did you find a way around?

kong75 avatar Mar 15 '26 17:03 kong75

+1

wangchaofan2018 avatar Mar 20 '26 08:03 wangchaofan2018

+1

Sagarika-Gawhankar-AI avatar Mar 22 '26 08:03 Sagarika-Gawhankar-AI

I've kind of found the solution. The issue was my AZURE_OPENAI_API_KEY wasn't setting permanently in WSL.

So for me the solution was to set the key permanently and this is how I managed to do it:

  1. In Windows set AZURE_OPENAI_API_KEY and value in Edit environment variables for your account --> User variables interface.
  2. Then we need to share this key with the WSL, so in the same window look for a variable named WSLENV or create one and set the value to AZURE_OPENAI_API_KEY/u (/u is important)
  3. Restart WSL (using wsl --shutdown)
  4. Restart the Codex app

If this doesn't work, try this:

In WSL terminal:

  1. nano ~/.bashrc
  2. Paste this at the very bottom: export AZURE_OPENAI_API_KEY="you-key"
  3. Save and Exit
  4. source ~/.bashrc
  5. Restart the Codex app

Let me know if it helps.

Sagarika-Gawhankar-AI avatar Mar 23 '26 17:03 Sagarika-Gawhankar-AI

Same problem here with codex app in wsl mode, although it works in cli and in the VSC codex extension, in wsl.

That's the config I am testing with:

model_provider = "openrouter"
model_reasoning_effort = "low"
model="xiaomi/mimo-v2.5-pro"

[model_providers.openrouter]
name = "openrouter"
base_url="https://openrouter.ai/api/v1"
env_key="OPENROUTER_API_KEY"

Error: Missing environment variable: OPENROUTER_API_KEY.

But when I open a terminal in the codex app and run echo $OPENROUTER_API_KEY it outputs the value.

h0gar avatar Jun 17 '26 10:06 h0gar

Same issue here — OpenRouter provider on native Linux (Oracle Linux 9, aarch64), Codex CLI v0.142.5, deepseek/deepseek-v4-flash.

Config:

model = "deepseek/deepseek-v4-flash"
model_provider = "openrouter"
model_reasoning_effort = "high"

[model_providers.openrouter]
name = "openrouter"
base_url = "https://openrouter.ai/api/v1"
env_key = "OPENROUTER_API_KEY"

OPENROUTER_API_KEY was set in ~/.bashrc and confirmed via echo $OPENROUTER_API_KEY, but Codex still reported Missing environment variable: OPENROUTER_API_KEY.

Root cause: The persistent app-server daemon (codex doctor showed app-server running (persistent mode)) was holding stale environment from before the key was added to .bashrc. The daemon starts on first Codex launch and never picks up new env vars.

Fix that worked:

rm -f ~/.codex/app-server-daemon/app-server.pid
pkill -f codex
source ~/.bashrc
codex

This confirms the issue isn't WSL-specific — it affects native Linux when the app-server daemon predates the env var being set. The CLI itself works fine once the daemon is restarted with the correct environment.

BongSuCHOI avatar Jul 06 '26 10:07 BongSuCHOI

I can reproduce this with the Codex VS Code extension when connecting through a VS Code Remote Tunnel to a Windows host and selecting WSL as the agent environment.

Environment:

  • Codex extension: [email protected]
  • Remote host: Windows
  • WSL: Ubuntu 26.04, default user user
  • Config: /home/user/.codex/config.toml
  • Provider: Azure OpenAI using env_key = "AZURE_OPENAI_API_KEY"
  1. The variable exists in the Windows VS Code host environment.

  2. The host WSLENV contains AZURE_OPENAI_API_KEY/u.

  3. A direct non-interactive launch receives it:

    wsl.exe --exec sh -c 'test -n "$AZURE_OPENAI_API_KEY" && echo SET || echo MISSING'
    

    Result: SET.

  4. The extension loads the correct WSL user and config file.

  5. The live Codex app-server process does not contain the variable:

    HOME=present
    USER=present
    PATH=present
    WSLENV=present
    AZURE_OPENAI_API_KEY=missing
    
  6. Inspecting WSLENV inside the live app-server process shows that it has been replaced with an extension-generated allowlist containing Windows system and proxy variables. The original AZURE_OPENAI_API_KEY/u entry is absent.

This suggests the WSL launcher constructs a new WSLENV value instead of merging its required entries with the existing host WSLENV. Consequently, wsl.exe itself propagates the variable correctly, but the Codex app-server is launched without it.

The same Azure configuration works when the tunnel and Codex extension run directly inside a WSL remote environment.

A likely fix would be for the Windows-to-WSL launcher to preserve and merge the existing WSLENV entries, or explicitly forward the environment variable referenced by the selected provider’s env_key.

tkalpdev avatar Aug 18 '26 15:08 tkalpdev

Root cause: The persistent app-server daemon (codex doctor showed app-server running (persistent mode)) was holding stale environment from before the key was added to .bashrc. The daemon starts on first Codex launch and never picks up new env vars.

I had this as well. I switched to using codex --no-daemon to launch and env vars are fine

jhofmeyr avatar Oct 12 '26 19:10 jhofmeyr

Guys, after you ensure that you export your api key, I use the following command to run my codex. This works for me. Hopefully this saves everyone's time.

codex --config preferred_auth_method='apikey'

gentdirga avatar Nov 29 '26 22:11 gentdirga