Skip to content

认证

OpenHub API 使用 Bearer Token 进行认证。

获取 API Key

  1. 登录 OpenHub 控制台
  2. 进入「API Keys」页面
  3. 点击「创建 API Key」
  4. 保存生成的 API Key(只显示一次)

使用 API Key

在所有 API 请求中,需要在 HTTP Header 中包含 API Key:

http
Authorization: Bearer YOUR_API_KEY

示例

bash
curl https://api.myopenhub.com/v1/llm/chat/completions \
  -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-4",
    "messages": [{"role": "user", "content": "Hello"}]
  }'

API Key 权限

创建 API Key 时可以设置权限:

  • 只读权限:只能查询数据,不能修改
  • 读写权限:可以调用所有 API
  • 管理员权限:可以管理用户和团队

API Key 安全

安全提示

  • 不要在客户端代码中硬编码 API Key
  • 不要将 API Key 提交到版本控制系统
  • 定期轮换 API Key
  • 为不同环境使用不同的 API Key

最佳实践

  1. 使用环境变量
bash
export OPENHUB_API_KEY=sk-xxxxxxxxxxxxxxxx
  1. 使用配置文件
yaml
# config.yaml
api:
  key: ${OPENHUB_API_KEY}
  base_url: https://api.myopenhub.com/v1
  1. 使用密钥管理服务
  • AWS Secrets Manager
  • Azure Key Vault
  • HashiCorp Vault

错误处理

401 Unauthorized

API Key 无效或已过期:

json
{
  "success": false,
  "error": {
    "code": "UNAUTHORIZED",
    "message": "Invalid API key"
  }
}

解决方法

  • 检查 API Key 是否正确
  • 检查 API Key 是否已过期
  • 重新生成 API Key

403 Forbidden

API Key 权限不足:

json
{
  "success": false,
  "error": {
    "code": "FORBIDDEN",
    "message": "Insufficient permissions"
  }
}

解决方法

  • 检查 API Key 权限设置
  • 使用具有足够权限的 API Key

速率限制

不同套餐有不同的速率限制:

套餐请求限制并发限制
免费版60 次/分钟5
智能版300 次/分钟20
专业版1000 次/分钟50
企业版无限制200

超过限制会返回 429 错误:

json
{
  "success": false,
  "error": {
    "code": "RATE_LIMIT_EXCEEDED",
    "message": "Rate limit exceeded. Retry after 60 seconds."
  }
}

下一步

Released under the MIT License.