Back to docs

Claude Code setup

Set up Claude Code with Virouter API

Use Virouter as the Anthropic-compatible LLM gateway for Claude Code. Claude Code sends Anthropic Messages requests to Virouter, and Virouter routes them through your Billing USD Quota wallet.

Important: Set ANTHROPIC_BASE_URL=https://api.virouter.com. The OpenAI SDK base URL https://api.virouter.com/v1is for OpenAI-compatible SDKs, not Claude Code. Virouter's Claude surface may route to Anthropic Messages-compatible upstreams today, and to Claude Code CLI-only upstreams after the dedicated bridge is enabled.

One-command setup

Paste your Virouter API key into the command below. The installer updates ~/.claude/settings.json, preserves any existing settings, and creates a timestamped backup before writing.

# Windows Command Prompt / PowerShell
powershell -NoProfile -ExecutionPolicy Bypass -Command "iwr https://virouter.com/install/claude-code.ps1 -OutFile $env:TEMP\virouter-claude-code.ps1; & $env:TEMP\virouter-claude-code.ps1 -ApiKey 'vr_sk_xxxxxxxxxxxx'"

# macOS / Linux / Git Bash
curl -fsSL https://virouter.com/install/claude-code.sh | sh -s -- vr_sk_xxxxxxxxxxxx

# Then restart Claude Code
claude

The script does not print your full key. You can also run it with VIROUTER_API_KEY if you prefer not to put the key in shell history.

export VIROUTER_API_KEY="vr_sk_xxxxxxxxxxxx"
curl -fsSL https://virouter.com/install/claude-code.sh | sh

Manual setup

Run Claude Code from a terminal where these environment variables are set.

export ANTHROPIC_BASE_URL="https://api.virouter.com"
export ANTHROPIC_AUTH_TOKEN="$VIROUTER_API_KEY"

# Optional: let Claude Code list Claude models exposed by Virouter
export CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1

claude

Setup checklist

1

Create a Virouter API key

Generate a server-side key in the Virouter dashboard and keep it secret.

2

Run the installer

The installer writes Anthropic gateway settings to ~/.claude/settings.json and creates a timestamped backup if the file already exists.

3

Check model discovery

The installer enables CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 so Claude Code can list active Virouter Claude models.

4

Run Claude Code

Start Claude Code from a new terminal so it reads the updated settings file.

Claude Code settings file option

If you prefer Claude Code settings over shell exports, store the same variables in your Claude Code settings. Keep the API key out of git and team-shared dotfiles. You can also download the installer script and inspect it before running.

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://api.virouter.com",
    "ANTHROPIC_AUTH_TOKEN": "vr_sk_xxxxxxxxxxxx",
    "CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY": "1"
  }
}

Verify with the Anthropic Messages endpoint

Claude Code sends Anthropic Messages requests. You can smoke test the same route with cURL before starting Claude Code.

curl https://api.virouter.com/v1/messages \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $VIROUTER_API_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -d '{
    "model": "claude-sonnet-4-6",
    "max_tokens": 128,
    "messages": [{"role":"user","content":"Say ok"}]
  }'

Model discovery

Claude Code can discover gateway models from /v1/models when CLAUDE_CODE_ENABLE_GATEWAY_MODEL_DISCOVERY=1 is set. Virouter returns only active models allowed for your key.

curl https://api.virouter.com/v1/models \
  -H "Authorization: Bearer $VIROUTER_API_KEY"

Billing and usage visibility

Claude Code requests through Virouter charge your Billing USD Quota and appear in the Virouter dashboard usage log. Successful responses include gateway cost and token headers such as x-virouter-cost-usd, x-virouter-remaining-usd, and token breakdown headers.

Troubleshooting

401 unauthorized

Your Virouter key is missing, invalid, revoked, or not exported as ANTHROPIC_AUTH_TOKEN.

404 not found

Use ANTHROPIC_BASE_URL=https://api.virouter.com. Do not include /v1 in this variable for Claude Code.

402 payment required

Your Billing USD Quota is too low for Claude Code's estimated request. Top up or reduce usage.

503 upstream unavailable

No healthy Anthropic lane is available for the selected Claude model. Retry or choose another active model.

Using the OpenAI SDK instead? Read OpenAI SDK baseURL for Virouter. Want Claude through OpenAI-compatible chat completions? See Claude with OpenAI SDK.