Claude Messages
Compatible with Anthropic Messages. You can use the Anthropic SDK and Claude Code directly. For models in the claude group.
https://api.codingrouter.ai/v1/messagesAuthentication
| Header | Description |
|---|---|
x-api-key: <token> | The official Anthropic form; Authorization: Bearer <token> also works |
anthropic-version: 2023-06-01 | Recommended; 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
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model name, such as claude-sonnet-5-5 |
messages | array | Yes | Conversation messages, role is user or assistant |
max_tokens | integer | Recommended | Maximum number of tokens to generate; the gateway uses a default if omitted |
system | string / array | No | System prompt; with an array you can add cache_control |
stream | boolean | No | When true, returns an SSE event stream |
temperature | number | No | 0 to 1 |
top_p, top_k | number | No | Sampling parameters |
stop_sequences | array | No | Stop when these strings appear |
thinking | object | No | Extended thinking: {"type": "enabled", "budget_tokens": 2000} |
tools | array | No | Callable tools |
tool_choice | object | No | {"type": "auto"}, {"type": "any"} or a specific tool |
Request example
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"}]
}'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)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
{
"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
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
{
"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:
{
"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
{
"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
{
"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.
