Docs navigation

API

Messages API (Anthropic-compatible)

The Anthropic-compatible /v1/messages endpoint.

POST /v1/messages is an Anthropic-compatible endpoint. Anthropic SDKs, Claude Code, and anything else that speaks the Messages API work by swapping the base URL to https://api.tokenrouter.io without a /v1 suffix, because Anthropic clients append /v1/messages themselves.

Authentication

Either Anthropic-style x-api-key or standard Authorization: Bearer works, always with your tr_ key:

bash
curl https://api.tokenrouter.io/v1/messages \
  -H "x-api-key: tr_your_key_here" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4-5",
    "max_tokens": 300,
    "messages": [{"role": "user", "content": "Hello, Claude via TokenRouter!"}]
  }'

Verbatim passthrough to Claude

When the target model is an Anthropic model, the request body is passed through to Anthropic verbatim — tools, extended thinking, vision, system prompts, and streaming all behave exactly as they do against Anthropic directly. TokenRouter only injects your provider key, enforces your budgets and limits, and records usage metadata.

You can also point /v1/messages at non-Claude models (for example openai/gpt-5-mini or auto); TokenRouter translates the Messages shape to the provider’s native API.

Streaming

Set "stream": true for Anthropic-style SSE events (message_start, content_block_delta, message_stop, …).

Counting tokens

POST /v1/messages/count_tokens is supported with the same request shape, minus max_tokens:

bash
curl https://api.tokenrouter.io/v1/messages/count_tokens \
  -H "x-api-key: tr_your_key_here" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-sonnet-4-5",
    "messages": [{"role": "user", "content": "How many tokens is this?"}]
  }'

# → {"input_tokens": 12}
Setting up Claude Code? The exact environment variables are in the Claude Code integration guide.