cURL / 原始 HTTP
不依赖任何 SDK,直接用 HTTP 调用。两套协议都可用。
所有端点都通过 Authorization: Bearer <your-key> 鉴权;Anthropic 原生的 x-api-key: <your-key> 头同样被接受。Key 在 /account/keys 创建,明文只在创建时显示一次。
OpenAI Chat Completions
curl https://api.tokenhouse.ai/v1/chat/completions \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "你好"}],
"stream": false
}'Anthropic Messages
curl https://api.tokenhouse.ai/v1/messages \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"max_tokens": 1024,
"messages": [{"role": "user", "content": "你好"}]
}'Anthropic 协议同时接受 Authorization: Bearer 或原生的 x-api-key 头。
流式(SSE)
请求体加 "stream": true,响应变为 SSE(text/event-stream)。curl 加 -N 关闭缓冲:
curl -N https://api.tokenhouse.ai/v1/chat/completions \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "你好"}],
"stream": true
}'输出为逐帧的 data: 行,以 data: [DONE] 结束:
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"你"}}]}
data: {"id":"chatcmpl-...","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"好"}}]}
data: [DONE]/v1/messages 同样支持 "stream": true,事件格式为 Anthropic 风格。
流式中断时:若已输出至少一帧,按已观测到的 token 数计费并在用量记录中标记 estimated;一帧都没产生的失败请求不扣费。
Token 计数
POST /v1/messages/count_tokens(Anthropic 协议)在发送前估算输入 token 数,不计费:
curl https://api.tokenhouse.ai/v1/messages/count_tokens \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"messages": [{"role": "user", "content": "你好"}]
}'响应:
{ "input_tokens": 10 }Responses 协议
POST /v1/responses 实现 OpenAI Responses 协议(Codex CLI 等工具使用此协议)。最小示例只需 model 与字符串 input:
curl https://api.tokenhouse.ai/v1/responses \
-H "Authorization: Bearer <your-key>" \
-H "Content-Type: application/json" \
-d '{
"model": "claude-sonnet-4-6",
"input": "用一句话介绍自己"
}'桥接为无状态:不支持 previous_response_id(带上即返回 400),由客户端自行携带完整对话历史。对话、function 工具调用、reasoning、流式 SSE 均支持;web_search / file_search / computer_use 与图像工具不支持。reasoning.effort 会自动映射到 Claude 的自适应思考档位。
列出模型
GET /v1/models(需鉴权)返回当前可调用的模型列表,OpenAI list 格式:
curl https://api.tokenhouse.ai/v1/models \
-H "Authorization: Bearer <your-key>"模型别名(如 claude-sonnet-4-6、glm-4.7)与规范键(anthropic.claude-*、zai.glm-*)两种写法都接受。完整模型列表见 /models,各模型费率见 /pricing。
错误响应示例
错误统一为 { "error": { "type", "code", "message" } } 结构。
401 —— 鉴权失败(缺头 / key 无效 / key 已撤销):
{
"error": {
"type": "auth_error",
"code": "invalid_api_key",
"message": "Invalid API key"
}
}402 —— 余额不足。Strict 模式预扣失败时,error 对象额外带 requiredUsd 字段(本次请求需要预留的金额,美元):
{
"error": {
"type": "insufficient_funds_error",
"code": "strict_reserve_failed",
"message": "Insufficient balance to reserve for this request.",
"requiredUsd": 1.5
}
}常见错误码一览:
| HTTP | type | code | 含义 |
|---|---|---|---|
| 401 | auth_error | missing_or_malformed / invalid_api_key / key_revoked_or_disabled | Authorization 头缺失或格式错误 / key 无效 / key 已撤销或禁用 |
| 400 | invalid_request_error | unknown_model | 模型名不识别 |
| 402 | insufficient_funds_error | strict_reserve_failed / flex_threshold_breached | Strict 预扣失败 / Flex 余额低于阈值 |
| 503 | upstream_unavailable | no_route / credential_error | 无可用路由 / 上游凭证异常 |
| — | upstream_error | — | 上游错误按原状态码透传 |
失败请求(4xx / 5xx / 超时)一律不扣费。收到 402 时前往 /account/billing 充值。完整错误说明与计费规则见错误码与计费。