OpenAI Codex Integration
Codex CLI connects to KOPRouter over the OpenAI-compatible Responses API, using your own key for coding tasks.
- Node.js 18 or newer (the Codex CLI runtime)
Install Codex CLI
Run the following in a terminal to install:
npm i -g @openai/codex
Then run codex --version — seeing a version number means it installed successfully (0.138 or newer is required).
Core configuration
Connecting to KOPRouter needs three values:
- Model: a model ID in KOPRouter, taken from the
idreturned by/v1/models - Base URL:
https://koprouter.kopglobal.com/v1(must end in/v1; Codex appends/responsesto it) - API Key: your KOPRouter API key (prefixed with
sk-)
Option 1: Put everything in config.toml (recommended)
Fill in the model provider and API key in one place — no environment variables needed.
```bash
mkdir -p ~/.codex
nano ~/.codex/config.toml
```
```bat
if not exist "%USERPROFILE%\.codex" mkdir "%USERPROFILE%\.codex"
notepad "%USERPROFILE%\.codex\config.toml"
```
Write the following:
model = "MODEL_ID"
model_provider = "koprouter"
[model_providers.koprouter]
name = "KOPRouter"
base_url = "https://koprouter.kopglobal.com/v1"
wire_api = "responses"
experimental_bearer_token = "sk-..."
Save it, then run codex directly.
Option 2: Provide the key via an environment variable
If you prefer not to store the key in plaintext, let Codex read it from an environment variable: replace the experimental_bearer_token line with env_key = "KOPROUTER_API_KEY", then set the variable.
```bash
echo $SHELL
```
* **Zsh**: write to `~/.zshrc`
* **Bash**: write to `~/.bashrc`
```bash
echo 'export KOPROUTER_API_KEY="sk-..."' >> ~/.zshrc
source ~/.zshrc
```
(Bash users: replace `~/.zshrc` with `~/.bashrc`.)
```bat
setx KOPROUTER_API_KEY "sk-..."
echo %KOPROUTER_API_KEY%
```
**PowerShell** (reopen a PowerShell window afterwards):
```powershell
[Environment]::SetEnvironmentVariable("KOPROUTER_API_KEY", "sk-...", [EnvironmentVariableTarget]::User)
echo $env:KOPROUTER_API_KEY
```
Use It
codex
To verify the setup end-to-end, run a minimal task first:
codex exec "echo hello"
KOPRouter forwards /v1/responses to the upstream channel behind the model unchanged and does not translate between Responses and Chat Completions. Codex therefore only works when that upstream natively supports the Responses API (e.g. OpenAI, Azure OpenAI, xAI). If the upstream only supports Chat Completions (e.g. DeepSeek, Moonshot, Mistral, Groq, Zhipu), requests will fail — switch to a model backed by a Responses-capable channel.
If your network cannot reach KOPRouter directly (e.g. mainland China), configure a VPN or proxy first. Codex CLI has no proxy option in config.toml — it reads the HTTPS_PROXY / HTTP_PROXY environment variables, so set them as described in Network & Proxy (Option 3).