# 导出当前 Key 用量 CSV

导出当前 Key 用量 CSV，结果限于当前凭证的可访问范围。

## 接口与鉴权

`GET https://api.tu-zi.com/api/log/token/export`

传输方式: json

使用模型调用 API Key。示例凭证为占位符，请从环境变量读取真实密钥。

```http
Authorization: Bearer YOUR_API_KEY
```

## 查询参数

| 字段 | 类型 | 必填 | 说明与约束 |
| --- | --- | --- | --- |
| start_timestamp | integer | 是 | 开始 Unix 秒，必填。 |
| end_timestamp | integer | 是 | 结束 Unix 秒，必填；范围不能超过 90 天。 |
| model_name | string | 否 | 模型筛选。 |
| type | integer | 否 | 日志类型筛选。 |

## 运行前准备

将 https://api.tu-zi.com 替换为本站地址（不要重复追加 /v1），YOUR_MODEL 替换为模型广场中的完整模型名。Python 示例需要先安装 requests；JavaScript 示例使用 Node.js 20+ 并保存为 .mjs 文件。WebSocket 示例按代码注释安装专用依赖。

先在本站令牌页面创建模型调用密钥，并设置 API_KEY 环境变量；cURL 中的 YOUR_API_KEY 需要替换。上传示例中的 input.png/input.wav 是本地文件，必须提前准备。

首次运行使用最小请求；成功后对照下方响应字段读取结果。异步接口返回任务 ID 后仍需查询，任务提交成功不代表生成已完成。

## 代码示例

### curl

```bash
curl --request GET 'https://api.tu-zi.com/api/log/token/export?start_timestamp=1700000000&end_timestamp=1700086400' \
  --header 'Authorization: Bearer YOUR_API_KEY' \
  --output 'usage.csv'
```

### python

```python
import json
import os
import requests

url = "https://api.tu-zi.com/api/log/token/export?start_timestamp=1700000000&end_timestamp=1700086400"
headers = json.loads("{\"Authorization\":\"Bearer YOUR_API_KEY\"}")
headers["Authorization"] = "Bearer " + os.environ["API_KEY"]
response = requests.request("GET", url, headers=headers, timeout=120)
response.raise_for_status()
if "json" in response.headers.get("Content-Type", "").lower():
    raise RuntimeError("Expected binary content, received JSON: " + response.text)
with open("usage.csv", "wb") as output:
    output.write(response.content)
```

### javascript

```javascript
import { writeFile } from 'node:fs/promises';
// Node.js 20+
const url = "https://api.tu-zi.com/api/log/token/export?start_timestamp=1700000000&end_timestamp=1700086400";
const headers = {
  "Authorization": "Bearer YOUR_API_KEY"
};
headers.Authorization = 'Bearer ' + process.env.API_KEY;
const response = await fetch(url, { method: "GET", headers });
if (!response.ok) throw new Error(await response.text());
if ((response.headers.get("content-type") || "").toLowerCase().includes("json")) throw new Error("Expected binary content, received JSON: " + await response.text());
await writeFile("usage.csv", Buffer.from(await response.arrayBuffer()));
```

## 响应字段

| 字段 | 类型 | 说明与约束 |
| --- | --- | --- |
| CSV | binary | 成功返回 UTF-8 CSV；错误时可能为 JSON success:false。 |

以下为示例响应，部分接口仅展示主要字段；可选字段不保证出现。

```json
{
  "success": true,
  "data": {}
}
```

## 错误处理与重试

检查 HTTP 状态与该协议的错误对象；不要将不同协议的错误字段混用。401/403 检查凭证与权限，429 退避等待，5xx 检查网关或上游状态。错误正文可能含 error/message/code 等字段，以实际协议为准。

查询可采用有上限的指数退避；创建任务、生成内容、创建或删除密钥等操作不要盲目重试，以免重复计费或重复执行。

## 适用限制

将示例时间替换为需要查询的 Unix 秒范围；账号导出使用系统令牌，Key 导出使用该 Key。先检查状态与 Content-Type，再保存 CSV。

## 文档依据

定义版本: 1

审核日期: 2026-11-02

来源: https://api.tu-zi.com/docs/api/reference-usage-token-export

本文只包含接口文档和示例。真实密钥、账号数据及用户请求内容不包含在复制内容中。
