Messages (Anthropic)
完全兼容 Anthropic Claude Messages API。
POST https://api.wrouter.io/v1/messages请求头
| Header | 必填 | 说明 |
|---|---|---|
Authorization | ✓* | Bearer sk-... |
x-api-key | ✓* | sk-...(替代 Authorization) |
anthropic-version | ✓ | 例如 2023-06-01 |
Content-Type | ✓ | application/json |
*Authorization 与 x-api-key 二选一。
请求体参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | ✓ | 例如 claude-sonnet-4-5、claude-opus-4-1 |
messages | array | ✓ | 对话消息,结构同 Anthropic 官方 |
max_tokens | integer | ✓ | 最大输出 token |
system | string | array | 系统提示 | |
temperature | number | ||
top_p | number | ||
top_k | integer | ||
stream | boolean | SSE 流式 | |
stop_sequences | array | ||
tools | array | Anthropic Tools 定义 | |
tool_choice | object | {"type":"auto"|"any"|"tool","name":"..."} | |
thinking | object | Extended thinking,例如 {"type":"enabled","budget_tokens":10000} | |
cache_control | object | Prompt 缓存,节省成本 | |
context_management | object | 长对话上下文管理策略 | |
output_config | object | 输出配置 | |
output_format | object | 输出格式约束(如结构化输出) | |
mcp_servers | array | MCP(Model Context Protocol)服务器列表 | |
container | string | object | 工具运行容器 | |
inference_geo | string | 推理区域提示 | |
metadata | object | {"user_id": "..."} | |
speed | string | "standard" | "fast" | |
service_tier | string | "auto" | "standard_only" |
详细字段语义与 Anthropic 官方文档一致。
示例
bash
curl https://api.wrouter.io/v1/messages \
-H "x-api-key: $WROUTER_API_KEY" \
-H "anthropic-version: 2023-06-01" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-5",
"max_tokens": 1024,
"messages": [{"role":"user","content":"用一句话介绍 wrouter"}]
}'Python (anthropic SDK)
python
import anthropic
client = anthropic.Anthropic(
api_key="sk-...",
base_url="https://api.wrouter.io",
)
resp = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[{"role": "user", "content": "你好"}],
)
print(resp.content[0].text)响应
与 Anthropic 官方一致:
json
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"content": [{"type":"text","text":"你好!..."}],
"model": "claude-sonnet-4-5",
"stop_reason": "end_turn",
"usage": {"input_tokens": 12, "output_tokens": 24}
}流式时按 Anthropic SSE 协议返回 message_start、content_block_delta、message_delta、message_stop 等事件。
跨厂商调用
/v1/messages 端点也允许 model 为非 Claude 模型(例如 gpt-4o),wrouter 会做协议转译。但建议非 Claude 模型使用 /v1/chat/completions 以获得最完整的特性支持。