OpenAI Codex Integration

Codex CLI connects to KOPRouter over the OpenAI-compatible Responses API, using your own key for coding tasks.

**Prerequisites**
  • 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 id returned by /v1/models
  • Base URL: https://koprouter.kopglobal.com/v1 (must end in /v1; Codex appends /responses to 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.

The file path is `~/.codex/config.toml`. Create the `.codex` directory first if it doesn't exist:
```bash
mkdir -p ~/.codex
nano ~/.codex/config.toml
```
The file path is `%USERPROFILE%\.codex\config.toml` (usually `C:\Users\\.codex\config.toml`). Using CMD as an example:
```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.

An inline key is stored in plaintext: run `chmod 600 ~/.codex/config.toml` and never commit it to git. Do not use the deprecated `api_key` field (silently ignored since Codex 0.151). Optional: if the selected upstream model supports reasoning summaries, add `model_supports_reasoning_summaries = true` near the `model` line. Leave it out if unsure.

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.

First check which shell you're using:
```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`.)
**CMD** (reopen a CMD window afterwards):
```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
```
`experimental_bearer_token` and `env_key` are mutually exclusive — use only one.

Use It

codex

To verify the setup end-to-end, run a minimal task first:

codex exec "echo hello"
* `base_url` must end in `/v1`; Codex appends `/responses` to it. * `wire_api` must be `responses`. Recent Codex removed `chat` (Chat Completions) support — setting it to `chat` is a hard error. * Don't use reserved provider ids such as `openai`, `ollama`, or `lmstudio` for `model_provider`; overrides against them are ignored. **About Responses API passthrough**

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.

**Can't connect (network restricted)?**

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).