Setting ANTHROPIC_BASE_URL for Claude Code
How to Configure ANTHROPIC_BASE_URL for Custom Endpoints
The ANTHROPIC_BASE_URL environment variable controls where your Claude API requests are sent. By default, the Anthropic SDK sends requests to https://api.anthropic.com. Changing this variable lets you route requests through relay services, proxies, or self-hosted gateways without modifying any application code.
Why Change the Base URL?
There are several important reasons to use a custom base URL:
- API relay services — Route through services like claude4u.com for multi-account pooling, failover, and cost tracking.
- Corporate proxies — Route API traffic through your organization's proxy infrastructure for compliance and monitoring.
- Regional optimization — Connect through a geographically closer relay endpoint for reduced latency.
- Development and testing — Point to a mock server or local proxy during development.
- Access management — Use a relay that enforces additional access controls, model restrictions, or spending limits.
Setting the Base URL
Environment Variable (Recommended)
# For relay services like claude4u.com
export ANTHROPIC_BASE_URL="https://claude4u.com/antigravity"
export ANTHROPIC_API_KEY="cr_your_relay_key_here"
# Add to shell profile for persistence
echo 'export ANTHROPIC_BASE_URL="https://claude4u.com/antigravity"' >> ~/.zshrc
echo 'export ANTHROPIC_API_KEY="cr_your_relay_key_here"' >> ~/.zshrc
source ~/.zshrc
In the Anthropic Python SDK
import anthropic
# Option 1: SDK reads from environment automatically
client = anthropic.Anthropic()
# Option 2: Set explicitly in code
client = anthropic.Anthropic(
base_url="https://claude4u.com/antigravity",
api_key="cr_your_relay_key_here"
)
# Option 3: Using httpx client for advanced proxy config
import httpx
client = anthropic.Anthropic(
base_url="https://claude4u.com/antigravity",
http_client=httpx.Client(proxy="http://corporate-proxy:8080")
)
In the Anthropic Node.js SDK
import Anthropic from '@anthropic-ai/sdk';
// Option 1: Reads ANTHROPIC_BASE_URL from environment
const client = new Anthropic();
// Option 2: Set explicitly
const client = new Anthropic({
baseURL: 'https://claude4u.com/antigravity',
apiKey: 'cr_your_relay_key_here',
});
For Claude Code CLI
# Claude Code reads these environment variables automatically
export ANTHROPIC_BASE_URL="https://claude4u.com/antigravity"
export ANTHROPIC_API_KEY="cr_your_relay_key_here"
# Then launch Claude Code normally
claude
Tip: When using Claude Code with a relay service, you get the same full functionality — file editing, command execution, git operations — but with the added benefits of multi-account pooling and automatic failover.
For curl Requests
# Direct API
curl https://api.anthropic.com/v1/messages \
-H "x-api-key: sk-ant-your-key" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":100,"messages":[{"role":"user","content":"Hello"}]}'
# Through relay (same request, different URL and key)
curl https://claude4u.com/antigravity/v1/messages \
-H "x-api-key: cr_your_relay_key" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":100,"messages":[{"role":"user","content":"Hello"}]}'
Configuration for Different Environments
Use different base URLs for different environments to separate development and production traffic:
# .env.development
ANTHROPIC_BASE_URL=http://localhost:3000/mock-claude
ANTHROPIC_API_KEY=test-key-dev
# .env.staging
ANTHROPIC_BASE_URL=https://claude4u.com/antigravity
ANTHROPIC_API_KEY=cr_staging_key
# .env.production
ANTHROPIC_BASE_URL=https://claude4u.com/antigravity
ANTHROPIC_API_KEY=cr_production_key
Docker and Docker Compose
# docker-compose.yml
services:
app:
environment:
ANTHROPIC_BASE_URL: "https://claude4u.com/antigravity"
ANTHROPIC_API_KEY: "${ANTHROPIC_API_KEY}"
Verifying Your Configuration
Confirm your base URL is set correctly:
# Check the environment variable
echo $ANTHROPIC_BASE_URL
# Test the connection
curl -s -o /dev/null -w "%{http_code}" \
https://claude4u.com/antigravity/v1/messages \
-H "x-api-key: $ANTHROPIC_API_KEY" \
-H "content-type: application/json" \
-H "anthropic-version: 2023-06-01" \
-d '{"model":"claude-sonnet-4-20250514","max_tokens":10,"messages":[{"role":"user","content":"ping"}]}'
Warning: Always use HTTPS for your base URL in production. HTTP connections transmit your API key in plain text over the network, creating a serious security risk.
Troubleshooting
- "Connection refused" — Verify the URL is correct and the relay service is running.
- "401 Unauthorized" — Ensure your API key matches the relay service, not the direct Anthropic key.
- "SSL certificate error" — Check that the relay service has a valid SSL certificate.
- Timeouts — If using a corporate proxy, ensure both the proxy and relay endpoints are reachable.
Get Started with 轻舟 AI
Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more
Sign Up Free
轻舟 AI