开始/鉴权

说明如何传 API Key、设置请求头,以及避免常见鉴权错误。

鉴权

所有 API 请求需要在 Header 中携带 API 密钥。

鉴权请求头

Authorization: Bearer <YOUR_UNIGATEWAY_API_KEY>

验证

curl https://api.unigateway.ai/v1/models \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY"

返回模型列表说明鉴权生效。

安全提示:API 密钥保存在服务端环境变量中,不要暴露在前端代码或公开仓库中。

JSON POST 请求

POST 请求还需要 Content-Type 请求头:

curl https://api.unigateway.ai/v1/chat/completions \
  -H "Authorization: Bearer $UNIGATEWAY_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-5.4",
    "messages": [{"role": "user", "content": "你好"}]
  }'

常见错误

状态码原因处理方式
401密钥缺失、无效或已过期检查 Authorization 请求头格式
403密钥存在但权限不足检查账户权限
429超过频率或额度增加退避重试

排查

  1. Authorization 格式必须为 Bearer <token>
  2. Base URL 必须为 https://api.unigateway.ai/v1
  3. model 值必须来自 GET /v1/models

密钥管理

  • 测试环境与生产环境使用不同的密钥
  • 疑似泄露时立即轮换密钥

Example request

Run it in your stack

Pick the SDK style that matches your app and copy the snippet directly into your project.

from openai import OpenAI

client = OpenAI(
    api_key="<YOUR_UNIGATEWAY_API_KEY>",
    base_url="https://api.unigateway.ai/v1",
)