跳到主要内容

认证鉴权

所有 API 请求都需要通过 API Key 进行身份认证。

获取 API Key

  1. 登录 ClawdRouter 平台控制台
  2. 进入 密钥管理页面
  3. 点击「新增密钥」
  4. 复制生成的 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 协议

请求头必填类型说明
Authorizationstring格式为 Bearer YOUR_API_KEY,用于身份认证
Content-Typestring固定为 application/json
Request-Idstring客户系统生成的唯一业务标识,用于请求追踪和问题排查

Anthropic 原生协议

请求头必填类型说明
x-api-keystring你的 API Key
anthropic-versionstringAPI 版本,如 2023-06-01
Content-Typestring固定为 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",
});

常见认证错误

状态码说明解决方法
401API Key 无效或缺失检查 Header 中是否正确携带了 Authorization,格式为 Bearer YOUR_API_KEY
403权限不足检查 API Key 是否有访问对应模型的权限
429请求频率超限降低请求频率,参考速率限制文档