{
  "openapi": "3.1.0",
  "info": {
    "title": "apipro 开放平台 API",
    "version": "1.0.0",
    "summary": "用程序读取你在 apipro 上的用量、费用与 API Key 清单。",
    "description": "鉴权：`Authorization: Bearer apk_…`，密钥在控制台「开放平台」创建，只显示一次，只带勾选的权限。时间：Unix 秒；入参也接受 RFC3339 或 `YYYY-MM-DD`（按 `utc_offset` 解释）。金额：`quota`（整数）· `amount`（6 位小数字符串，= quota / 500000）· `currency`（USD）。错误体：`{\"error\":{\"code\",\"message\",\"request_id\"}}`。"
  },
  "servers": [
    {
      "url": "/openapi/v1",
      "description": "当前站点"
    }
  ],
  "security": [
    {
      "apiKey": []
    }
  ],
  "tags": [
    {
      "name": "me",
      "description": "我是谁"
    },
    {
      "name": "usage",
      "description": "用量与费用（scope `user:usage:*`）"
    },
    {
      "name": "bills",
      "description": "账单：这段时间该付多少、怎么算出来的。/bills/preview 是实时算的草稿账单；已出账的单据将来住在 /bills/{bill_no}。"
    },
    {
      "name": "exports",
      "description": "异步导出（scope `user:usage:export`）"
    },
    {
      "name": "tokens",
      "description": "API Key 清单（scope `user:tokens:list`）"
    },
    {
      "name": "auth",
      "description": "`apipro login` 的浏览器授权与自吊销。start / token 免鉴权；revoke 用当前密钥调用。"
    }
  ],
  "paths": {
    "/me": {
      "get": {
        "tags": [
          "me"
        ],
        "operationId": "getMe",
        "summary": "当前密钥所属身份",
        "description": "免 scope。用来确认密钥能用、以及它代表谁。",
        "responses": {
          "200": {
            "description": "OK",
            "headers": {
              "X-Apipro-Key-Expires-At": {
                "$ref": "#/components/headers/KeyExpiresAt"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Me"
                },
                "example": {
                  "user_id": 99001,
                  "username": "acme",
                  "display_name": "Acme Corp",
                  "kind": "user",
                  "key": {
                    "id": 12,
                    "name": "cli-macbook",
                    "kind": "cli",
                    "scopes": [
                      "user:usage:summary",
                      "user:usage:list",
                      "user:usage:export",
                      "user:tokens:list"
                    ],
                    "expires_at": 1765000000
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/usage/summary": {
      "get": {
        "tags": [
          "usage"
        ],
        "operationId": "getUsageSummary",
        "summary": "用量与费用汇总",
        "description": "scope `user:usage:summary`。只统计成功（计费）请求。`group_by` 为空时返回一行总计；否则按时间桶 + 维度分组，行序为桶升序、维度升序。响应头 `X-Apipro-Data-Through` 是分析库实际覆盖到的时刻，`end_time` 超过它会被压回。",
        "parameters": [
          {
            "$ref": "#/components/parameters/StartTimeRequired"
          },
          {
            "$ref": "#/components/parameters/EndTimeRequired"
          },
          {
            "name": "group_by",
            "in": "query",
            "description": "逗号列表：至多一个时间桶（`hour`/`day`/`month`，必须在首位）+ 至多两个维度（`model`/`token`/`group`）。例：`day,model`。",
            "schema": {
              "type": "string"
            },
            "example": "day,model"
          },
          {
            "$ref": "#/components/parameters/UtcOffset"
          },
          {
            "$ref": "#/components/parameters/ModelFilter"
          },
          {
            "$ref": "#/components/parameters/TokenNameFilter"
          },
          {
            "$ref": "#/components/parameters/GroupFilter"
          },
          {
            "name": "page_size",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 10000,
              "default": 1000
            },
            "example": 100
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "headers": {
              "X-Apipro-Data-Through": {
                "$ref": "#/components/headers/DataThrough"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data",
                    "next_cursor",
                    "meta"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/SummaryRow"
                      }
                    },
                    "next_cursor": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "meta": {
                      "$ref": "#/components/schemas/SummaryMeta"
                    }
                  }
                },
                "example": {
                  "data": [
                    {
                      "bucket_start": 1756656000,
                      "bucket_end": 1756742400,
                      "model": "gpt-4o",
                      "requests": 120,
                      "input_tokens": 240000,
                      "cache_read_tokens": 30000,
                      "cache_write_5m_tokens": 0,
                      "cache_write_1h_tokens": 0,
                      "output_tokens": 18000,
                      "total_tokens": 258000,
                      "input_image_tokens": 0,
                      "input_audio_tokens": 0,
                      "output_audio_tokens": 0,
                      "tool_calls": {
                        "web_search": 0,
                        "web_search_preview": 0,
                        "google_search": 0,
                        "file_search": 0,
                        "image_generation": 0
                      },
                      "quota": 1235000,
                      "amount": "2.470000",
                      "currency": "USD"
                    }
                  ],
                  "next_cursor": null,
                  "meta": {
                    "start_time": 1756656000,
                    "end_time": 1756742400,
                    "group_by": [
                      "day",
                      "model"
                    ],
                    "utc_offset": "+08:00",
                    "data_through": 1756800000,
                    "generated_at": 1756800123
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/usage": {
      "get": {
        "tags": [
          "usage"
        ],
        "operationId": "listUsage",
        "summary": "逐条请求记录（成功与失败）",
        "description": "scope `user:usage:list`。成功行与失败行字段集合相同：失败行用量与费用全 0，`status_code`/`error_code`/`message` 有值。带 `any_id` 时可省时间（默认最近 90 天）且 `status` 默认 `all`；否则时间必填、跨度 ≤ 7 天。排序 `time ASC, id ASC`。",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string",
              "enum": [
                "success",
                "error",
                "all"
              ]
            },
            "description": "默认 `success`；带 `any_id` 时默认 `all`。",
            "example": "all"
          },
          {
            "name": "any_id",
            "in": "query",
            "schema": {
              "type": "string",
              "maxLength": 128
            },
            "description": "一个框查所有号：我方 `request_id`、你传的 `client_request_id`、API 响应体里的 `id`、上游请求号。只在你自己的数据里找。"
          },
          {
            "$ref": "#/components/parameters/StartTime"
          },
          {
            "$ref": "#/components/parameters/EndTime"
          },
          {
            "$ref": "#/components/parameters/UtcOffset"
          },
          {
            "$ref": "#/components/parameters/ModelFilter"
          },
          {
            "$ref": "#/components/parameters/TokenNameFilter"
          },
          {
            "$ref": "#/components/parameters/GroupFilter"
          },
          {
            "name": "status_code",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号列表，≤ 20 个，每个 0–999；带它时只返回匹配的失败行。例：`429,500`。"
          },
          {
            "name": "page_size",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 1000,
              "default": 100
            },
            "example": 20
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "headers": {
              "X-Apipro-Data-Through": {
                "$ref": "#/components/headers/DataThrough"
              }
            },
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data",
                    "next_cursor",
                    "meta"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/UsageRecord"
                      }
                    },
                    "next_cursor": {
                      "type": [
                        "string",
                        "null"
                      ]
                    },
                    "meta": {
                      "$ref": "#/components/schemas/ListMeta"
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/usage/exports": {
      "post": {
        "tags": [
          "exports"
        ],
        "operationId": "createUsageExport",
        "summary": "提交明细导出任务",
        "description": "scope `user:usage:export`。导出**逐条请求明细**（列同 `GET /usage`），三种格式列口径一致：`csv` / `jsonl` 给程序读，`xlsx` 是同样的列（英文键、Unix 秒）加一页说明。区间在提交时按数据水位冻结。同时最多 3 个进行中任务，超出返回 `429 CONCURRENCY_LIMITED`；xlsx 上限 200 万行。\n\n**要账单请用 `POST /bills/exports`**：那份产物按日/月聚合，带原价单价与可复算的 Excel 公式。",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/ExportRequest"
              },
              "example": {
                "format": "csv",
                "start_time": "2026-11-29",
                "end_time": "2026-12-05",
                "utc_offset": "+08:00",
                "status": "all"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "已受理，返回任务对象（status=pending）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExportJob"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "422": {
            "description": "导出行数超过上限（`ROW_LIMIT_EXCEEDED`），请缩小区间",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      },
      "get": {
        "tags": [
          "exports"
        ],
        "operationId": "listUsageExports",
        "summary": "最近的导出任务",
        "description": "scope `user:usage:export`。本用户最近 100 个任务，按提交时间降序。",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/ExportJob"
                      }
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/usage/exports/{job_id}": {
      "get": {
        "tags": [
          "exports"
        ],
        "operationId": "getUsageExport",
        "summary": "查询导出任务",
        "parameters": [
          {
            "$ref": "#/components/parameters/JobId"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExportJob"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/usage/exports/{job_id}/cancel": {
      "post": {
        "tags": [
          "exports"
        ],
        "operationId": "cancelUsageExport",
        "summary": "取消导出任务",
        "description": "只对 `pending`/`processing` 有效，否则 `400 INVALID_ARGUMENT`。",
        "parameters": [
          {
            "$ref": "#/components/parameters/JobId"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/ExportJob"
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/usage/exports/{job_id}/file": {
      "get": {
        "tags": [
          "exports"
        ],
        "operationId": "downloadUsageExport",
        "summary": "下载导出文件",
        "description": "鉴权二选一：`Authorization` 头，或任务对象 `file_url` 自带的 `token`（24 小时内有效）。CSV：UTF-8 无 BOM、LF、逗号，`tool_calls` 摊平为 `tool_web_search` 等列；JSONL 每行一个对象。",
        "security": [
          {
            "apiKey": []
          },
          {}
        ],
        "parameters": [
          {
            "$ref": "#/components/parameters/JobId"
          },
          {
            "name": "token",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "`file_url` 里的签名。"
          }
        ],
        "responses": {
          "200": {
            "description": "文件内容",
            "headers": {
              "Content-Disposition": {
                "schema": {
                  "type": "string"
                }
              },
              "X-Apipro-File-Sha256": {
                "schema": {
                  "type": "string"
                },
                "description": "文件 SHA-256，用于校验。"
              }
            },
            "content": {
              "text/csv": {
                "schema": {
                  "type": "string"
                }
              },
              "application/x-ndjson": {
                "schema": {
                  "type": "string"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "410": {
            "description": "文件已过保留期（`JOB_EXPIRED`）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          }
        }
      }
    },
    "/bills/preview": {
      "get": {
        "tags": [
          "bills"
        ],
        "operationId": "listBillPreview",
        "summary": "草稿账单的行",
        "description": "scope `user:bill:list`。实时从分析库算出这段时间的账单行：一行一个价，带用量、**原价**单价、多模附加倍率、工具附加费、分组倍率与费用。跨度上限：`month` ≤ 24 个月，`day` ≤ 366 天，`hour` ≤ 31 天。\n\n单价一律是模型定价原价，不含分组倍率——折扣只出现在 `group_ratio` 一列，`original_amount = amount ÷ group_ratio` 在每一行都成立。",
        "parameters": [
          {
            "name": "period",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "month",
                "day",
                "hour"
              ]
            },
            "description": "账单粒度。没有默认值——按月与按小时的行数差两个数量级。"
          },
          {
            "name": "start_time",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/TimeInput"
            }
          },
          {
            "name": "end_time",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/TimeInput"
            },
            "description": "左闭右开。"
          },
          {
            "name": "utc_offset",
            "in": "query",
            "schema": {
              "type": "string",
              "default": "+08:00"
            },
            "description": "日切时区。"
          },
          {
            "name": "model",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "token_name",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "group",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "page_size",
            "in": "query",
            "schema": {
              "type": "integer",
              "default": 1000,
              "maximum": 10000
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "上一页返回的 `next_cursor`。"
          }
        ],
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillPreviewPage"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/bills/preview/summary": {
      "get": {
        "tags": [
          "bills"
        ],
        "operationId": "getBillPreviewSummary",
        "summary": "草稿账单汇总",
        "description": "scope `user:bill:summary`。与 `GET /bills/preview` 参数完全相同，返回合计、折扣与（可选的）分组小计。",
        "parameters": [
          {
            "name": "period",
            "in": "query",
            "required": true,
            "schema": {
              "type": "string",
              "enum": [
                "month",
                "day",
                "hour"
              ]
            },
            "description": "账单粒度。没有默认值——按月与按小时的行数差两个数量级。"
          },
          {
            "name": "start_time",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/TimeInput"
            }
          },
          {
            "name": "end_time",
            "in": "query",
            "required": true,
            "schema": {
              "$ref": "#/components/schemas/TimeInput"
            },
            "description": "左闭右开。"
          },
          {
            "name": "utc_offset",
            "in": "query",
            "schema": {
              "type": "string",
              "default": "+08:00"
            },
            "description": "日切时区。"
          },
          {
            "name": "model",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "token_name",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "group",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔。"
          },
          {
            "name": "group_by",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号分隔，可选 `model` / `group`；不给只回 `totals`。"
          }
        ],
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillSummary"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/bills/exports": {
      "post": {
        "tags": [
          "bills"
        ],
        "operationId": "createBillExport",
        "summary": "提交账单导出",
        "description": "scope `user:bill:export`。默认异步，返回任务对象。\n\n`xlsx` 是完整账单工作簿——**费用 / 定价 / 说明** 三页，「原价」与「费用公式参考」两列是真 Excel 公式，客户点开单元格就能把费用算回来；`csv` / `jsonl` 只出「费用」页那张平表（字段名同 `GET /bills/preview`，无公式）。\n\n`wait=true` 表示愿意等：服务端按规模决定走同步还是异步——同步时**直接回文件流**（`Content-Disposition` 带文件名），超出阈值仍回任务对象，客户端继续轮询。并发上限与日配额与用量导出合并计数。\n\n本期导出的一律是**草稿账单**（`bill_status=draft`）：客户自助导出，平台尚未确认空返与调整，不作结算依据。",
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/BillExportRequest"
              },
              "example": {
                "period": "month",
                "format": "xlsx",
                "start_time": "2026-11-29",
                "end_time": "2026-12-05",
                "utc_offset": "+08:00",
                "wait": false
              }
            }
          }
        },
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "已受理，返回任务对象（`status=pending`）；或 `wait=true` 且规模在阈值内时，直接返回账单文件。",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillExportJob"
                }
              },
              "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "422": {
            "description": "行数超上限（`ROW_LIMIT_EXCEEDED`），请缩小区间或换更粗的粒度",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      },
      "get": {
        "tags": [
          "bills"
        ],
        "operationId": "listBillExports",
        "summary": "最近的账单导出任务",
        "description": "scope `user:bill:export`。本用户最近 100 个账单导出任务，按提交时间降序。",
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/BillExportJob"
                      }
                    }
                  }
                }
              }
            }
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/bills/exports/{job_id}": {
      "get": {
        "tags": [
          "bills"
        ],
        "operationId": "getBillExport",
        "summary": "账单导出任务状态",
        "description": "scope `user:bill:export`。",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillExportJob"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/bills/exports/{job_id}/cancel": {
      "post": {
        "tags": [
          "bills"
        ],
        "operationId": "cancelBillExport",
        "summary": "取消账单导出",
        "description": "scope `user:bill:export`。只对 `pending` / `processing` 有效。",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ],
        "responses": {
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/BillExportJob"
                }
              }
            }
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/bills/exports/{job_id}/file": {
      "get": {
        "tags": [
          "bills"
        ],
        "operationId": "downloadBillExport",
        "summary": "下载账单产物",
        "description": "授权二选一：URL 里的签名 `token`，或 `Authorization` 头（须持有 `user:bill:export`）。响应带 `Content-Disposition`（中文名走 RFC 5987）与 `X-Apipro-File-Sha256`。",
        "parameters": [
          {
            "name": "job_id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string"
            }
          },
          {
            "name": "token",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "任务对象 `file_url` 里自带。"
          }
        ],
        "responses": {
          "200": {
            "description": "账单文件",
            "content": {
              "application/octet-stream": {
                "schema": {
                  "type": "string",
                  "format": "binary"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "410": {
            "description": "产物已过期（`JOB_EXPIRED`）",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Error"
                }
              }
            }
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/tokens": {
      "get": {
        "tags": [
          "tokens"
        ],
        "operationId": "listTokens",
        "summary": "API Key 清单",
        "description": "scope `user:tokens:list`。不返回明文 key、额度与消费。",
        "parameters": [
          {
            "name": "status",
            "in": "query",
            "schema": {
              "type": "string"
            },
            "description": "逗号列表：`enabled`/`disabled`/`expired`/`exhausted`。"
          },
          {
            "name": "page_size",
            "in": "query",
            "schema": {
              "type": "integer",
              "minimum": 1,
              "maximum": 200,
              "default": 100
            },
            "example": 20
          },
          {
            "$ref": "#/components/parameters/Cursor"
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "required": [
                    "data",
                    "next_cursor"
                  ],
                  "properties": {
                    "data": {
                      "type": "array",
                      "items": {
                        "$ref": "#/components/schemas/Token"
                      }
                    },
                    "next_cursor": {
                      "type": [
                        "string",
                        "null"
                      ]
                    }
                  }
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          }
        }
      }
    },
    "/tokens/{id}": {
      "get": {
        "tags": [
          "tokens"
        ],
        "operationId": "getToken",
        "summary": "单个 API Key",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "integer"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/Token"
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          },
          "403": {
            "$ref": "#/components/responses/Forbidden"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          }
        }
      }
    },
    "/auth/cli/start": {
      "post": {
        "tags": [
          "auth"
        ],
        "operationId": "startCliAuth",
        "summary": "发起 CLI 登录授权",
        "description": "`apipro login` 第一步：登记请求，拿到 `request_id` 与验证码。免鉴权；每地址 10 分钟内最多 20 次。",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CLIAuthStartRequest"
              },
              "example": {
                "client": {
                  "name": "apipro",
                  "version": "0.1.0",
                  "os": "darwin",
                  "arch": "arm64",
                  "hostname": "macbook"
                },
                "code_challenge": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CLIAuthStartResponse"
                },
                "example": {
                  "request_id": "3f9c…",
                  "user_code": "WDJB-MJHT",
                  "verification_uri": "https://api.apipro.ai/openapi/cli/auth",
                  "verification_uri_complete": "https://api.apipro.ai/openapi/cli/auth?code=WDJB-MJHT",
                  "expires_in": 600,
                  "interval": 3
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          },
          "503": {
            "$ref": "#/components/responses/Unavailable"
          }
        }
      }
    },
    "/auth/cli/token": {
      "post": {
        "tags": [
          "auth"
        ],
        "operationId": "pollCliAuth",
        "summary": "轮询登录授权结果",
        "description": "按 `interval` 轮询。用户在授权页批准后，服务端在这一次响应里签发密钥并返回明文，随后请求作废（再调是 404）。太快回 429 `SLOW_DOWN`；`code_verifier` 不符回 400 并作废请求。",
        "security": [],
        "requestBody": {
          "required": true,
          "content": {
            "application/json": {
              "schema": {
                "$ref": "#/components/schemas/CLIAuthTokenRequest"
              },
              "example": {
                "request_id": "3f9c…",
                "code_verifier": "dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk"
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "$ref": "#/components/schemas/CLIAuthTokenResponse"
                },
                "examples": {
                  "pending": {
                    "value": {
                      "status": "pending"
                    }
                  },
                  "approved": {
                    "value": {
                      "status": "approved",
                      "api_key": "apk_…",
                      "key": {
                        "id": 12,
                        "name": "cli-macbook",
                        "kind": "cli",
                        "scopes": [
                          "user:usage:summary",
                          "user:usage:list",
                          "user:usage:export",
                          "user:tokens:list"
                        ],
                        "expires_at": 1765000000
                      },
                      "principal": {
                        "user_id": 99001,
                        "username": "acme",
                        "display_name": "Acme Corp",
                        "kind": "user"
                      }
                    }
                  }
                }
              }
            }
          },
          "400": {
            "$ref": "#/components/responses/InvalidArgument"
          },
          "404": {
            "$ref": "#/components/responses/NotFound"
          },
          "429": {
            "$ref": "#/components/responses/RateLimited"
          }
        }
      }
    },
    "/auth/revoke": {
      "post": {
        "tags": [
          "auth"
        ],
        "operationId": "revokeSelf",
        "summary": "吊销当前密钥",
        "description": "免 scope：任何密钥都可以吊销自己（`apipro logout --revoke`）。之后再用它调用一律 401 `KEY_REVOKED`。",
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "revoked": {
                      "type": "boolean"
                    },
                    "key_id": {
                      "type": "integer"
                    }
                  }
                },
                "example": {
                  "revoked": true,
                  "key_id": 12
                }
              }
            }
          },
          "401": {
            "$ref": "#/components/responses/Unauthorized"
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "apiKey": {
        "type": "http",
        "scheme": "bearer",
        "description": "`Authorization: Bearer apk_…`。密钥在控制台「开放平台」页创建，只显示一次。"
      }
    },
    "headers": {
      "KeyExpiresAt": {
        "description": "当前密钥的过期时刻（秒）。",
        "schema": {
          "type": "integer"
        }
      },
      "DataThrough": {
        "description": "分析库实际覆盖到的时刻（秒）。",
        "schema": {
          "type": "integer"
        }
      }
    },
    "parameters": {
      "StartTimeRequired": {
        "name": "start_time",
        "in": "query",
        "required": true,
        "schema": {
          "$ref": "#/components/schemas/TimeInput"
        },
        "description": "含。",
        "example": "2026-11-27"
      },
      "EndTimeRequired": {
        "name": "end_time",
        "in": "query",
        "required": true,
        "schema": {
          "$ref": "#/components/schemas/TimeInput"
        },
        "description": "不含。",
        "example": "2026-12-05"
      },
      "StartTime": {
        "name": "start_time",
        "in": "query",
        "schema": {
          "$ref": "#/components/schemas/TimeInput"
        },
        "description": "含。无 `any_id` 时必填。",
        "example": "2026-11-29"
      },
      "EndTime": {
        "name": "end_time",
        "in": "query",
        "schema": {
          "$ref": "#/components/schemas/TimeInput"
        },
        "description": "不含。无 `any_id` 时必填。",
        "example": "2026-12-05"
      },
      "UtcOffset": {
        "name": "utc_offset",
        "in": "query",
        "schema": {
          "type": "string",
          "pattern": "^[+-]\\d{2}:\\d{2}$",
          "default": "+08:00"
        },
        "description": "决定日期入参的时区与分桶边界。",
        "example": "+08:00"
      },
      "ModelFilter": {
        "name": "model",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "逗号列表，≤ 50 个。",
        "example": ""
      },
      "TokenNameFilter": {
        "name": "token_name",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "逗号列表，≤ 50 个，每个 ≤ 191 字符。",
        "example": ""
      },
      "GroupFilter": {
        "name": "group",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "逗号列表，≤ 20 个，每个 ≤ 191 字符。",
        "example": ""
      },
      "Cursor": {
        "name": "cursor",
        "in": "query",
        "schema": {
          "type": "string"
        },
        "description": "上一页的 `next_cursor`。筛选条件变了游标即失效（400）。"
      },
      "JobId": {
        "name": "job_id",
        "in": "path",
        "required": true,
        "schema": {
          "type": "string"
        }
      }
    },
    "responses": {
      "InvalidArgument": {
        "description": "参数错误（`INVALID_ARGUMENT` / `INVALID_TIME_RANGE` / `INVALID_GROUP_BY` / `INVALID_CURSOR`）",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unauthorized": {
        "description": "密钥缺失、无效、已吊销或已过期（`UNAUTHORIZED` / `KEY_REVOKED` / `KEY_EXPIRED`）",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Forbidden": {
        "description": "密钥没有该端点的 scope（`SCOPE_INSUFFICIENT`）",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "NotFound": {
        "description": "不存在或不属于你（`NOT_FOUND` / 导出任务 `JOB_NOT_FOUND`）",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "RateLimited": {
        "description": "超过每密钥每分钟上限（`RATE_LIMITED`，看 `Retry-After`）或导出并发上限 3（`CONCURRENCY_LIMITED`）",
        "headers": {
          "Retry-After": {
            "schema": {
              "type": "integer"
            }
          }
        },
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      },
      "Unavailable": {
        "description": "分析库暂不可用（`DATA_SOURCE_UNAVAILABLE`），稍后重试",
        "content": {
          "application/json": {
            "schema": {
              "$ref": "#/components/schemas/Error"
            }
          }
        }
      }
    },
    "schemas": {
      "TimeInput": {
        "oneOf": [
          {
            "type": "integer",
            "description": "Unix 秒"
          },
          {
            "type": "string",
            "description": "RFC3339 或 `YYYY-MM-DD`（按 `utc_offset` 解释为当日 0 点）"
          }
        ]
      },
      "Error": {
        "type": "object",
        "required": [
          "error"
        ],
        "properties": {
          "error": {
            "type": "object",
            "required": [
              "code",
              "message",
              "request_id"
            ],
            "properties": {
              "code": {
                "type": "string",
                "enum": [
                  "INVALID_ARGUMENT",
                  "INVALID_TIME_RANGE",
                  "INVALID_GROUP_BY",
                  "INVALID_CURSOR",
                  "UNAUTHORIZED",
                  "KEY_EXPIRED",
                  "KEY_REVOKED",
                  "FORBIDDEN",
                  "SCOPE_INSUFFICIENT",
                  "NOT_FOUND",
                  "JOB_NOT_FOUND",
                  "JOB_EXPIRED",
                  "ROW_LIMIT_EXCEEDED",
                  "RATE_LIMITED",
                  "CONCURRENCY_LIMITED",
                  "DATA_SOURCE_UNAVAILABLE",
                  "INTERNAL_ERROR"
                ]
              },
              "message": {
                "type": "string"
              },
              "request_id": {
                "type": "string"
              }
            }
          }
        }
      },
      "Me": {
        "type": "object",
        "required": [
          "user_id",
          "username",
          "display_name",
          "kind"
        ],
        "properties": {
          "user_id": {
            "type": "integer"
          },
          "username": {
            "type": "string"
          },
          "display_name": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "user",
              "org_owner",
              "root"
            ]
          },
          "key": {
            "$ref": "#/components/schemas/MeKey"
          }
        }
      },
      "ToolCalls": {
        "type": "object",
        "required": [
          "web_search",
          "web_search_preview",
          "google_search",
          "file_search",
          "image_generation"
        ],
        "properties": {
          "web_search": {
            "type": "integer"
          },
          "web_search_preview": {
            "type": "integer"
          },
          "google_search": {
            "type": "integer"
          },
          "file_search": {
            "type": "integer"
          },
          "image_generation": {
            "type": "integer"
          }
        }
      },
      "UsageFields": {
        "type": "object",
        "description": "`input_tokens` 是输入总量，已含缓存与图片/音频 token；`cache_*` 与 `input_image_tokens`/`*_audio_tokens` 都是它的子集，只拆出来供复算，不参与求和。`total_tokens = input_tokens + output_tokens`。",
        "required": [
          "requests",
          "input_tokens",
          "cache_read_tokens",
          "cache_write_5m_tokens",
          "cache_write_1h_tokens",
          "output_tokens",
          "total_tokens",
          "input_image_tokens",
          "input_audio_tokens",
          "output_audio_tokens",
          "tool_calls",
          "quota",
          "amount",
          "currency"
        ],
        "properties": {
          "requests": {
            "type": "integer"
          },
          "input_tokens": {
            "type": "integer"
          },
          "cache_read_tokens": {
            "type": "integer"
          },
          "cache_write_5m_tokens": {
            "type": "integer"
          },
          "cache_write_1h_tokens": {
            "type": "integer"
          },
          "output_tokens": {
            "type": "integer"
          },
          "total_tokens": {
            "type": "integer"
          },
          "input_image_tokens": {
            "type": "integer"
          },
          "input_audio_tokens": {
            "type": "integer"
          },
          "output_audio_tokens": {
            "type": "integer"
          },
          "tool_calls": {
            "$ref": "#/components/schemas/ToolCalls"
          },
          "quota": {
            "type": "integer",
            "description": "额度单位；amount = quota / 500000。"
          },
          "amount": {
            "type": "string",
            "description": "十进制字符串，6 位小数，= quota / 500000。",
            "example": "2.470000"
          },
          "currency": {
            "type": "string",
            "const": "USD"
          }
        }
      },
      "SummaryRow": {
        "allOf": [
          {
            "type": "object",
            "properties": {
              "bucket_start": {
                "type": "integer",
                "description": "秒；仅 `group_by` 含时间桶时出现。"
              },
              "bucket_end": {
                "type": "integer",
                "description": "秒，不含；仅 `group_by` 含时间桶时出现。"
              },
              "model": {
                "type": "string",
                "description": "仅 `group_by` 含 `model` 时出现。"
              },
              "token_name": {
                "type": "string",
                "description": "仅 `group_by` 含 `token` 时出现。"
              },
              "group": {
                "type": "string",
                "description": "仅 `group_by` 含 `group` 时出现。"
              }
            }
          },
          {
            "$ref": "#/components/schemas/UsageFields"
          }
        ]
      },
      "SummaryMeta": {
        "type": "object",
        "required": [
          "start_time",
          "end_time",
          "group_by",
          "utc_offset",
          "data_through",
          "generated_at"
        ],
        "properties": {
          "start_time": {
            "type": "integer"
          },
          "end_time": {
            "type": "integer",
            "description": "生效值（可能被水位压回）。"
          },
          "group_by": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "utc_offset": {
            "type": "string"
          },
          "data_through": {
            "type": "integer"
          },
          "generated_at": {
            "type": "integer"
          }
        }
      },
      "ListMeta": {
        "type": "object",
        "required": [
          "start_time",
          "end_time",
          "status",
          "data_through",
          "generated_at"
        ],
        "properties": {
          "start_time": {
            "type": "integer"
          },
          "end_time": {
            "type": "integer"
          },
          "status": {
            "type": "string",
            "enum": [
              "success",
              "error",
              "all"
            ]
          },
          "data_through": {
            "type": "integer"
          },
          "generated_at": {
            "type": "integer"
          }
        }
      },
      "UsageRecord": {
        "allOf": [
          {
            "type": "object",
            "required": [
              "status",
              "time",
              "request_id",
              "client_request_id",
              "token_name",
              "group",
              "model",
              "is_stream",
              "duration_seconds",
              "status_code",
              "error_code",
              "message"
            ],
            "properties": {
              "status": {
                "type": "string",
                "enum": [
                  "success",
                  "error"
                ]
              },
              "time": {
                "type": "integer",
                "description": "秒。"
              },
              "request_id": {
                "type": "string",
                "description": "我方请求号。"
              },
              "client_request_id": {
                "type": "string",
                "description": "你带来的号，缺失为空串。"
              },
              "token_name": {
                "type": "string"
              },
              "group": {
                "type": "string"
              },
              "model": {
                "type": "string"
              },
              "is_stream": {
                "type": "boolean"
              },
              "duration_seconds": {
                "type": "integer"
              },
              "status_code": {
                "type": "integer",
                "description": "失败行：我方对客状态码；成功行 0。"
              },
              "error_code": {
                "type": "string",
                "description": "失败行：我方错误分类；成功行空串。"
              },
              "message": {
                "type": "string",
                "description": "失败行：对客文案；成功行空串。"
              }
            }
          },
          {
            "$ref": "#/components/schemas/UsageFields"
          }
        ]
      },
      "ExportRequest": {
        "type": "object",
        "required": [
          "start_time",
          "end_time"
        ],
        "properties": {
          "format": {
            "type": "string",
            "enum": [
              "csv",
              "jsonl",
              "xlsx"
            ],
            "default": "csv",
            "description": "三者列口径一致：`csv` / `jsonl` 给程序读，`xlsx` 是同样的列加一页说明。"
          },
          "start_time": {
            "$ref": "#/components/schemas/TimeInput"
          },
          "end_time": {
            "$ref": "#/components/schemas/TimeInput"
          },
          "utc_offset": {
            "type": "string",
            "default": "+08:00"
          },
          "status": {
            "type": "string",
            "enum": [
              "success",
              "error",
              "all"
            ],
            "description": "默认 `success`。"
          },
          "model": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "token_name": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "group": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      },
      "ExportJob": {
        "type": "object",
        "required": [
          "job_id",
          "format",
          "status",
          "start_time",
          "end_time",
          "utc_offset",
          "submitted_at",
          "started_at",
          "completed_at",
          "row_count",
          "file_size_bytes",
          "file_sha256",
          "file_url",
          "file_expires_at",
          "error"
        ],
        "properties": {
          "job_id": {
            "type": "string"
          },
          "format": {
            "type": "string",
            "enum": [
              "csv",
              "jsonl",
              "xlsx"
            ]
          },
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "processing",
              "completed",
              "failed",
              "canceled",
              "expired"
            ]
          },
          "start_time": {
            "type": "integer"
          },
          "end_time": {
            "type": "integer",
            "description": "提交时按水位冻结后的生效值。"
          },
          "utc_offset": {
            "type": "string"
          },
          "status_filter": {
            "type": "string"
          },
          "submitted_at": {
            "type": "integer"
          },
          "started_at": {
            "type": "integer",
            "description": "未到为 0。"
          },
          "completed_at": {
            "type": "integer",
            "description": "未到为 0。"
          },
          "row_count": {
            "type": "integer"
          },
          "file_size_bytes": {
            "type": "integer"
          },
          "file_sha256": {
            "type": "string"
          },
          "file_url": {
            "type": "string",
            "description": "完成后；自带签名 `token`，24 小时内有效。"
          },
          "file_expires_at": {
            "type": "integer"
          },
          "error": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      },
      "Token": {
        "type": "object",
        "required": [
          "id",
          "name",
          "key_masked",
          "status",
          "groups",
          "allow_ips",
          "expires_at",
          "created_at",
          "last_used_at"
        ],
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "key_masked": {
            "type": "string",
            "description": "与控制台列表同一脱敏。"
          },
          "status": {
            "type": "string",
            "enum": [
              "enabled",
              "disabled",
              "expired",
              "exhausted"
            ]
          },
          "groups": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "空数组 = 随用户分组。"
          },
          "allow_ips": {
            "type": "array",
            "items": {
              "type": "string"
            },
            "description": "空数组即不限。"
          },
          "expires_at": {
            "type": "integer",
            "description": "秒；0 永不过期。"
          },
          "created_at": {
            "type": "integer"
          },
          "last_used_at": {
            "type": "integer"
          }
        }
      },
      "CLIClient": {
        "type": "object",
        "description": "发起登录的 CLI 的自述，授权页原样展示给人核对。",
        "properties": {
          "name": {
            "type": "string",
            "maxLength": 32,
            "example": "apipro"
          },
          "version": {
            "type": "string",
            "maxLength": 32,
            "example": "0.1.0"
          },
          "os": {
            "type": "string",
            "maxLength": 16,
            "example": "darwin"
          },
          "arch": {
            "type": "string",
            "maxLength": 16,
            "example": "arm64"
          },
          "hostname": {
            "type": "string",
            "maxLength": 64,
            "example": "macbook"
          }
        }
      },
      "CLIAuthStartRequest": {
        "type": "object",
        "required": [
          "code_challenge"
        ],
        "properties": {
          "client": {
            "$ref": "#/components/schemas/CLIClient"
          },
          "code_challenge": {
            "type": "string",
            "description": "base64url(sha256(code_verifier))，43 字符（PKCE S256）。",
            "example": "E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM"
          }
        }
      },
      "CLIAuthStartResponse": {
        "type": "object",
        "required": [
          "request_id",
          "user_code",
          "verification_uri",
          "verification_uri_complete",
          "expires_in",
          "interval"
        ],
        "properties": {
          "request_id": {
            "type": "string",
            "description": "只给 CLI 的请求标识（64 位十六进制），轮询时带上。"
          },
          "user_code": {
            "type": "string",
            "description": "给人核对的验证码，XXXX-XXXX。",
            "example": "WDJB-MJHT"
          },
          "verification_uri": {
            "type": "string",
            "format": "uri"
          },
          "verification_uri_complete": {
            "type": "string",
            "format": "uri",
            "description": "带验证码的授权页地址，CLI 直接打开它。"
          },
          "expires_in": {
            "type": "integer",
            "description": "秒。",
            "example": 600
          },
          "interval": {
            "type": "integer",
            "description": "建议的轮询间隔（秒）。",
            "example": 3
          }
        }
      },
      "CLIAuthTokenRequest": {
        "type": "object",
        "required": [
          "request_id",
          "code_verifier"
        ],
        "properties": {
          "request_id": {
            "type": "string"
          },
          "code_verifier": {
            "type": "string",
            "description": "start 时那个 challenge 对应的 verifier（43–128 字符）。"
          }
        }
      },
      "MeKey": {
        "type": "object",
        "description": "调用者自己这把密钥的元数据。",
        "properties": {
          "id": {
            "type": "integer"
          },
          "name": {
            "type": "string"
          },
          "kind": {
            "type": "string",
            "enum": [
              "normal",
              "debug",
              "cli"
            ]
          },
          "scopes": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "expires_at": {
            "type": "integer",
            "description": "秒。"
          }
        }
      },
      "CLIAuthTokenResponse": {
        "type": "object",
        "required": [
          "status"
        ],
        "properties": {
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "approved",
              "denied",
              "expired"
            ]
          },
          "api_key": {
            "type": "string",
            "description": "status = approved 时才有，只出现这一次。"
          },
          "key": {
            "$ref": "#/components/schemas/MeKey"
          },
          "principal": {
            "$ref": "#/components/schemas/Me"
          }
        }
      },
      "BillLine": {
        "type": "object",
        "description": "一行账单：一个时间桶里、一套定价下的全部流量。行粒度 = 时间桶 × 模型 × 令牌 × 分组 × 定价版本 × 分组倍率 × 多模附加倍率 × 行类型——凡是影响单价的字段都在里面，所以一行之内所有请求共用同一套单价，账单能逐行复算。",
        "properties": {
          "bucket_start": {
            "type": "integer"
          },
          "bucket_end": {
            "type": "integer",
            "description": "左闭右开。"
          },
          "date": {
            "type": "string",
            "description": "按粒度渲染的日期：`2026-09`、`2026-09-03` 或 `2026-09-03 14:00`。"
          },
          "type": {
            "type": "string",
            "description": "`消耗` / `退款`（各列为负）/ `违规扣费` / `零计费`。"
          },
          "user_id": {
            "type": "integer"
          },
          "user_name": {
            "type": "string"
          },
          "model": {
            "type": "string"
          },
          "token_name": {
            "type": "string"
          },
          "group": {
            "type": "string"
          },
          "billing_mode": {
            "type": "string",
            "enum": [
              "ratio",
              "tiered_expr",
              "fixed_price"
            ],
            "description": "按量计费 / 阶梯计价 / 按次计费。"
          },
          "matched_tier": {
            "type": "string",
            "description": "阶梯计价本行命中的档位。"
          },
          "requests": {
            "type": "integer"
          },
          "input_tokens": {
            "type": "integer",
            "description": "计费口径的纯输入：已刨除缓存读取 / 写入与图片、音频——那几类各按自己的单价单独计费。"
          },
          "output_tokens": {
            "type": "integer"
          },
          "cache_read_tokens": {
            "type": "integer"
          },
          "cache_write_tokens": {
            "type": "integer",
            "description": "上游未区分 TTL 的写入。与 5m / 1h 互不重叠。"
          },
          "cache_write_5m_tokens": {
            "type": "integer"
          },
          "cache_write_1h_tokens": {
            "type": "integer"
          },
          "input_image_tokens": {
            "type": "integer"
          },
          "input_audio_tokens": {
            "type": "integer"
          },
          "input_price": {
            "type": "number",
            "description": "$/1M，**原价**，不含分组倍率。下同。"
          },
          "output_price": {
            "type": "number"
          },
          "cache_read_price": {
            "type": "number"
          },
          "cache_write_price": {
            "type": "number"
          },
          "cache_write_5m_price": {
            "type": "number"
          },
          "cache_write_1h_price": {
            "type": "number"
          },
          "input_image_price": {
            "type": "number"
          },
          "input_audio_price": {
            "type": "number"
          },
          "fixed_price": {
            "type": "number",
            "description": "$/次，按次计费用。"
          },
          "extra_ratio": {
            "type": "number",
            "description": "多模附加倍率之积（出图张数、视频时长、分辨率…），无则为 1。"
          },
          "extra_ratio_detail": {
            "type": "string",
            "description": "倍率明细的可读渲染，如 `n×2, seconds×8`。"
          },
          "group_ratio": {
            "type": "number",
            "description": "分组折扣倍率，小于 1 为折扣。"
          },
          "tool_surcharge": {
            "type": "string",
            "description": "内置工具附加费，**原价口径**（不含分组倍率），六位小数字符串。"
          },
          "quota": {
            "type": "integer",
            "description": "本行扣的内部额度，账目权威值。"
          },
          "amount": {
            "type": "string",
            "description": "= quota ÷ 500000，六位小数字符串。"
          },
          "original_amount": {
            "type": "string",
            "description": "未享分组折扣前的目录价。`original_amount × group_ratio ≈ amount`。"
          },
          "currency": {
            "type": "string"
          },
          "zero_charge_reason": {
            "type": "string",
            "description": "零计费行才有；只有「上游输出前拒答」这一种在日志里留了标记。"
          },
          "price_backfilled": {
            "type": "boolean",
            "description": "本行日志未冻结定价快照（任务类退款），单价取自同期同模型，仅供参考。"
          }
        }
      },
      "BillPeriod": {
        "type": "object",
        "properties": {
          "start_time": {
            "type": "integer"
          },
          "end_time": {
            "type": "integer"
          },
          "utc_offset": {
            "type": "string"
          },
          "granularity": {
            "type": "string"
          }
        }
      },
      "BillPreviewPage": {
        "type": "object",
        "required": [
          "period",
          "bill_status",
          "currency",
          "data",
          "next_cursor"
        ],
        "properties": {
          "period": {
            "$ref": "#/components/schemas/BillPeriod"
          },
          "bill_status": {
            "type": "string",
            "enum": [
              "draft"
            ],
            "description": "预览出的一律是草稿账单：平台尚未确认空返与调整，不作结算依据。"
          },
          "currency": {
            "type": "string"
          },
          "data": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/BillLine"
            }
          },
          "next_cursor": {
            "type": [
              "string",
              "null"
            ],
            "description": "为 `null` 即最后一页。"
          }
        }
      },
      "BillTotals": {
        "type": "object",
        "properties": {
          "rows": {
            "type": "integer"
          },
          "requests": {
            "type": "integer"
          },
          "quota": {
            "type": "integer"
          },
          "amount": {
            "type": "string"
          },
          "original_amount": {
            "type": "string"
          },
          "discount_amount": {
            "type": "string",
            "description": "费用 − 原价，**负数**：与费用相加即原价。"
          },
          "effective_ratio": {
            "type": "string",
            "description": "费用 ÷ 原价，四位小数。原价为 0 时不出现。"
          },
          "zero_charged_rows": {
            "type": "integer",
            "description": "有用量、金额为 0 的行数。"
          },
          "zero_charged_requests": {
            "type": "integer"
          },
          "refunded_quota": {
            "type": "integer",
            "description": "退款额度（负数）。"
          },
          "violation_quota": {
            "type": "integer",
            "description": "违规扣费额度。"
          }
        }
      },
      "BillGroupTotals": {
        "type": "object",
        "properties": {
          "key": {
            "type": "string"
          },
          "requests": {
            "type": "integer"
          },
          "quota": {
            "type": "integer"
          },
          "amount": {
            "type": "string"
          },
          "original_amount": {
            "type": "string"
          }
        }
      },
      "BillSummary": {
        "type": "object",
        "required": [
          "period",
          "bill_status",
          "currency",
          "totals"
        ],
        "properties": {
          "period": {
            "$ref": "#/components/schemas/BillPeriod"
          },
          "bill_status": {
            "type": "string",
            "enum": [
              "draft"
            ]
          },
          "currency": {
            "type": "string"
          },
          "totals": {
            "$ref": "#/components/schemas/BillTotals"
          },
          "by_model": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/BillGroupTotals"
            },
            "description": "`group_by` 含 `model` 时出现，按金额降序。"
          },
          "by_group": {
            "type": "array",
            "items": {
              "$ref": "#/components/schemas/BillGroupTotals"
            },
            "description": "`group_by` 含 `group` 时出现。"
          }
        }
      },
      "BillExportRequest": {
        "type": "object",
        "required": [
          "period",
          "start_time",
          "end_time"
        ],
        "properties": {
          "period": {
            "type": "string",
            "enum": [
              "month",
              "day",
              "hour"
            ],
            "description": "账单粒度。跨度上限：`month` ≤ 24 个月，`day` ≤ 366 天，`hour` ≤ 31 天。"
          },
          "start_time": {
            "$ref": "#/components/schemas/TimeInput"
          },
          "end_time": {
            "$ref": "#/components/schemas/TimeInput"
          },
          "utc_offset": {
            "type": "string",
            "default": "+08:00"
          },
          "format": {
            "type": "string",
            "enum": [
              "xlsx",
              "csv",
              "jsonl"
            ],
            "default": "xlsx",
            "description": "`xlsx` 是完整账单工作簿（费用 / 定价 / 说明三页 + Excel 公式）；另两个只出「费用」页那张平表。"
          },
          "model": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "token_name": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "group": {
            "type": "array",
            "items": {
              "type": "string"
            }
          },
          "wait": {
            "type": "boolean",
            "default": false,
            "description": "愿意等结果。服务端按规模决定同步还是异步：同步时直接回文件流，超阈值仍回任务对象。"
          },
          "bill_no": {
            "type": "string",
            "description": "已出账账单的单号，与时间窗口那组互斥。**本期未实现**，给了返回 `INVALID_ARGUMENT`。"
          }
        }
      },
      "BillExportJob": {
        "type": "object",
        "required": [
          "job_id",
          "period",
          "format",
          "bill_status",
          "status",
          "start_time",
          "end_time",
          "utc_offset",
          "submitted_at",
          "started_at",
          "completed_at",
          "row_count",
          "file_name",
          "file_size_bytes",
          "file_sha256",
          "file_url",
          "file_expires_at",
          "error"
        ],
        "properties": {
          "job_id": {
            "type": "string"
          },
          "period": {
            "type": "string",
            "enum": [
              "month",
              "day",
              "hour"
            ]
          },
          "format": {
            "type": "string",
            "enum": [
              "xlsx",
              "csv",
              "jsonl"
            ]
          },
          "bill_status": {
            "type": "string",
            "enum": [
              "draft",
              "issued",
              "confirmed",
              "void"
            ],
            "description": "本期一律 `draft`。"
          },
          "bill_no": {
            "type": "string"
          },
          "status": {
            "type": "string",
            "enum": [
              "pending",
              "processing",
              "completed",
              "failed",
              "canceled",
              "expired"
            ]
          },
          "start_time": {
            "type": "integer"
          },
          "end_time": {
            "type": "integer"
          },
          "utc_offset": {
            "type": "string"
          },
          "submitted_at": {
            "type": "integer"
          },
          "started_at": {
            "type": "integer",
            "description": "未到为 0。"
          },
          "completed_at": {
            "type": "integer",
            "description": "未到为 0。"
          },
          "row_count": {
            "type": "integer"
          },
          "file_name": {
            "type": "string",
            "description": "`{对象}_{ID}_{起}_to_{止}_{粒度}_apipro-bill.xlsx`，下载时同名。"
          },
          "file_size_bytes": {
            "type": "integer"
          },
          "file_sha256": {
            "type": "string"
          },
          "file_url": {
            "type": "string",
            "description": "完成后；自带签名 `token`，24 小时内有效。"
          },
          "file_expires_at": {
            "type": "integer"
          },
          "error": {
            "type": [
              "object",
              "null"
            ],
            "properties": {
              "code": {
                "type": "string"
              },
              "message": {
                "type": "string"
              }
            }
          }
        }
      }
    }
  }
}
