{
  "tags": [
    "API 密钥"
  ],
  "description": "为当前登录用户开通 API Key；管理员可通过可选的 `uid` 为指定用户开通。每个用户只能保存一组 API Key，已开通时不能重复创建。服务端随机生成 16 位 API Key 和 30 位 API Secret，并以明文返回；新记录的来源 IP 白名单初始为 null，即不限制调用来源 IP，后续可通过更新接口设置。调用方应立即安全保存响应，避免写入日志或暴露给无关方。",
  "operationId": "user_post_v1_api_key",
  "security": [
    {
      "AccessToken": []
    },
    {
      "ApiKey": [],
      "ApiSecret": []
    }
  ],
  "responses": {
    "200": {
      "description": "业务结果通过 HTTP 200 返回。开通成功时返回新生成的 API Key 和 API Secret；目标用户已有密钥、普通用户尝试指定 `uid` 或登录认证无效时返回字符串业务错误码，创建事务会回滚。",
      "content": {
        "application/json": {
          "schema": {
            "oneOf": [
              {
                "type": "object",
                "required": [
                  "code",
                  "msg",
                  "data"
                ],
                "properties": {
                  "code": {
                    "type": "integer",
                    "enum": [
                      0
                    ],
                    "description": "成功状态码，API Key 开通成功时固定为 0。"
                  },
                  "msg": {
                    "type": "string",
                    "enum": [
                      "api key开通成功"
                    ],
                    "description": "API Key 记录创建并提交后返回的确认消息。"
                  },
                  "data": {
                    "type": "object",
                    "required": [
                      "api_key",
                      "api_secret"
                    ],
                    "properties": {
                      "api_key": {
                        "type": "string",
                        "minLength": 16,
                        "maxLength": 16,
                        "description": "新生成的 16 位访问标识，调用 API 时通过 `api-key` 请求头提交。"
                      },
                      "api_secret": {
                        "type": "string",
                        "minLength": 30,
                        "maxLength": 30,
                        "description": "与 API Key 配对的新生成 30 位明文密钥，调用 API 时通过 `api-secret` 请求头提交。"
                      }
                    },
                    "additionalProperties": false,
                    "description": "新开通的 API Key 凭证对。"
                  }
                },
                "additionalProperties": false
              },
              {
                "type": "object",
                "required": [
                  "code",
                  "msg",
                  "data"
                ],
                "properties": {
                  "code": {
                    "type": "string",
                    "enum": [
                      "api-key-1",
                      "task-6",
                      "login_required"
                    ],
                    "description": "业务错误码：`api-key-1` 表示目标用户已经开通 API Key，`task-6` 表示普通用户尝试为指定用户开通，`login_required` 表示登录态或 API Key 认证无效。"
                  },
                  "msg": {
                    "type": "string",
                    "description": "与业务错误码对应的中文重复开通、权限或认证失败原因。"
                  },
                  "data": {
                    "type": "string",
                    "enum": [
                      ""
                    ],
                    "description": "业务错误时固定为空字符串。"
                  }
                },
                "additionalProperties": false
              }
            ]
          },
          "example": {
            "code": 0,
            "msg": "api key开通成功",
            "data": {
              "api_key": "EXAMPLEAPIKEY001",
              "api_secret": "EXAMPLE_API_SECRET_00000000000"
            }
          }
        }
      }
    }
  },
  "x-python-handler": "system.ApiKeyAPI",
  "x-python-route-line": 907,
  "x-audience": "user",
  "x-auth": "user",
  "x-reviewed": true,
  "requestBody": {
    "required": false,
    "content": {
      "application/json": {
        "schema": {
          "type": "object",
          "properties": {
            "uid": {
              "type": "integer",
              "format": "int64",
              "minimum": 1,
              "description": "要开通 API Key 的用户 ID。省略请求体或该字段时为当前登录用户；仅管理员可指定其他用户。"
            }
          },
          "additionalProperties": false
        },
        "example": {}
      }
    }
  },
  "parameters": [],
  "x-itecdn-source-reviewed": true,
  "extensions": [
    {
      "key": "x-python-handler",
      "value": "system.ApiKeyAPI"
    },
    {
      "key": "x-python-route-line",
      "value": 907
    },
    {
      "key": "x-audience",
      "value": "user"
    },
    {
      "key": "x-auth",
      "value": "user"
    },
    {
      "key": "x-reviewed",
      "value": true
    },
    {
      "key": "x-itecdn-source-reviewed",
      "value": true
    }
  ],
  "method": "post",
  "path": "/v1/api-key",
  "servers": [
    {
      "url": "https://master.example.invalid",
      "description": "不可调用的示例地址；复制示例后替换为自己的主控域名。"
    }
  ],
  "securitySchemes": {
    "AccessToken": {
      "type": "apiKey",
      "in": "header",
      "name": "access-token",
      "description": "通过登录接口获得的短期访问令牌。"
    },
    "ApiKey": {
      "type": "apiKey",
      "in": "header",
      "name": "api-key",
      "description": "API Key；必须和 api-secret 同时发送。"
    },
    "ApiSecret": {
      "type": "apiKey",
      "in": "header",
      "name": "api-secret",
      "description": "API Secret；必须和 api-key 同时发送。"
    }
  },
  "jsonRequestBodyExample": {
    "uid": 0
  },
  "info": {
    "title": "itecdn v5 用户 API",
    "version": "5.x",
    "description": "itecdn v5.x Python 主控用户 API。本文档不适用于 v6 或其它实现。"
  },
  "postman": {
    "name": "开通 API Key",
    "description": {
      "content": "为当前登录用户开通 API Key；管理员可通过可选的 `uid` 为指定用户开通。每个用户只能保存一组 API Key，已开通时不能重复创建。服务端随机生成 16 位 API Key 和 30 位 API Secret，并以明文返回；新记录的来源 IP 白名单初始为 null，即不限制调用来源 IP，后续可通过更新接口设置。调用方应立即安全保存响应，避免写入日志或暴露给无关方。",
      "type": "text/plain"
    },
    "url": {
      "path": [
        "v1",
        "api-key"
      ],
      "host": [
        "{{baseUrl}}"
      ],
      "query": [],
      "variable": []
    },
    "header": [
      {
        "key": "Content-Type",
        "value": "application/json"
      },
      {
        "key": "Accept",
        "value": "application/json"
      }
    ],
    "method": "POST",
    "body": {
      "mode": "raw",
      "raw": "",
      "options": {
        "raw": {
          "language": "json"
        }
      }
    },
    "auth": {
      "type": "apikey",
      "apikey": [
        {
          "type": "any",
          "value": "access-token",
          "key": "key"
        },
        {
          "type": "any",
          "value": "{{apiKey}}",
          "key": "value"
        },
        {
          "type": "any",
          "value": "header",
          "key": "in"
        }
      ]
    }
  }
}