Skip to main content
Watch: I Built an AI App Without Leaving Databricks

Setup guide - Documented by OpenRouter

Use OpenRouter in Codex CLI

OpenRouter's official Codex CLI guide adds an openrouter model_provider to ~/.codex/config.toml with base_url https://openrouter.ai/api/v1. The key comes from a command-based auth block, which also makes Codex fetch the OpenRouter model catalog.

Last verified 7 steps1 official source

Steps

  1. Step 1.

    Install Codex CLI by following the instructions in the openai/codex GitHub repo.

  2. Step 2.

    Create an OpenRouter API key at openrouter.ai/keys (it starts with sk-or-...).

  3. Step 3.

    Create or edit ~/.codex/config.toml.

    TOML
    model_provider = "openrouter"
    model_reasoning_effort = "high"
    model="~openai/gpt-sol-latest"
    
    [model_providers.openrouter]
    name = "openrouter"
    base_url="https://openrouter.ai/api/v1"
    
    [model_providers.openrouter.auth]
    command = "sh"
    args = ["-c", "echo $OPENROUTER_API_KEY"]
  4. Step 4.

    On Windows, use the PowerShell auth block instead.

    TOML
    [model_providers.openrouter.auth]
    command = "powershell"
    args = ["-NoProfile", "-Command", "Write-Output $env:OPENROUTER_API_KEY"]
  5. Step 5.

    Export your key in your shell profile (~/.zshrc, ~/.bashrc, or ~/.config/fish/config.fish).

    Terminal
    export OPENROUTER_API_KEY="sk-or-..."
  6. Step 6.

    On Windows, set it as a user-level variable, then fully restart Codex.

    Terminal
    setx OPENROUTER_API_KEY "sk-or-..."
  7. Step 7.

    Start Codex in your project.

    Terminal
    cd /path/to/your/project
    codex

Caveats

  • A plain env_key = "OPENROUTER_API_KEY" also authenticates, but Codex then won't fetch the OpenRouter model catalog, and non-OpenAI models show the "Unknown model" fallback-metadata warning. OpenRouter recommends the command-based auth block.
  • A 401 with "Missing Authentication header" usually means the auth command failed to run.
  • model_reasoning_effort accepts "low", "medium", "high", "xhigh", "max" per the OpenRouter guide.
  • The Codex desktop app (ChatGPT for macOS and Windows) has a separate OpenRouter guide.

FAQ

How do I use OpenRouter in Codex CLI?

OpenRouter's official Codex CLI guide adds an openrouter model_provider to ~/.codex/config.toml with base_url https://openrouter.ai/api/v1. The key comes from a command-based auth block, which also makes Codex fetch the OpenRouter model catalog.

Which OpenRouter model ids work in Codex CLI?

The official docs read for this guide list ~openai/gpt-sol-latest and ~anthropic/claude-sonnet-latest. Requests go to https://openrouter.ai/api/v1.

Is this an official OpenRouter and Codex CLI setup?

OpenRouter documents this setup in its own docs. It was last checked against 1 official page on October 17, 2026.

Sources

Last verified against 1 official page.

Newsletter

New models, set up the day they ship

One email per week. New model releases, the coding tools that support them and how to wire them up.