Configure OpenCode with XAI Router

Posted January 10, 2026 by The XAI Tech Teamย โ€ย 2ย min read

OpenCode (opencode) is a developer-friendly coding assistant for the terminal and beyond. This guide shows you how to route opencode through XAI Router (xairouter).


Prerequisites

  1. An XAI Router account: Sign up at m.xairouter.com and create an API Key.
  2. opencode installed locally.

Step 1: Create an API Key in XAI Router

  1. Log in to m.xairouter.com.
  2. Go to API Keys and create a new key (use a label like opencode).
  3. Copy the key. Youโ€™ll use it as XAI_API_KEY.

Step 2: Set the environment variable

We recommend XAI_API_KEY.

macOS / Linux:

export XAI_API_KEY="sk-xxx"

Windows PowerShell:

$env:XAI_API_KEY="sk-xxx"

Step 3: Configure opencode

Create or overwrite ~/.config/opencode/opencode.json:

cat > ~/.config/opencode/opencode.json << 'EOF'
{
  "$schema": "https://opencode.ai/config.json",
  "provider": {
    "XAI": {
      "options": {
        "baseURL": "https://api.xairouter.com/v1",
        "apiKey": "{env:XAI_API_KEY}"
      },
      "models": {
        "glm-4.7": {},
        "ark-code-latest": {},
        "MiniMax-M2.1": {},
        "gemini-3-pro-preview": {}
      }
    }
  },
  "plugin": ["oh-my-opencode"]
}
EOF

Note: We use the XAI provider and configure models like glm-4.7, MiniMax-M2.1, and gemini-3-pro-preview.


Step 4: Validate

opencode debug config

Verify that the configured models appear in the output.


With the setup above, your opencode runs through XAI Router with centralized key management, observability, and cost control.