How to use an OpenRouter API key in Claude Code
Originally published at alwaysagent.app . The original is kept up to date. OpenRouter has an official Claude Code integration. Four environment variables and a logout are all it takes. This guide covers the full config, verification, model switching, costs, troubleshooting, server setups, and the open-source Claude Code Router. The short answer: four variables To use an OpenRouter API key in…
To utilize an OpenRouter API key within Claude Code, you must configure four environment variables and log out of any Anthropic account. First, set ANTHROPIC_BASE_URL to https://openrouter.ai/api, followed by assigning your API key to ANTHROPIC_AUTH_TOKEN. Next, ensure ANTHROPIC_API_KEY is left empty as it is treated as a direct Anthropic credential and should not be included in the key. Finally, log out of any Anthropic account. These steps are outlined in OpenRouter's official Claude Code integration guide.
The base URL for OpenRouter is https://openrouter.ai/api, without the /v1. The API key is stored in the ANTHROPIC_AUTH_TOKEN variable, which Claude Code sends as Authorization: Bearer. Importantly, ANTHROPIC_API_KEY should remain empty as it is considered a direct Anthropic credential and should not be sent. The ANTHROPIC_API_KEY is sent as x-api-key and must stay blank.
There are two methods to configure these variables: using shell profile exports or a settings.json file. Shell variables are recommended for quick testing, while settings.json is preferred for persistent configurations. The shell profile method involves adding the exports to ~/.zshrc, ~/.bashrc, or your fish config and reloading the profile. Alternatively, you can edit ~/.claude/settings.json for project-wide configurations or .claude/settings.local.json for specific projects.
Once configured, verify the setup by running /status within Claude Code. The output should display Auth token: ANTHROPIC_AUTH_TOKEN and Anthropic base URL: https://openrouter.ai/api. If the output displays ANTHROPIC_API_KEY or a Claude account, reload the shell and restart Claude Code. Sending a prompt and checking the dashboard in the OpenRouter Activity dashboard will confirm successful configuration, with the model name appearing in the request log.
Rewritten by urgent.news; may contain errors — check the original.
Source: dev.to