Claude 原生接口
本平台提供 Anthropic Messages API 兼容接口,可直接使用 Claude SDK 接入。
1. 基础信息
- API 基础地址:
https://router.qingting.work - Messages API:
POST /v1/messages - Token 计数:
POST /v1/messages/count_tokens
2. 鉴权方式
支持两种认证方式:
Authorization: Bearer your-api-key
或
x-api-key: your-api-key
3. Messages API
请求示例
bash
curl -X POST https://router.qingting.work/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key" \
-H "anthropic-version: 2023-06-01" \
-d '{
"model": "claude-sonnet-4-20250514",
"max_tokens": 1024,
"messages": [
{"role": "user", "content": "Hello!"}
]
}'
使用 Claude SDK
python
import anthropic
client = anthropic.Anthropic(
api_key="your-api-key",
base_url="https://router.qingting.work"
)
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(message.content[0].text)
typescript
import Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: 'your-api-key',
baseURL: 'https://router.qingting.work',
});
const message = await client.messages.create({
model: 'claude-sonnet-4-20250514',
max_tokens: 1024,
messages: [
{ role: 'user', content: 'Hello!' },
],
});
console.log(message.content[0].text);
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 对话消息数组 |
max_tokens | integer | 是 | 最大输出 token 数 |
system | string/array | 否 | 系统提示词 |
stream | boolean | 否 | 是否启用流式输出 |
temperature | number | 否 | 采样温度 |
top_p | number | 否 | 核采样参数 |
stop_sequences | array | 否 | 停止序列 |
tools | array | 否 | 工具定义列表 |
tool_choice | object | 否 | 工具选择策略 |
消息格式
json
{
"role": "user",
"content": "Hello!"
}
role 支持:user、assistant
content 支持文本字符串或多模态数组:
json
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{"type": "image", "source": {"type": "url", "url": "https://example.com/image.png"}}
]
}
响应格式
json
{
"id": "msg_xxx",
"type": "message",
"role": "assistant",
"model": "claude-sonnet-4-20250514",
"content": [
{
"type": "text",
"text": "Hello! How can I help you?"
}
],
"stop_reason": "end_turn",
"usage": {
"input_tokens": 10,
"output_tokens": 15
}
}
流式输出
设置 stream: true 启用 SSE 流式输出:
python
with client.messages.stream(
model="claude-sonnet-4-20250514",
max_tokens=1024,
messages=[{"role": "user", "content": "Hello!"}]
) as stream:
for text in stream.text_stream:
print(text, end="")
流式事件类型:
| 事件 | 说明 |
|---|---|
message_start | 消息开始,包含消息元数据 |
content_block_start | 内容块开始 |
content_block_delta | 内容块增量更新 |
content_block_stop | 内容块结束 |
message_delta | 消息级别更新(stop_reason、usage) |
message_stop | 消息结束 |
Tool Use(工具调用)
python
message = client.messages.create(
model="claude-sonnet-4-20250514",
max_tokens=1024,
tools=[{
"name": "get_weather",
"description": "Get current weather for a location",
"input_schema": {
"type": "object",
"properties": {
"location": {"type": "string", "description": "City name"}
},
"required": ["location"]
}
}],
messages=[{"role": "user", "content": "What's the weather in Beijing?"}]
)
工具调用响应中 content 会包含 type: "tool_use" 的内容块:
json
{
"type": "tool_use",
"id": "toolu_xxx",
"name": "get_weather",
"input": {"location": "Beijing"}
}
4. Token 计数
在发送请求前预估 token 用量:
bash
curl -X POST https://router.qingting.work/v1/messages/count_tokens \
-H "Content-Type: application/json" \
-H "x-api-key: your-api-key" \
-d '{
"model": "claude-sonnet-4-20250514",
"messages": [
{"role": "user", "content": "Hello!"}
]
}'
5. 错误处理
错误响应格式:
json
{
"type": "error",
"error": {
"type": "authentication_error",
"message": "错误描述"
}
}
| HTTP 状态码 | 错误类型 | 说明 |
|---|---|---|
| 401 | authentication_error | API Key 无效、已过期或未提供 |
| 402 | insufficient_wallet_balance | 钱包余额不足 |
| 402 | insufficient_api_key_balance | API Key 额度不足 |
| 402 | expired_api_key | API Key 已过期 |
| 502 | upstream_error | 服务返回错误 |
| 503 | upstream_unavailable | 当前无可用服务 |
6. 注意事项
- 可用模型列表请在「模型广场」页面查看
anthropic-version请求头可选,可省略- 流式输出使用 Server-Sent Events (SSE) 协议
- 所有请求均会计费,计费规则请参考「模型广场」中的价格信息