Developer setup

How to Use Claude Code with a Third-Party API Provider

Configure Claude Code with an Anthropic Base URL, environment variables, or CC Switch. Verify your first request, troubleshoot errors, and migrate safely.

What you need before changing providers

Claude Code can send requests to a third-party gateway that supports the required Anthropic API format. You need a working Claude Code CLI, a provider credential, an Anthropic-compatible base URL, an enabled Claude model, and available account credit. An OpenAI Chat Completions endpoint alone is insufficient for this configuration.

This tutorial uses API Route’s https://global.api-route.com endpoint and claude-sonnet-4-6 as an explicit model example. Check current Pricing and your key’s available models first. Create a key through API Access after registration. Start with a small test budget.

Configuration references checked October 17, 2026: Anthropic’s gateway connection guide, gateway compatibility requirements, and model configuration. If you need the CLI, use the official installation instructions, then run claude --version.

Use the Anthropic Base URL, without a duplicate /v1

For API Route, set ANTHROPIC_BASE_URL=https://global.api-route.com. Claude Code appends paths such as /v1/messages. Do not use the website address or append /v1/chat/completions. A URL that already includes the wrong protocol path can produce a 404 even when the credential is valid.

ANTHROPIC_AUTH_TOKEN sends a Bearer token in the Authorization header. ANTHROPIC_API_KEY sends x-api-key. The examples below use the Bearer configuration generated by API Route’s CC Switch export. Use one credential method deliberately and remove a conflicting old credential from the current shell.

1. Configure a fresh terminal session

First record your existing provider settings so you can restore them. Back up ~/.claude/settings.json if it exists; keep the backup private because it may contain a credential. Inspect existing shell variables and project, user, and managed settings locally. Do not paste their secrets into an issue.

macOS / Linux: Bash or Zsh

unset ANTHROPIC_API_KEY CLAUDE_CODE_OAUTH_TOKEN
export ANTHROPIC_BASE_URL='https://global.api-route.com'
read -rsp 'API Route key: ' ANTHROPIC_AUTH_TOKEN; printf '\n'
export ANTHROPIC_AUTH_TOKEN
export ANTHROPIC_MODEL='claude-sonnet-4-6'
export ANTHROPIC_DEFAULT_SONNET_MODEL="$ANTHROPIC_MODEL"

Windows: PowerShell 7

Remove-Item Env:ANTHROPIC_API_KEY, Env:CLAUDE_CODE_OAUTH_TOKEN -ErrorAction SilentlyContinue
$env:ANTHROPIC_BASE_URL = 'https://global.api-route.com'
$env:ANTHROPIC_AUTH_TOKEN = Read-Host 'API Route key' -MaskInput
$env:ANTHROPIC_MODEL = 'claude-sonnet-4-6'
$env:ANTHROPIC_DEFAULT_SONNET_MODEL = $env:ANTHROPIC_MODEL

These settings apply to this terminal and processes started from it. A new terminal does not inherit changes made in another running terminal. Pin any additional Haiku or Opus aliases to models your route actually supports before a workflow invokes them; see the official model configuration reference. Mapping every alias to one model changes model selection and should be an intentional choice.

2. Verify the Messages endpoint before launching Claude Code

Run this small paid request from the configured terminal. It requires curl and jq on Bash; PowerShell has its own version below. A reply with a content array and usage object verifies basic Messages access. It does not test tools or streaming.

curl --fail-with-body -sS "$ANTHROPIC_BASE_URL/v1/messages" \
  -H "Authorization: Bearer $ANTHROPIC_AUTH_TOKEN" \
  -H 'anthropic-version: 2023-06-01' \
  -H 'content-type: application/json' \
  -d "$(jq -nc --arg model "$ANTHROPIC_MODEL" \
    '{model:$model,max_tokens:32,messages:[{role:"user",content:"Reply with OK."}]}')"
$body = @{
  model = $env:ANTHROPIC_MODEL
  max_tokens = 32
  messages = @(@{ role = 'user'; content = 'Reply with OK.' })
} | ConvertTo-Json -Depth 5
Invoke-RestMethod -Method Post -Uri "$env:ANTHROPIC_BASE_URL/v1/messages" -Headers @{
  Authorization = "Bearer $env:ANTHROPIC_AUTH_TOKEN"
  'anthropic-version' = '2023-06-01'
} -ContentType 'application/json' -Body $body

Then run claude --model claude-sonnet-4-6. Use /status to inspect the active model and credential source, then ask for a short reply. Match the request time and model against Usage Logs. If the gateway or CLI does not expose an exact upstream identity, a displayed model name alone is not proof of the underlying route.

3. Use CC Switch when you want saved provider profiles

CC Switch is an independent community desktop tool that manages provider configurations. Install it from the project’s own release channel and review what it stores. It is optional; the shell setup above is enough to test a route.

  1. Open API Route’s API Access page and select your verified key, an available Claude model, and the endpoint.
  2. Select Claude Code as the target app. Use the CC Switch import action and confirm the imported provider’s base URL, model, and credential field locally.
  3. Activate the imported provider in CC Switch. For a manual profile, choose Claude Code, add a provider, and enter the base URL and key with the equivalent env configuration.
  4. Close and reopen Claude Code. Clear stale shell overrides before testing the saved profile, since shell variables can override file settings.
  5. Run a short request and verify its charge in API Route Usage Logs. Avoid running multiple profile managers against the same settings file.

For manual persistence, merge the following into your private user ~/.claude/settings.json (Windows: %USERPROFILE%\.claude\settings.json). Preserve unrelated settings; do not replace the whole file blindly. Substitute your key locally and keep this file out of version control.

{
  "env": {
    "ANTHROPIC_BASE_URL": "https://global.api-route.com",
    "ANTHROPIC_AUTH_TOKEN": "REPLACE_WITH_YOUR_KEY",
    "ANTHROPIC_MODEL": "claude-sonnet-4-6",
    "ANTHROPIC_DEFAULT_SONNET_MODEL": "claude-sonnet-4-6"
  }
}

The CLI and editor integrations may read configuration differently. For example, Anthropic documents VS Code’s claudeCode.environmentVariables setting for the extension. Verify that specific integration instead of assuming an already-running editor inherited terminal exports.

Troubleshoot by response and active configuration

Claude Code gateway troubleshooting
SymptomWhat to check next
401 / authentication failedCheck that the key belongs to the target provider, is active, and is sent in the expected header. Remove stale API key or OAuth overrides. Never print the key.
403 / access deniedCheck key permissions, allowed model/group, account restrictions, and provider policy. Repeating the request will not fix a permission denial.
404 / not foundCheck the URL for a doubled /v1 or Chat Completions path. Confirm the exact model identifier with your provider.
Insufficient creditCheck account credit and key quota. Inspect the actual response; the status code varies by gateway. Choose a small top-up only after verifying the route and price.
429 / rate limitReduce concurrency and follow Retry-After when provided. Check account/key limits; credit balance alone does not determine throughput.
400 on tools, thinking, or beta featuresConfirm gateway support for the request fields and beta headers. Reproduce with a minimal request and add required features one at a time.
Text works, streaming or tools failTest SSE and a harmless read-only tool separately. Check proxy buffering, event format, tool results, and timeout behavior.
Login prompt or unexpected providerInspect /status and settings precedence. Restart the CLI or extension after configuration changes; a saved login does not establish which credential is active.

Capture a timestamp, HTTP status, sanitized error body, and request ID when available. Share those with the provider, without credentials or source code. The compatibility guide explains required headers and feature forwarding. Third-party routing can lag new Claude Code features.

Migrate gradually and keep a rollback path

  1. Save the old provider profile and a private backup of your settings. Record the old model selection and any shell startup entries.
  2. Test the new route with a short text request, streaming, a harmless tool call, and a representative repository task. Review the cost comparison and your actual charges.
  3. Measure repeated context with the cache verification guide. Do not assume caches or credits transfer between providers.
  4. Move a limited workload first. Keep credentials separate and revoke an old key only when nothing still needs it.
  5. To roll back, deactivate the new CC Switch profile or restore the previous user settings, remove the new variables from your shell and startup files, and restart the client. Recheck /status and billing logs.

Third-party credentials bill the provider account separately from a Claude app subscription. Review the provider’s data handling and routing constraints for your repository. API compatibility does not guarantee identical support, tools, limits, or service terms.

Create an API Route account, check the price for your intended model, and verify one request before funding ongoing use. The quickstart covers the account and first-call workflow.