快速开始/鉴权与请求约定

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

鉴权与请求约定

UniGateway 使用 Bearer Token 鉴权。

必需请求头

Authorization: Bearer <UNIGATEWAY_API_KEY>

常见调用方式

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

对于 JSON POST 请求,还需要:

Content-Type: application/json

请求头兼容说明

对绝大多数 UniGateway 调用,Authorization: Bearer ... 即可。 /v1/models 在兼容模式下会识别部分兼容请求头(Anthropic/Gemini),但标准 UniGateway 接入不需要额外请求头。

安全建议

  • 不要把 API Key 暴露在前端代码里。
  • 生产和测试环境使用不同 Key。
  • 疑似泄露时立即轮换。
  • 服务端实现限流与退避重试。

常见错误

  • 401 Unauthorized:未携带 Key、Key 无效或已过期。
  • 403 Forbidden:Key 存在但权限不足。
  • 429 Too Many Requests:超过频率或额度。

排查清单

  1. 确认 Authorization 格式严格为 Bearer <token>
  2. 确认 base URL 使用 https://api.unigateway.ai/v1
  3. 确认 model 来自 /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",
)