Messages
Claude API Messages endpoint - Create Claude 3.5 Sonnet, Claude 3 Opus conversations through UniterRouter. Anthropic-compatible API with streaming and tool use support.
Messages API
Create a message using Claude models. This endpoint provides native Anthropic API compatibility for applications that prefer the Anthropic SDK.
Alternative: You can also access Claude models through the OpenAI-compatible API, which is recommended for most use cases.
Endpoint
POST https://api.uniterouter.ai/v1/messagesAuthentication
UniterRouter supports two authentication methods:
Option 1: Anthropic Native (x-api-key)
x-api-key: your_uniterouter_api_key
anthropic-version: 2023-06-01Option 2: Bearer Token
Authorization: Bearer your_uniterouter_api_key
anthropic-version: 2023-06-01Both authentication methods work identically. Use x-api-key for Anthropic SDK compatibility, or Bearer for tools like Claude Code.
Request Body
UniterRouter 完全兼容 Anthropic 官方 API,支持所有参数。
基础参数
| Parameter | Type | Required | Description |
|---|---|---|---|
model | string | Yes | Model ID (e.g., claude-sonnet-4-20250514) |
messages | array | Yes | Array of message objects |
max_tokens | integer | Yes | Maximum tokens to generate |
系统提示
| Parameter | Type | Required | Description |
|---|---|---|---|
system | string | array | No | System prompt. Can be a string or array of TextBlockParam |
采样参数
| Parameter | Type | Required | Description |
|---|---|---|---|
temperature | number | No | Sampling temperature (0.0-1.0, default: 1.0) |
top_p | number | No | Nucleus sampling parameter |
top_k | integer | No | Top-k sampling parameter |
stop_sequences | array | No | Custom stop sequences |
流式与元数据
| Parameter | Type | Required | Description |
|---|---|---|---|
stream | boolean | No | Enable streaming (default: false) |
metadata | object | No | Request metadata with optional user_id |
service_tier | string | No | Service tier: auto or standard_only |
工具调用
| Parameter | Type | Required | Description |
|---|---|---|---|
tools | array | No | Tool definitions (custom, bash, text_editor, web_search) |
tool_choice | object | No | Tool selection: auto, any, tool, or none |
扩展思考 (Extended Thinking)
| Parameter | Type | Required | Description |
|---|---|---|---|
thinking | object | No | Extended thinking configuration |
Thinking Configuration:
{
"thinking": {
"type": "enabled",
"budget_tokens": 10000
}
}Extended thinking allows Claude to show its reasoning process. Requires budget_tokens ≥ 1024 and less than max_tokens.
Message Format
{
"messages": [
{
"role": "user",
"content": "Hello, Claude!"
}
]
}Message with Images
{
"messages": [
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZJRg..."
}
},
{
"type": "text",
"text": "What's in this image?"
}
]
}
]
}Example Request
curl https://api.uniterouter.ai/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: $UNITEROUTER_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"system": "You are a helpful assistant.",
"messages": [
{"role": "user", "content": "Hello, Claude!"}
]
}'import anthropic
client = anthropic.Anthropic(
api_key="your_uniterouter_api_key",
base_url="https://api.uniterouter.ai"
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
system="You are a helpful assistant.",
messages=[
{"role": "user", "content": "Hello, Claude!"}
]
)
print(message.content[0].text)import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: process.env.UNITEROUTER_API_KEY,
baseURL: "https://api.uniterouter.ai"
});
const message = await client.messages.create({
model: "claude-sonnet-4-20250514",
max_tokens: 1024,
system: "You are a helpful assistant.",
messages: [
{ role: "user", content: "Hello, Claude!" }
]
});
console.log(message.content[0].text);Response
{
"id": "msg_01XFDUDYJgAACzvnptvVoYEL",
"type": "message",
"role": "assistant",
"content": [
{
"type": "text",
"text": "Hello! How can I help you today?"
}
],
"model": "claude-3-5-sonnet-20241022",
"stop_reason": "end_turn",
"stop_sequence": null,
"usage": {
"input_tokens": 25,
"output_tokens": 15
}
}Streaming
Enable streaming for real-time responses:
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Tell me a story"}]
) as stream:
for text in stream.text_stream:
print(text, end="", flush=True)Stream Events
| Event Type | Description |
|---|---|
message_start | Message creation started |
content_block_start | Content block started |
content_block_delta | Content chunk received |
content_block_stop | Content block finished |
message_delta | Message metadata update |
message_stop | Message completed |
Tool Use (Function Calling)
Claude supports tool use for structured interactions:
tools = [
{
"name": "get_weather",
"description": "Get the current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "City name"
}
},
"required": ["location"]
}
}
]
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=tools,
messages=[
{"role": "user", "content": "What's the weather in Paris?"}
]
)
# Check if tool was called
for block in message.content:
if block.type == "tool_use":
print(f"Tool: {block.name}")
print(f"Input: {block.input}")Vision
Send images to Claude for analysis:
import base64
with open("image.jpg", "rb") as f:
image_data = base64.standard_b64encode(f.read()).decode("utf-8")
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{
"role": "user",
"content": [
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": image_data
}
},
{
"type": "text",
"text": "Describe this image in detail."
}
]
}
]
)Available Models
| Model | Description | Context | Max Output |
|---|---|---|---|
claude-sonnet-4-20250514 | Latest Claude 4 Sonnet | 200K | 64K |
claude-opus-4-20250514 | Most powerful Claude 4 | 200K | 32K |
claude-3-7-sonnet-20250219 | Claude 3.7 with extended thinking | 200K | 128K |
claude-3-5-sonnet-20241022 | Claude 3.5 Sonnet | 200K | 8K |
claude-3-5-haiku-20241022 | Fast and affordable | 200K | 8K |
claude-3-opus-20240229 | Claude 3 Opus | 200K | 4K |
claude-3-haiku-20240307 | Previous Haiku | 200K | 4K |
Error Codes
| Code | Description |
|---|---|
400 | Invalid request parameters |
401 | Invalid API key |
403 | Permission denied |
404 | Model not found |
429 | Rate limit exceeded |
500 | Internal server error |
529 | API overloaded |
Related
- Chat Completions (OpenAI) - OpenAI-compatible endpoint
- Vision - Image input guide
- Reasoning - Extended thinking
How is this guide?
Anthropic API
Claude API integration - Access Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku through UniterRouter. Native Anthropic SDK support with tool use and vision capabilities.
Google Compatible
Google Gemini API proxy - Access Gemini Pro, Gemini 1.5 Pro, Gemini Flash through UniterRouter. Native Google AI SDK support with multimodal, grounding and code execution.