鉴权
如何使用 API Key 调用 Gateway
API Key 格式
在 Console 令牌管理 中创建的密钥用于调用 Gateway,请求头格式为:
Authorization: Bearer sk-xxxxxxxx- 前缀一般为
sk- - 不要 将 Key 提交到 Git、前端公开代码或浏览器可见环境
- Key 泄露后请立即在 Console 中禁用或删除并重新创建
请求示例
curl "$YOUR_GATEWAY_BASE_URL/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model":"gpt-4o","messages":[{"role":"user","content":"Hi"}]}'使用 OpenAI 官方 SDK
只需修改 baseURL 与 apiKey:
from openai import OpenAI
client = OpenAI(
base_url="YOUR_GATEWAY_BASE_URL/v1",
api_key="YOUR_API_KEY",
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": "Hello!"}],
)
print(response.choices[0].message.content)import OpenAI from "openai";
const client = new OpenAI({
baseURL: "YOUR_GATEWAY_BASE_URL/v1",
apiKey: "YOUR_API_KEY",
});
const response = await client.chat.completions.create({
model: "gpt-4o",
messages: [{ role: "user", content: "Hello!" }],
});
console.log(response.choices[0]?.message?.content);令牌权限说明
创建令牌时可配置:
| 选项 | 说明 |
|---|---|
| 可用模型 | 不选则默认可访问平台全部 active 模型 |
| 消费额度 | 可选 USD 上限,超出后请求会被拒绝 |
| 过期时间 | 可选,过期后 Key 失效 |
可在 令牌管理 随时编辑或轮换密钥。
与 Console 登录的区别
| Console | Gateway | |
|---|---|---|
| 用途 | 管理令牌、钱包、日志 | 调用模型 API |
| 鉴权 | 用户 JWT(浏览器登录) | API Key(Bearer sk-xxx) |
| 典型路径 | /api/v1/tokens 等 | /v1/chat/completions 等 |
请勿把 Console 的登录 Token 当作 API Key 使用。
