Skip to content

Messages API

POST /api/v1/messages — the Anthropic-compatible Messages endpoint. Point any Anthropic SDK or Claude Code at AnyRouter by swapping the base URL.

The Messages API is an Anthropic-compatible endpoint. Any tool that speaks the Anthropic Messages protocol — including Claude Code and the official @anthropic-ai/sdk — works against AnyRouter by changing the base URL.

POST/api/v1/messages

Authenticate with an LLM API key (sk-ar-v1-…) from /dashboard/keys, passed via either Authorization: Bearer … or Anthropic's x-api-key header. Management keys (ak_…) are not accepted.

Request

Unlike the OpenAI-compatible endpoints, the Messages API expects Anthropic's auth headers.

HeaderRequiredDescription
x-api-keyyesYour AnyRouter API key (prefixed sk-ar-). Authorization: Bearer sk-ar-… is also accepted.
anthropic-versionrecommendedAnthropic API version. Dated values (YYYY-MM-DD, e.g. 2023-06-01) are accepted; a missing header is allowed.
Content-Typeyesapplication/json.

Request body fields:

FieldTypeRequiredDescription
modelstringyesAny catalog model id (e.g. anthropic/claude-sonnet-4.6, openai/gpt-4o, anyrouter/coding).
messagesarrayyes[{role, content}] — content can be a string or rich blocks.
max_tokensintegeryesRequired by the Messages API.
systemstringnoSystem prompt (Anthropic splits this out of messages).
temperaturenumberno0–1.
streambooleannoSSE streaming.
stop_sequencesstring[]noStop sequences.
toolsarraynoTool definitions in Anthropic's schema.
session_idstringnoSticky-session id (≤256 chars) that groups related requests in Request Logs. Also settable via the x-session-id header; the body field wins.

Response

{
  "id": "msg_01XY...",
  "type": "message",
  "role": "assistant",
  "model": "anthropic/claude-sonnet-4.6",
  "content": [{ "type": "text", "text": "Hello there." }],
  "stop_reason": "end_turn",
  "usage": { "input_tokens": 10, "output_tokens": 4 }
}

Supported models

The Messages endpoint accepts any model in the catalog, not just Anthropic ones. anthropic/* models are sent to Anthropic-native upstreams as a passthrough; every other model — including OpenAI-compatible providers and AnyRouter virtual models such as anyrouter/coding — is transparently translated to and from the Chat Completions dialect, so the request and response you see stay in Anthropic Messages format.

This means Anthropic-native clients (e.g. Claude Code) can target the full catalog through /messages without switching to chat completions.

Examples

curl https://anyrouter.dev/api/v1/messages \
  -H "x-api-key: sk-ar-your-key" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "anthropic/claude-haiku-4.5",
    "max_tokens": 1024,
    "messages": [
      { "role": "user", "content": "Say hi in 5 words" }
    ]
  }'
import Anthropic from "@anthropic-ai/sdk"

const client = new Anthropic({
  baseURL: "https://anyrouter.dev/api",
  apiKey: "sk-ar-your-key",
})

const response = await client.messages.create({
  model: "anthropic/claude-sonnet-4.6",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Hello!" }],
})

For the Anthropic SDK the base URL is https://anyrouter.dev/api — the SDK appends /v1/messages itself.

Errors

Errors use the standard AnyRouter envelope. An unknown provider or an unrouteable model returns unknown_provider or no_upstream:

{
  "error": {
    "code": "no_upstream",
    "message": "No upstream is available for \"<model>\".",
    "metadata": { "type": "upstream_error" }
  }
}
StatusMeaningFix
400Malformed body, or max_tokens missing.max_tokens is required by the Messages API.
401Missing or invalid key.Send a valid sk-ar-… key via x-api-key or Authorization.
404Unknown model or no upstream.Verify the id with List models.

See Errors for the full status table and retry guidance.

Related