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:

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

Get Started with 轻舟 AI

Stable, fast AI API relay — supports Claude, OpenAI, Gemini and more

Sign Up Free