Using an API Relay in Claude Code
Point Claude Code at a custom API base URL using environment variables or settings.json.
Claude Code can send requests to a custom API base URL — all you need to do is set the relevant environment variables or write them into a config file. This article explains how to point Claude Code at an API relay that is compatible with the Anthropic API.
What You'll Need
Before you start, get the following information from the API platform you use:
- API base URL: the root endpoint address the platform gives you
- API key: the token used for authentication
- Model name: the model identifier the platform supports and that you plan to use
Option 1: Environment Variables
Set the following environment variables in your terminal (you can write them into ~/.zshrc or ~/.bashrc to make them permanent):
export ANTHROPIC_BASE_URL="https://example.com" # Replace with the API base URL the platform provides
export ANTHROPIC_AUTH_TOKEN="your-key" # Replace with your API key
export ANTHROPIC_MODEL="your-model-name" # Replace with a model name the platform supports
After setting them, reopen your terminal and run claude for the changes to take effect.
Option 2: settings.json
You can also write them into Claude Code's config file ~/.claude/settings.json (global) or .claude/settings.json inside a project:
{
"env": {
"ANTHROPIC_BASE_URL": "https://example.com",
"ANTHROPIC_AUTH_TOKEN": "your-key",
"ANTHROPIC_MODEL": "your-model-name"
}
}
When the config contains your API key, we recommend using .claude/settings.local.json, which is kept out of version control.
Common Issues
- Getting a 404: some services are sensitive to trailing slashes; try adding or removing the trailing
/on the API base URL. - Model name does not match: Claude Code does not convert model names automatically, so set the model name to one the platform recognizes.
- No effect in GUI apps: an app launched from the Dock or from a shortcut does not read environment variables set in a terminal.
- Conflicts with a subscription: if you are also signed in to a Claude subscription account, the custom base URL overrides the subscription; remove the settings above to switch back to the official service.