Skip to content

Claude Messages ​

Compatible with Anthropic Messages. You can use the Anthropic SDK and Claude Code directly. For models in the claude group.

POSThttps://api.codingrouter.ai/v1/messages

Authentication ​

HeaderDescription
x-api-key: <token>The official Anthropic form; Authorization: Bearer <token> also works
anthropic-version: 2023-06-01Recommended; SDKs add it automatically

Base URL without /v1

The Anthropic SDK and Claude Code append /v1/messages themselves, so set the Base URL to https://api.codingrouter.ai.

Request parameters ​

ParameterTypeRequiredDescription
modelstringYesModel name, such as claude-sonnet-5-5
messagesarrayYesConversation messages, role is user or assistant
max_tokensintegerRecommendedMaximum number of tokens to generate; the gateway uses a default if omitted
systemstring / arrayNoSystem prompt; with an array you can add cache_control
streambooleanNoWhen true, returns an SSE event stream
temperaturenumberNo0 to 1
top_p, top_knumberNoSampling parameters
stop_sequencesarrayNoStop when these strings appear
thinkingobjectNoExtended thinking: {"type": "enabled", "budget_tokens": 2000}
toolsarrayNoCallable tools
tool_choiceobjectNo{"type": "auto"}, {"type": "any"} or a specific tool

Request example ​

bash
curl https://api.codingrouter.ai/v1/messages \
  -H "x-api-key: $CODINGROUTER_KEY" \
  -H "anthropic-version: 2023-06-01" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-5-5",
    "max_tokens": 1024,
    "messages": [{"role": "user", "content": "Explain what an API gateway is in one sentence"}]
  }'
python
import anthropic

client = anthropic.Anthropic(
    base_url="https://api.codingrouter.ai",
    api_key="sk-your-token",
)

msg = client.messages.create(
    model="claude-sonnet-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Explain what an API gateway is in one sentence"}],
)
print(msg.content[0].text)
js
import Anthropic from "@anthropic-ai/sdk";

const client = new Anthropic({
  baseURL: "https://api.codingrouter.ai",
  apiKey: process.env.CODINGROUTER_KEY,
});

const msg = await client.messages.create({
  model: "claude-sonnet-5-5",
  max_tokens: 1024,
  messages: [{ role: "user", content: "Explain what an API gateway is in one sentence" }],
});
console.log(msg.content[0].text);

Response example ​

json
{
  "id": "msg_...",
  "type": "message",
  "role": "assistant",
  "model": "claude-sonnet-5-5",
  "content": [{"type": "text", "text": "An API gateway is the unified entry point between clients and backend services..."}],
  "stop_reason": "end_turn",
  "usage": {
    "input_tokens": 50,
    "cache_creation_input_tokens": 0,
    "cache_read_input_tokens": 0,
    "output_tokens": 70
  }
}

stop_reason: end_turn normal end, max_tokens limit reached, tool_use a tool call is requested, stop_sequence a stop sequence was hit.

Streaming ​

python
with client.messages.stream(
    model="claude-sonnet-5-5",
    max_tokens=1024,
    messages=[{"role": "user", "content": "Write a short poem about the starry sky"}],
) as stream:
    for text in stream.text_stream:
        print(text, end="", flush=True)

The events come in this order: message_start, content_block_start, content_block_delta (text increments), content_block_stop, message_delta (with stop_reason and usage), message_stop.

Extended thinking ​

json
{
  "model": "claude-opus-5-5",
  "max_tokens": 4000,
  "thinking": {"type": "enabled", "budget_tokens": 2000},
  "messages": [{"role": "user", "content": "Design a rate-limiting algorithm"}]
}

content first contains a {"type": "thinking", ...} block, then a text block. max_tokens must be greater than budget_tokens, and thinking is billed as output tokens.

Prompt caching ​

Add cache_control to system or a message block. Later requests with the same prefix are billed at the cache read price:

json
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "system": [
    {"type": "text", "text": "<a very long system prompt or document>", "cache_control": {"type": "ephemeral"}}
  ],
  "messages": [{"role": "user", "content": "Answer based on the document: ..."}]
}

In the first request, usage.cache_creation_input_tokens is the number of tokens written to the cache. Later requests within 5 minutes show the number of hits in usage.cache_read_input_tokens. The cached prefix must be at least 1024 tokens to take effect.

Image input ​

json
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "messages": [{
    "role": "user",
    "content": [
      {"type": "image", "source": {"type": "base64", "media_type": "image/png", "data": "iVBORw0KGgo..."}},
      {"type": "text", "text": "What color is this image?"}
    ]
  }]
}

Tool calls ​

json
{
  "model": "claude-sonnet-5-5",
  "max_tokens": 1024,
  "messages": [{"role": "user", "content": "What is the weather in Beijing today?"}],
  "tools": [{
    "name": "get_weather",
    "description": "Get the weather of a city",
    "input_schema": {
      "type": "object",
      "properties": {"city": {"type": "string"}},
      "required": ["city"]
    }
  }]
}

When the model calls a tool, content contains {"type": "tool_use", "id": "toolu_...", "name": "get_weather", "input": {"city": "Beijing"}} and stop_reason is tool_use. Put the result in a tool_result block in the next user message to continue.