Claude Code
Claude Code ↗ reads its endpoint and credentials from environment variables. This configuration sends requests to AI Gateway's Anthropic endpoint using your own Anthropic API key, passed directly in the ANTHROPIC_API_KEY environment variable. The Anthropic endpoint exposes the same /v1/messages API that Claude Code expects.
Before you start, you need:
- An authenticated gateway and its gateway token. The gateway token must have
Runpermissions. - Your Cloudflare account ID. To find it, refer to Find your account and zone IDs.
- An Anthropic API key. To create one, go to Account Settings ↗ in the Claude Console. For more information, refer to Anthropic's API overview ↗.
- Claude Code ↗ installed and updated to the latest version.
-
Set the base URL to your gateway's Anthropic endpoint, pass your Anthropic API key, and send your gateway token in the
cf-aig-authorizationheader. The following commands set these as shell environment variables for the current session. To persist them, add them to your shell profile (for example,~/.zshrcor~/.bashrc) or to Claude Code'ssettings.json↗ under theenvkey.Replace
<ACCOUNT_ID>,<GATEWAY_ID>,<ANTHROPIC_API_KEY>, and<CF_AIG_TOKEN>with your values.Terminal window export ANTHROPIC_BASE_URL="https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/anthropic"export ANTHROPIC_API_KEY="<ANTHROPIC_API_KEY>"export ANTHROPIC_CUSTOM_HEADERS="cf-aig-authorization: Bearer <CF_AIG_TOKEN>"PowerShell $env:ANTHROPIC_BASE_URL = "https://gateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/anthropic"$env:ANTHROPIC_API_KEY = "<ANTHROPIC_API_KEY>"$env:ANTHROPIC_CUSTOM_HEADERS = "cf-aig-authorization: Bearer <CF_AIG_TOKEN>" -
Start Claude Code and send a prompt. Requests now route through AI Gateway.
Terminal window claude
To confirm traffic reaches AI Gateway, refer to Verify it works.