API 接口
更新时间:2026-10-30 17:38:58
OpenAI Chat Completions
接口信息
- 接口路径:
POST https://router.253.com/v1/chat/completions - 认证方式:API Key
- 响应格式:
application/json
用于基于消息历史生成聊天回复,兼容 OpenAI Chat Completions 请求和响应格式。
请求头
Authorization: Bearer sk-xxxxxxxxxxxxxxxx Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 对话消息列表;特殊 FIM 模型使用 prefix/suffix 时可省略 |
stream | boolean | 否 | 是否启用流式响应,默认 false |
stream_options | object | 否 | 流式选项,例如 include_usage |
max_tokens | integer | 否 | 最大输出 Token 数;部分新模型建议使用 max_completion_tokens |
max_completion_tokens | integer | 否 | 最大补全 Token 数 |
temperature | number | 否 | 采样温度;具体范围由模型决定 |
top_p | number | 否 | 核采样参数 |
top_k | integer | 否 | Top-K 采样;仅部分渠道支持 |
stop | string/array | 否 | 停止序列 |
n | integer | 否 | 返回候选数量;仅部分模型支持 |
presence_penalty | number | 否 | 话题重复惩罚 |
frequency_penalty | number | 否 | 词频重复惩罚 |
seed | number | 否 | 随机种子;不保证所有模型可复现 |
reasoning_effort | string | 否 | 推理强度,例如 low、medium、high,取值由模型决定 |
verbosity | string/object | 否 | 输出详细程度;仅部分模型支持 |
response_format | object | 否 | 输出格式,可使用 text、json_object 或 json_schema |
tools | array | 否 | 可调用工具列表 |
tool_choice | string/object | 否 | 工具选择策略 |
parallel_tool_calls | boolean | 否 | 是否允许并行工具调用 |
modalities | array | 否 | 输出模态,例如文本或音频 |
audio | object | 否 | 音频输出配置 |
web_search_options | object | 否 | Web Search 配置;search_context_size 可为 low、medium、high |
metadata | object | 否 | 请求元数据 |
user | string | 否 | 终端用户标识 |
store | boolean | 否 | 是否允许上游存储请求/响应;可能受渠道配置影响 |
service_tier | string | 否 | 上游服务等级;默认可能被渠道配置过滤 |
messages 结构
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
role | string | 是 | system、developer、user、assistant 或 tool |
content | string/array/null | 是 | 文本或多模态内容;工具调用消息中可为 null |
name | string | 否 | 消息发送者名称 |
tool_calls | array | 否 | Assistant 发起的工具调用 |
tool_call_id | string | 否 | Tool 消息对应的工具调用 ID |
reasoning_content | string | 否 | 推理内容;仅部分兼容渠道使用 |
多模态 content 支持的常见内容块:
type | 主要字段 | 说明 |
|---|---|---|
text | text | 文本内容 |
image_url | image_url.url、image_url.detail | 图片 URL 或 Data URL |
input_audio | input_audio.data、input_audio.format | Base64 音频,常见格式为 wav、mp3 |
file | file.file_id 或 file.file_data | 文件引用或文件数据 |
video_url | video_url.url | 视频地址;仅部分渠道支持 |
基础请求示例
curl -X POST "https://router.253.com/v1/chat/completions" \ -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "messages": [ { "role": "system", "content": "你是一个简洁、准确的助手。" }, { "role": "user", "content": "用三句话介绍杭州。" } ], "temperature": 0.7, "max_completion_tokens": 500 }'
2.5 非流式响应示例
{ "id": "chatcmpl-example", "object": "chat.completion", "created": 1792368000, "model": "your-model-name", "choices": [ { "index": 0, "message": { "role": "assistant", "content": "杭州是浙江省省会,以西湖和深厚的人文历史闻名。这里兼具数字经济活力与江南山水气质。龙井茶、灵隐寺和京杭大运河也是城市的重要名片。" }, "finish_reason": "stop" } ], "usage": { "prompt_tokens": 25, "completion_tokens": 48, "total_tokens": 73 } }
finish_reason 常见值:
| 值 | 说明 |
|---|---|
stop | 正常结束 |
length | 达到最大输出 Token 数 |
tool_calls | 模型请求调用工具 |
content_filter | 内容过滤导致结束 |
流式请求示例
curl -N -X POST "https://router.253.com/v1/chat/completions" \ -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "messages": [ {"role": "user", "content": "写一句关于秋天的话。"} ], "stream": true, "stream_options": { "include_usage": true } }'
响应由多个 data: 块组成:
data: {"id":"chatcmpl-example","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]} data: {"id":"chatcmpl-example","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"秋风掠过树梢,"},"finish_reason":null}]} data: {"id":"chatcmpl-example","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"把黄昏染成了金色。"},"finish_reason":null}]} data: [DONE]
工具调用示例
请求:
{ "model": "your-model-name", "messages": [ { "role": "user", "content": "杭州现在天气怎么样?" } ], "tools": [ { "type": "function", "function": { "name": "get_weather", "description": "查询指定城市天气", "parameters": { "type": "object", "properties": { "city": { "type": "string", "description": "城市名称" } }, "required": [ "city" ] } } } ], "tool_choice": "auto" }
模型可能返回:
{ "choices": [ { "index": 0, "message": { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_001", "type": "function", "function": { "name": "get_weather", "arguments": "{\"city\":\"杭州\"}" } } ] }, "finish_reason": "tool_calls" } ] }
执行工具后,将结果作为 tool 消息再次提交:
{ "model": "your-model-name", "messages": [ { "role": "user", "content": "杭州现在天气怎么样?" }, { "role": "assistant", "content": null, "tool_calls": [ { "id": "call_001", "type": "function", "function": { "name": "get_weather", "arguments": "{\"city\":\"杭州\"}" } } ] }, { "role": "tool", "tool_call_id": "call_001", "content": "{\"temperature\":24,\"condition\":\"晴\"}" } ] }
OpenAI Responses
接口信息
- 接口路径:
POST https://router.253.com/v1/responses - 认证方式:API Key
- 响应格式:
application/json
Responses API 使用统一的 input 和 output 结构,适合推理模型、多轮响应、工具调用和多模态输入。
请求头
Authorization: Bearer sk-xxxxxxxxxxxxxxxx Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
input | string/array | 是 | 输入文本或输入项数组 |
instructions | string/array | 否 | 系统级指令 |
stream | boolean | 否 | 是否启用流式响应 |
stream_options | object | 否 | 流式选项 |
max_output_tokens | integer | 否 | 最大输出 Token 数 |
temperature | number | 否 | 采样温度 |
top_p | number | 否 | 核采样参数 |
top_logprobs | integer | 否 | 返回 Top Logprobs 数量;仅部分模型支持 |
reasoning | object | 否 | 推理配置,例如 effort、summary |
text | object | 否 | 文本输出格式配置 |
tools | array | 否 | 工具定义,包括函数工具或上游支持的内置工具 |
tool_choice | string/object | 否 | 工具选择策略 |
parallel_tool_calls | boolean | 否 | 是否允许并行工具调用 |
max_tool_calls | integer | 否 | 最大工具调用次数;仅部分渠道支持 |
previous_response_id | string | 否 | 上一条响应 ID,用于服务端关联多轮上下文 |
conversation | string/object | 否 | 会话引用;仅支持该能力的渠道有效 |
include | array | 否 | 指定附加返回内容 |
truncation | string | 否 | 上下文截断策略 |
metadata | object | 否 | 请求元数据 |
store | boolean | 否 | 是否允许上游存储请求/响应;可能受渠道配置影响 |
service_tier | string | 否 | 上游服务等级;默认可能被渠道配置过滤 |
prompt_cache_key | string | 否 | 提示缓存键 |
prompt_cache_options | object | 否 | 提示缓存选项 |
prompt_cache_retention | string/object | 否 | 提示缓存保留配置 |
safety_identifier | string | 否 | 安全审计用户标识;默认可能被渠道配置过滤 |
user | string | 否 | 终端用户标识 |
input 可以直接使用字符串:
{ "input": "解释什么是幂等性。" }
也可以使用消息数组:
{ "input": [ { "role": "user", "content": [ { "type": "input_text", "text": "描述这张图片。" }, { "type": "input_image", "image_url": "https://example.com/image.jpg" } ] } ] }
常见输入内容块:
type | 主要字段 | 说明 |
|---|---|---|
input_text | text | 文本输入 |
input_image | image_url、detail | 图片 URL 或 Data URL |
input_file | file_url 或上游支持的文件引用 | 文件输入 |
function_call_output | call_id、output | 函数调用执行结果 |
基础请求示例
curl -X POST "https://router.253.com/v1/responses" \ -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "instructions": "回答应准确、简洁。", "input": "解释 REST API 中幂等性的含义。", "max_output_tokens": 500 }'
非流式响应示例
{ "id": "resp_example", "object": "response", "created_at": 1792368000, "status": "completed", "model": "your-model-name", "output": [ { "type": "message", "id": "msg_example", "status": "completed", "role": "assistant", "content": [ { "type": "output_text", "text": "幂等性表示同一个请求执行一次或多次,对服务端产生的最终效果相同。" } ] } ], "usage": { "input_tokens": 18, "output_tokens": 30, "total_tokens": 48 } }
status 常见值:
| 值 | 说明 |
|---|---|
completed | 响应已完成 |
in_progress | 仍在生成 |
incomplete | 未完整生成,原因通常位于 incomplete_details |
failed | 生成失败,错误信息通常位于 error |
不要假设 output[0].content[0] 永远是文本。output 还可能包含推理项、函数调用、Web Search 调用、文件搜索调用或图片生成调用,应根据每个元素的 type 分支处理。
流式请求示例
curl -N -X POST "https://router.253.com/v1/responses" \ -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "input": "写一段 50 字以内的产品介绍。", "stream": true }'
流式响应由带 type 的事件组成,常见事件包括:
data: {"type":"response.created","response":{"id":"resp_example","status":"in_progress"}} data: {"type":"response.output_item.added","output_index":0,"item":{"type":"message","role":"assistant"}} data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"这是一款"} data: {"type":"response.output_text.delta","output_index":0,"content_index":0,"delta":"高效的智能助手。"} data: {"type":"response.completed","response":{"id":"resp_example","status":"completed"}}
客户端应按照 type 处理事件,并通过 output_index、content_index、item_id 等字段组装完整输出。
工具调用示例
请求:
{ "model": "your-model-name", "input": "查询杭州天气。", "tools": [ { "type": "function", "name": "get_weather", "description": "查询指定城市天气", "parameters": { "type": "object", "properties": { "city": { "type": "string" } }, "required": [ "city" ], "additionalProperties": false }, "strict": true } ], "tool_choice": "auto" }
模型可能在 output 中返回:
{ "type": "function_call", "id": "fc_example", "call_id": "call_001", "name": "get_weather", "arguments": "{\"city\":\"杭州\"}" }
执行函数后,可通过 previous_response_id 和 function_call_output 继续请求:
{ "model": "your-model-name", "previous_response_id": "resp_example", "input": [ { "type": "function_call_output", "call_id": "call_001", "output": "{\"temperature\":24,\"condition\":\"晴\"}" } ] }
Anthropic Claude Messages
接口信息
- 接口路径:
POST https://router.253.com/v1/messages - 认证方式:API Key
- 响应格式:
application/json
用于以 Anthropic Messages API 格式调用 Claude 或其他支持该协议的模型。
请求头
推荐使用 Anthropic 风格:
x-api-key: sk-xxxxxxxxxxxxxxxx anthropic-version: 2023-06-01 Content-Type: application/json
也可以使用 Bearer Token:
Authorization: Bearer sk-xxxxxxxxxxxxxxxx anthropic-version: 2023-06-01 Content-Type: application/json
请求参数
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model | string | 是 | 模型名称 |
messages | array | 是 | 对话消息列表,只使用 user 和 assistant 角色 |
max_tokens | integer | 建议是 | 最大输出 Token 数;网关接受省略,但原生 Claude 上游通常要求提供 |
system | string/array | 否 | 系统提示,不应放在 messages 的 system 角色中 |
stream | boolean | 否 | 是否启用流式响应 |
temperature | number | 否 | 采样温度 |
top_p | number | 否 | 核采样参数 |
top_k | integer | 否 | Top-K 采样 |
stop_sequences | array | 否 | 自定义停止序列 |
tools | array | 否 | 工具列表 |
tool_choice | object | 否 | 工具选择策略 |
thinking | object | 否 | 扩展思考配置,例如 type、budget_tokens |
metadata | object | 否 | 请求元数据 |
context_management | object | 否 | 上下文管理配置;仅部分模型支持 |
output_config | object | 否 | 输出配置 |
output_format | object | 否 | 结构化输出配置;仅部分模型支持 |
container | string/object | 否 | 容器或上下文引用;仅部分上游支持 |
mcp_servers | array | 否 | MCP Server 配置;仅部分上游支持 |
inference_geo | string | 否 | 推理地域;默认可能被渠道配置过滤 |
speed | string | 否 | 推理速度模式;默认可能被渠道配置过滤 |
service_tier | string | 否 | 上游服务等级;默认可能被渠道配置过滤 |
messages 结构
| 字段 | 类型 | 必填 | 说明 |
|---|---|---|---|
role | string | 是 | user 或 assistant |
content | string/array | 是 | 文本或内容块数组 |
常见内容块:
type | 主要字段 | 说明 |
|---|---|---|
text | text | 文本内容 |
image | source | 图片内容,source.type 可为 base64 或 url |
tool_use | id、name、input | Assistant 请求调用工具 |
tool_result | tool_use_id、content | 用户侧返回工具执行结果 |
thinking | thinking、signature | 扩展思考内容;是否返回由模型和配置决定 |
基础请求示例
curl -X POST "https://router.253.com/v1/messages" \ -H "x-api-key: sk-xxxxxxxxxxxxxxxx" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "system": "你是一个简洁、准确的助手。", "messages": [ { "role": "user", "content": "用三句话介绍杭州。" } ], "max_tokens": 500 }'
非流式响应示例
{ "id": "msg_example", "type": "message", "role": "assistant", "content": [ { "type": "text", "text": "杭州是浙江省省会,以西湖和深厚的人文历史闻名。这里兼具数字经济活力与江南山水气质。龙井茶、灵隐寺和京杭大运河也是城市的重要名片。" } ], "model": "your-model-name", "stop_reason": "end_turn", "usage": { "input_tokens": 25, "output_tokens": 48, "cache_creation_input_tokens": 0, "cache_read_input_tokens": 0 } }
stop_reason 常见值:
| 值 | 说明 |
|---|---|
end_turn | 模型正常结束当前回复 |
max_tokens | 达到最大输出 Token 数 |
stop_sequence | 命中自定义停止序列 |
tool_use | 模型请求调用工具 |
流式请求示例
curl -N -X POST "https://router.253.com/v1/messages" \ -H "x-api-key: sk-xxxxxxxxxxxxxxxx" \ -H "anthropic-version: 2023-06-01" \ -H "Content-Type: application/json" \ -d '{ "model": "your-model-name", "messages": [ {"role": "user", "content": "写一句关于秋天的话。"} ], "max_tokens": 200, "stream": true }'
常见 SSE 事件顺序:
event: message_start data: {"type":"message_start","message":{"id":"msg_example","type":"message","role":"assistant","content":[],"model":"your-model-name"}} event: content_block_start data: {"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}} event: content_block_delta data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"秋风掠过树梢,"}} event: message_delta data: {"type":"message_delta","delta":{"stop_reason":"end_turn"},"usage":{"output_tokens":12}} event: message_stop data: {"type":"message_stop"}
工具调用示例
请求:
{ "model": "your-model-name", "messages": [ { "role": "user", "content": "查询杭州天气。" } ], "max_tokens": 500, "tools": [ { "name": "get_weather", "description": "查询指定城市天气", "input_schema": { "type": "object", "properties": { "city": { "type": "string" } }, "required": [ "city" ] } } ] }
模型可能返回:
{ "id": "msg_example", "type": "message", "role": "assistant", "content": [ { "type": "tool_use", "id": "toolu_001", "name": "get_weather", "input": { "city": "杭州" } } ], "stop_reason": "tool_use" }
执行工具后,将结果作为 tool_result 内容块再次提交:
{ "model": "your-model-name", "messages": [ { "role": "user", "content": "查询杭州天气。" }, { "role": "assistant", "content": [ { "type": "tool_use", "id": "toolu_001", "name": "get_weather", "input": { "city": "杭州" } } ] }, { "role": "user", "content": [ { "type": "tool_result", "tool_use_id": "toolu_001", "content": "{\"temperature\":24,\"condition\":\"晴\"}" } ] } ], "max_tokens": 500 }
错误响应
OpenAI 格式错误
/v1/chat/completions 和 /v1/responses 通常返回 OpenAI 兼容错误:
{ "error": { "message": "model is required (request id: xxxxxxxx)", "type": "new_api_error", "param": "", "code": "invalid_request" } }
Claude 格式错误
/v1/messages 通常返回 Claude 兼容错误:
{ "type": "error", "error": { "type": "invalid_request_error", "message": "field messages is required (request id: xxxxxxxx)" } }
常见 HTTP 状态码
| 状态码 | 含义 | 常见原因 |
|---|---|---|
400 | 请求错误 | 缺少必填字段、JSON 无效、参数超出范围、模型参数不兼容 |
401 | 鉴权失败 | API Key 缺失、无效、过期或不可用 |
403 | 禁止访问 | 用户被禁用、令牌无权访问对应资源 |
413 | 请求体过大 | JSON、Base64 图片、音频或文件超过网关限制 |
429 | 请求受限 | 触发速率限制、并发限制或额度不足 |
500 | 服务内部错误 | 网关内部异常、数据库异常 |
502 | 上游错误 | 上游服务异常或返回无法处理的响应 |
503 | 服务暂不可用 | 无可用渠道、系统维护或性能保护 |
建议记录错误响应中的 request id,以便服务端检索对应日志。
查询用户余额/当前APIKey余额
接口信息
- 接口路径:
GET https://router.253.com/v1/me/balance - 认证方式:API Key
- 请求格式:无请求体
- 响应格式:
application/json - 用途:查询当前 API Key 所属用户的钱包余额。
认证
请求头必须携带 Authorization:
Authorization: Bearer sk-xxxxxxxxxxxxxxxx
说明:
- 支持
Bearer <API_KEY>格式。 - 该接口是只读认证接口。
- 已耗尽额度或已过期的令牌仍可查询余额。
- 已禁用的令牌不可查询。
- 所属用户被封禁时不可查询。
- 该接口最小调用间隔为5分钟
请求参数
Header
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
Authorization | string | 是 | API Key,推荐格式:Bearer sk-xxxx |
Query 参数
| 字段 | 类型 | 说明 |
|---|---|---|
scope | string | 可选,固定值:token, 有值查询api key余额,无值则查用户余额 |
Body 参数
无。
成功响应
HTTP 状态码
200 OK
响应字段
| 字段 | 类型 | 说明 |
|---|---|---|
success | boolean | 是否成功 |
message | string | 成功时为空字符串 |
data.balance | number | 当前用户余额,单位为 data.currency,保留 2 位小数 |
data.currency | string | 币种,固定为 CNY |
响应示例
{ "success": true, "message": "", "data": { "balance": 3.6, "currency": "CNY" } }
错误响应
未提供 API Key
HTTP 状态码:401 Unauthorized
{ "success": false, "message": "token is not provided" }
API Key 无效
HTTP 状态码:401 Unauthorized
{ "success": false, "message": "invalid token" }
API Key 已禁用
HTTP 状态码:401 Unauthorized
{ "success": false, "message": "token status is unavailable" }
用户被封禁
HTTP 状态码:403 Forbidden
{ "success": false, "message": "user is banned" }
这篇文档对您有帮助吗?




