认证鉴权
所有 API 请求都需要通过 API Key 进行身份认证。
获取 API Key
- 登录 ClawdRouter 平台控制台
- 进入 密钥管理页面
- 点击「新增密钥」
- 复制生成的 Key 并妥善保存
安全提示
API Key 是你的账户凭证,请务必妥善保管。不要在客户端代码、公开仓库或日志中暴露你的 API Key。
认证方式
ClawdRouter 支持两种认证方式,分别对应两种 API 协议:
OpenAI 协议 (/v1/chat/completions)
在请求的 Authorization Header 中携带 API Key:
Authorization: Bearer YOUR_API_KEY
curl https://api.clawdrouter.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxx" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}'
Anthropic 原生协议 (/v1/messages)
使用 x-api-key Header 携带 API Key,并需提供 anthropic-version:
x-api-key: YOUR_API_KEY
anthropic-version: 2023-06-01
curl https://api.clawdrouter.com/v1/messages \
-H "Content-Type: application/json" \
-H "x-api-key: sk-xxxxxxxxxxxxxxxxxxxx" \
-H "anthropic-version: 2023-06-01" \
-d '{"model": "claude-sonnet-4-6", "max_tokens": 1024, "messages": [{"role": "user", "content": "你好"}]}'
两种认证方式使用相同的 API Key
无论选择哪种协议,都使用同一个 API Key,仅传递方式不同。
请求头说明
OpenAI 协议
| 请求头 | 必填 | 类型 | 说明 |
|---|---|---|---|
Authorization | 是 | string | 格式为 Bearer YOUR_API_KEY,用于身份认证 |
Content-Type | 是 | string | 固定为 application/json |
Request-Id | 否 | string | 客户系统生成的唯一业务标识,用于请求追踪和问题排查 |
Anthropic 原生协议
| 请求头 | 必填 | 类型 | 说明 |
|---|---|---|---|
x-api-key | 是 | string | 你的 API Key |
anthropic-version | 是 | string | API 版本,如 2023-06-01 |
Content-Type | 是 | string | 固定为 application/json |
Request-Id
你可以在请求头中传入自定义的 Request-Id,用于:
- 请求追踪 — 将你的业务系统中的请求与 ClawdRouter 的调用关联
- 问题排查 — 在联系技术支持时提供
Request-Id可加快问题定位
curl https://api.clawdrouter.com/v1/chat/completions \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-H "Request-Id: your-unique-request-id-123" \
-d '{"model": "gpt-4o", "messages": [{"role": "user", "content": "你好"}]}'
在 SDK 中使用
Python (OpenAI SDK)
from openai import OpenAI
client = OpenAI(
api_key="YOUR_API_KEY",
base_url="https://api.clawdrouter.com/v1",
)
Python (Anthropic SDK)
import anthropic
client = anthropic.Anthropic(
api_key="YOUR_API_KEY",
base_url="https://api.clawdrouter.com",
)
Node.js (OpenAI SDK)
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.clawdrouter.com/v1",
});
Node.js (Anthropic SDK)
import Anthropic from "@anthropic-ai/sdk";
const client = new Anthropic({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.clawdrouter.com",
});
常见认证错误
| 状态码 | 说明 | 解决方法 |
|---|---|---|
401 | API Key 无效或缺失 | 检查 Header 中是否正确携带了 Authorization,格式为 Bearer YOUR_API_KEY |
403 | 权限不足 | 检查 API Key 是否有访问对应模型的权限 |
429 | 请求频率超限 | 降低请求频率,参考速率限制文档 |