1. 创建 API Key 分组

POST /v1/apikey_groups
创建一个新的 API Key 分组,用于组织和管理多个 API Key。

鉴权方式

Authorization: Bearer <API Key>
本页接口仅允许使用账号的默认 API Key 调用。使用其他 API Key 调用会返回 403 Forbidden

请求体

{
  "name": "生产环境",
  "description": "用于生产环境的 API Key 分组"
}
字段类型必需说明
namestring分组名称,不能为空、不能重复、长度不能超过 128 个字符
descriptionstring分组描述(可选)

请求示例

curl -X POST https://api.nonelinear.com/v1/apikey_groups \
  -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "生产环境",
    "description": "用于生产环境的 API Key 分组"
  }'

成功响应 (201)

{
  "success": true,
  "message": "分组\"生产环境\"创建成功",
  "group_id": "6a06f7db38b3bf1609a9c200",
  "group_name": "生产环境"
}
字段类型说明
successboolean是否成功
messagestring操作提示信息
group_idstring新创建分组的 ID
group_namestring新创建分组的名称

错误响应

状态码说明示例
400请求参数错误{"success": false, "error": "分组名称已存在"}{"success": false, "error": "分组名称长度不能超过128个字符"}
401API Key 缺失或无效-
403API Key 有效但不是默认 Key"仅允许使用默认 API Key 创建分组"

2. 查询 API Key 分组列表

GET /v1/apikey_groups
获取当前账号下所有的 API Key 分组信息。

鉴权方式

Authorization: Bearer <API Key>
仅允许使用该账号的默认 API Key,其他 Key 将返回 403 Forbidden

请求示例

curl -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  https://api.nonelinear.com/v1/apikey_groups

成功响应 (200)

{
  "object": "list",
  "data": [
    {
      "id": "6a06f7db38b3bf1609a9c200",
      "name": "生产环境",
      "description": "用于生产环境的 API Key 分组",
      "created_at": 1774081270,
      "updated_at": 1774081270,
      "apikey_count": 5
    },
    {
      "id": "6a06f7db38b3bf1609a9c201",
      "name": "测试环境",
      "description": "用于测试环境的 API Key 分组",
      "created_at": 1774081280,
      "updated_at": 1774081280,
      "apikey_count": 3
    }
  ]
}

data 数组中的字段

字段类型说明
idstring分组 ID
namestring分组名称
descriptionstring分组描述
created_atnumber创建时间(Unix 时间戳)
updated_atnumber最后更新时间(Unix 时间戳)
apikey_countnumber该分组中的 API Key 数量

错误响应

状态码说明
401API Key 缺失或无效
403API Key 有效但不是默认 Key

3. 批量设置分组内 API Key 的限额

POST /v1/apikey_groups/<group_id>/batch_budget
为指定分组下的所有 API Key 统一设置各类限额(预算、有效期、日限额、月限额、价格限额等)。

鉴权方式

Authorization: Bearer <API Key>
仅允许使用该账号的默认 API Key,其他 Key 将返回 403 Forbidden

URL 参数

参数类型说明
group_idstring分组 ID,从查询分组接口获取

请求体

{
  "budget": 1000.0,
  "daily_limit": 100.0,
  "monthly_limit": 500.0,
  "expiry_date": "2026-12-31",
  "input_price_limit": 50.0,
  "output_price_limit": 200.0
}
字段类型必需说明
budgetnumber预算限额(元),0 表示无限制
daily_limitnumber每日消费限额(元),0 表示无限制
monthly_limitnumber每月消费限额(元),0 表示无限制
expiry_datestring有效期截止日期(格式:YYYY-MM-DD
input_price_limitnumber模型输入价格限额(元/M token),0 表示无限制
output_price_limitnumber模型输出价格限额(元/M token),0 表示无限制
至少需要指定一项限额参数,否则返回 400 Bad Request

请求示例

curl -X POST https://api.nonelinear.com/v1/apikey_groups/6a06f7db38b3bf1609a9c200/batch_budget \
  -H "Authorization: Bearer sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "budget": 1000.0,
    "daily_limit": 100.0,
    "monthly_limit": 500.0,
    "expiry_date": "2026-12-31"
  }'

成功响应 (200)

{
  "success": true,
  "message": "批量设置限额成功,共更新 5 个API Key",
  "updated_count": 5,
  "failed_count": 0
}
字段类型说明
successboolean是否成功
messagestring操作提示信息
updated_countnumber成功更新的 API Key 数量
failed_countnumber失败的 API Key 数量

错误响应

状态码说明示例
400请求参数错误参数类型错误、超出范围、格式错误或未找到符合条件的 API Key
401API Key 缺失或无效-
403API Key 有效但不是默认 Key"仅允许使用默认 API Key 设置限额"

常见错误响应

{
  "success": false,
  "error": "参数 budget 类型错误,期望 int 或 float,实际收到 str"
}
{
  "success": false,
  "error": "参数 daily_limit 不能为负数"
}
{
  "success": false,
  "error": "参数 expiry_date 格式错误,应为 YYYY-MM-DD"
}
{
  "success": false,
  "error": "至少需要指定一项限额参数"
}

参数验证规则

数值类型参数(budget, daily_limit, monthly_limit, input_price_limit, output_price_limit)

  • 类型intfloat
  • 范围:≥ 0,不能为负数
  • 说明0 表示无限制

日期类型参数(expiry_date)

  • 类型string
  • 格式YYYY-MM-DD(ISO 8601 日期格式)
  • 示例"2026-12-31"

Python 调用示例

import requests
import json

BASE_URL = 'https://api.nonelinear.com'

def create_apikey_group(apikey: str, name: str, description: str = '') -> dict:
    """创建 API Key 分组"""
    resp = requests.post(
        f'{BASE_URL}/v1/apikey_groups',
        headers={
            'Authorization': f'Bearer {apikey}',
            'Content-Type': 'application/json'
        },
        json={
            'name': name,
            'description': description
        }
    )
    resp.raise_for_status()
    return resp.json()


def query_apikey_groups(apikey: str) -> list:
    """查询所有 API Key 分组"""
    resp = requests.get(
        f'{BASE_URL}/v1/apikey_groups',
        headers={'Authorization': f'Bearer {apikey}'}
    )
    resp.raise_for_status()
    return resp.json()['data']


def set_group_apikey_budget(apikey: str, group_id: str, **kwargs) -> dict:
    """
    批量设置分组内 API Key 的限额
    
    kwargs 可包含:
      - budget: 预算限额
      - daily_limit: 每日限额
      - monthly_limit: 每月限额
      - expiry_date: 有效期
      - input_price_limit: 输入价格限额
      - output_price_limit: 输出价格限额
    """
    resp = requests.post(
        f'{BASE_URL}/v1/apikey_groups/{group_id}/batch_budget',
        headers={
            'Authorization': f'Bearer {apikey}',
            'Content-Type': 'application/json'
        },
        json=kwargs
    )
    resp.raise_for_status()
    return resp.json()


if __name__ == '__main__':
    API_KEY = '<你的默认APIKey>'

    # 创建分组
    result = create_apikey_group(API_KEY, '生产环境', '用于生产环境的 API Key')
    print(f"创建分组: {result}")
    group_id = result.get('group_id')

    # 查询所有分组
    groups = query_apikey_groups(API_KEY)
    print(f"\n所有分组:")
    for group in groups:
        print(f"  {group['name']}: {group['apikey_count']} 个 API Key")

    # 批量设置分组内 API Key 的限额
    if group_id:
        budget_result = set_group_apikey_budget(
            API_KEY,
            group_id,
            budget=1000.0,
            daily_limit=100.0,
            monthly_limit=500.0,
            expiry_date='2026-12-31'
        )
        print(f"\n批量设置限额结果: {budget_result}")

最佳实践

分组命名建议

  • 按环境分组生产环境测试环境开发环境
  • 按业务分组社交媒体内容生成数据分析
  • 按团队分组团队A团队B运维团队

使用场景

  1. 成本中心分配 - 为不同部门/业务分配不同的 API Key,便于成本统计
  2. 权限隔离 - 为敏感业务单独创建分组,便于权限管理
  3. 配额管理 - 为不同的业务场景设置不同的调用限額
  4. 监控告警 - 为不同的分组设置独立的监控和告警规则

相关接口