跳到主要内容

API 概览

Claude Code Router Server 提供了完整的 HTTP API,支持:

  • 消息 API:兼容 Anthropic Claude API 的消息接口
  • 配置 API:读取和更新服务器配置
  • 日志 API:查看和管理服务日志
  • 工具 API:计算 Token 数量

基础信息

Base URL: http://localhost:3456

认证方式: API Key(通过 x-api-key 请求头)

curl -H "x-api-key: your-api-key" http://localhost:3456/api/config

API 端点列表

消息相关

端点方法描述
/v1/messagesPOST发送消息(兼容 Anthropic API)
/v1/messages/count_tokensPOST计算消息的 Token 数量

配置管理

端点方法描述
/api/configGET获取当前配置
/api/configPOST更新配置
/api/transformersGET获取可用的转换器列表

日志管理

端点方法描述
/api/logs/filesGET获取日志文件列表
/api/logsGET获取日志内容
/api/logsDELETE清除日志

服务管理

端点方法描述
/api/restartPOST重启服务
/uiGETWeb 管理界面
/ui/GETWeb 管理界面(重定向)

认证

API Key 认证

在请求头中添加 API Key:

curl -X POST http://localhost:3456/v1/messages \
-H "x-api-key: your-api-key" \
-H "content-type: application/json" \
-d '...'

流式响应

消息 API 支持流式响应(Server-Sent Events):

curl -X POST http://localhost:3456/v1/messages \
-H "x-api-key: your-api-key" \
-H "content-type: application/json" \
-d '{"stream": true, ...}'

流式响应格式:

event: message_start
data: {"type":"message_start","message":{...}}

event: content_block_delta
data: {"type":"content_block_delta","delta":{"type":"text_delta","text":"Hello"}}

event: message_stop
data: {"type":"message_stop"}