Codex App (WSL mode): Missing environment variable `AZURE_OPENAI_API_KEY` although variable exists in Windows and WSL
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?
- Create
~/.codex/config.tomlon Windows with the Azure provider configuration - Set the environment variable
AZURE_OPENAI_API_KEY - Enable WSL mode (
runCodexInWindowsSubsystemForLinux=true) - Start the Codex App
- 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.
Are you sure the environment variable is exported? Can you see its value VS Code's built-in terminal?
yes, in VSCode connected to the same repo in WSL and executing "printenv AZURE_OPENAI_API_KEY" -> I see the key.
Fixed ? same for the ZenMux provider
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.
+1 on this. Did you find a way around?
+1
+1
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:
- In Windows set
AZURE_OPENAI_API_KEYand value in Edit environment variables for your account --> User variables interface. - Then we need to share this key with the WSL, so in the same window look for a variable named
WSLENVor create one and set the value toAZURE_OPENAI_API_KEY/u(/uis important) - Restart WSL (using
wsl --shutdown) - Restart the Codex app
If this doesn't work, try this:
In WSL terminal:
nano ~/.bashrc- Paste this at the very bottom:
export AZURE_OPENAI_API_KEY="you-key" - Save and Exit
source ~/.bashrc- Restart the Codex app
Let me know if it helps.
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.
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.
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"
-
The variable exists in the Windows VS Code host environment.
-
The host
WSLENVcontainsAZURE_OPENAI_API_KEY/u. -
A direct non-interactive launch receives it:
wsl.exe --exec sh -c 'test -n "$AZURE_OPENAI_API_KEY" && echo SET || echo MISSING'Result:
SET. -
The extension loads the correct WSL user and config file.
-
The live Codex
app-serverprocess does not contain the variable:HOME=present USER=present PATH=present WSLENV=present AZURE_OPENAI_API_KEY=missing -
Inspecting
WSLENVinside the liveapp-serverprocess shows that it has been replaced with an extension-generated allowlist containing Windows system and proxy variables. The originalAZURE_OPENAI_API_KEY/uentry 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.
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
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'