# 快速入门

几分钟内运行您的第一次生成。所有 1,496 个模型都接受相同类型的请求，并为图像、视频和音频返回
相同的请求对象：在这里学会一次即可。

> [!NOTE]
> 基础 URL 为 `https://api.doitong.com/v1`。每次调用都发送 `Authorization: Bearer <your key>`，同一个密钥可用于 HTTP
> API 和 MCP 服务器。

## 1. 获取 API 密钥

在 [https://doitong.com/developers/dashboard#keys](https://doitong.com/developers/dashboard#keys) 创建密钥，并将其保存到环境
变量中：它只会显示一次。每个密钥都可以拥有自己的预算，因此您可以将一个密钥交给智能体，而不必担心整个账户的风险。`GET /account` 会返回您的 API 余额。

```bash
export DOITONG_API_KEY="doitong_..."

curl "https://api.doitong.com/v1/account" \
  -H "Authorization: Bearer $DOITONG_API_KEY"
```
```javascript
const res = await fetch("https://api.doitong.com/v1/account", {
  headers: { Authorization: `Bearer ${process.env.DOITONG_API_KEY}` },
});

console.log(await res.json());
```
```python
import os
import requests

r = requests.get(
    "https://api.doitong.com/v1/account",
    headers={"Authorization": f"Bearer {os.environ['DOITONG_API_KEY']}"},
)
print(r.json())
```

运行费用从您账户的 **API 余额** 中以 API 积分支付：1,000 API 积分 = $1，每个模型的费用完全按照其提供方的价格计算。在 [https://doitong.com/developers/dashboard#balance](https://doitong.com/developers/dashboard#balance) 充值。

## 2. 先获取报价，再运行

`POST /quote/{model_id}` 接收输入 JSON 并返回确切价格。它不需要密钥，也不产生费用。使用相同输入运行时，费用正好就是该报价：报价即为扣费金额。（按用量计费的模型例外：其报价是本次运行可能产生的最高费用，未使用部分会在运行结束后退款。）

```bash
curl -X POST "https://api.doitong.com/v1/quote/kling-video/v3/pro/image-to-video" \
  -H "Content-Type: application/json" \
  -d '{"image": "https://example.com/fox.jpg", "duration": 5, "generate_audio": true}'
```
```javascript
const res = await fetch("https://api.doitong.com/v1/quote/kling-video/v3/pro/image-to-video", {
  method: "POST",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ image: "https://example.com/fox.jpg", duration: 5, generate_audio: true }),
});

const quote = await res.json();
console.log(quote.credits, quote.usd); // 840 "0.84"
```
```python
import requests

r = requests.post(
    "https://api.doitong.com/v1/quote/kling-video/v3/pro/image-to-video",
    json={"image": "https://example.com/fox.jpg", "duration": 5, "generate_audio": True},
)
print(r.json()["credits"], r.json()["usd"])  # 840 0.84
```

```json 200 OK · 报价
{
  "object": "quote",
  "model": "kling-video/v3/pro/image-to-video",
  "credits": 840,
  "usd": "0.84",
  "unit": "second",
  "quantity": 5,
  "unit_credits": 168,
  "mode": "exact",
  "breakdown": "5 s × 168 API credits"
}
```

然后将相同的请求体发送到 `POST /run/{model_id}`。添加 `?wait=20` 可将连接保持最多 20 秒：快速模型会在一次往返中返回完成的结果。发送 `Idempotency-Key` 标头，以确保重试不会运行两次（或扣费两次）。

```bash
curl -X POST "https://api.doitong.com/v1/run/flux/schnell?wait=20" \
  -H "Authorization: Bearer $DOITONG_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: lighthouse-001" \
  -d '{"prompt": "A lighthouse on a rocky coast at dusk, warm light in the window"}'
```
```javascript
const res = await fetch("https://api.doitong.com/v1/run/flux/schnell?wait=20", {
  method: "POST",
  headers: {
    Authorization: `Bearer ${process.env.DOITONG_API_KEY}`,
    "Content-Type": "application/json",
    "Idempotency-Key": "lighthouse-001",
  },
  body: JSON.stringify({ prompt: "A lighthouse on a rocky coast at dusk, warm light in the window" }),
});

const request = await res.json();
console.log(request.status, request.outputs[0]?.url);
```
```python
import os
import requests

r = requests.post(
    "https://api.doitong.com/v1/run/flux/schnell",
    params={"wait": 20},
    headers={
        "Authorization": f"Bearer {os.environ['DOITONG_API_KEY']}",
        "Idempotency-Key": "lighthouse-001",
    },
    json={"prompt": "A lighthouse on a rocky coast at dusk, warm light in the window"},
)
request = r.json()
print(request["status"], request["outputs"][0]["url"] if request["outputs"] else None)
```

响应是一个请求对象：

```json 200 OK · 请求
{
  "id": "req_8k2m4q7x1z9c3v5b6n0p2r4t",
  "object": "request",
  "model": "flux/schnell",
  "status": "succeeded",
  "cost": { "credits": 3, "usd": "0.003", "charged": 3, "refunded": 0 },
  "outputs": [
    { "type": "image", "url": "https://content.doitong.com/dev-api/2026/10/20/req_8k2m4q7x1z9c3v5b6n0p2r4t/0-output.jpeg",
      "content_type": "image/jpeg", "width": 1024, "height": 768, "stored": true }
  ],
  "error": null
}
```

## 3. 跟踪状态

如果等待结束时模型仍在处理，您会收到带有请求 `id` 和当前 `status` 的 `202 Accepted`。每个请求都会经过相同的生命周期：

请求生命周期: `queued` → `running` → `succeeded` | `failed` | `canceled` (最终状态；失败和已取消的请求会退款).

使用请求的 id 再次读取请求。这里同样支持 `?wait=20`，因此简单的循环就足够了：

```bash
curl "https://api.doitong.com/v1/requests/req_8k2m4q7x1z9c3v5b6n0p2r4t?wait=20" \
  -H "Authorization: Bearer $DOITONG_API_KEY"
```
```javascript
const headers = { Authorization: `Bearer ${process.env.DOITONG_API_KEY}` };
let request = await (await fetch("https://api.doitong.com/v1/requests/req_8k2m4q7x1z9c3v5b6n0p2r4t?wait=20", { headers })).json();

while (request.status === "queued" || request.status === "running") {
  const res = await fetch(`https://api.doitong.com/v1/requests/${request.id}?wait=20`, { headers });
  request = await res.json();
}

console.log(request.status, request.outputs[0]?.url);
```
```python
import os
import requests

headers = {"Authorization": f"Bearer {os.environ['DOITONG_API_KEY']}"}
request = {"id": "req_8k2m4q7x1z9c3v5b6n0p2r4t", "status": "queued"}

while request["status"] in ("queued", "running"):
    request = requests.get(
        f"https://api.doitong.com/v1/requests/{request['id']}",
        params={"wait": 20},
        headers=headers,
    ).json()

print(request["status"], request["outputs"][0]["url"] if request["outputs"] else None)
```

当 `status` 为 `succeeded` 时，`outputs[].url` 包含指向我们存储中文件的链接。如果请求 `failed` 或 `canceled`，其 API 积分会自动退款，`error` 会说明原因。取消仍处于 `queued` 或 `running` 状态的任何请求：

```bash
curl -X POST "https://api.doitong.com/v1/requests/req_8k2m4q7x1z9c3v5b6n0p2r4t/cancel" \
  -H "Authorization: Bearer $DOITONG_API_KEY"
```

对于长时间的视频任务，您可以跳过轮询，改为传入 `webhook_url`（参见 [Webhooks](https://doitong.com/developers/docs/webhooks)）。

## 4. 文件

将任何公共 URL 作为输入，或通过 `POST /files` 获取上传 URL，将文件 `PUT` 到该 URL，然后使用返回的 `file_url`（参见 [文件](https://doitong.com/developers/docs/files)）。成功的请求会在 `outputs[]` 中列出结果，包括类型、内容类型，以及在已知时提供的尺寸和时长。

```bash
curl -X POST "https://api.doitong.com/v1/files" \
  -H "Authorization: Bearer $DOITONG_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"filename": "fox.jpg", "content_type": "image/jpeg", "size": 482113}'
```

> [!TIP]
> **文件永久保留，或免费保留 7 天：由您选择。** 请求成功前，输出文件会被复制到我们的
> 存储中。默认情况下，文件会一直保留到您删除它们（每 GB 每月 100 API 积分）；在控制台中关闭
> “永久保存文件”，或传入 `?storage=temporary`，即可免费保留 7 天（参见 [文件](https://doitong.com/developers/docs/files#storage)）。

## 错误

每个模型都以相同方式失败：一个包含 `type`、准确 `code`、出错的 `param` 以及 `details` 中所有问题的信封对象，使智能体无需阅读说明文字即可修复请求。无效请求不会产生任何费用。

```json 422 Unprocessable Entity
{
  "error": {
    "type": "invalid_request",
    "code": "invalid_input",
    "message": "duration must be one of 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15.",
    "param": "duration",
    "details": [ { "param": "duration", "message": "duration must be one of 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15." } ],
    "request_id": null,
    "doc_url": "https://doitong.com/developers/docs/errors#invalid_input"
  }
}
```

所有类型和代码都列在 [错误](https://doitong.com/developers/docs/errors) 中。

## 定价

价格以 USD 和 API 积分表示，并在运行前显示在每个模型页面上。每个请求都包含带有 `credits`、`usd`、`charged` 和 `refunded` 的 `cost`。请求被接受时即会扣费；`failed` 和 `canceled` 请求会自动退款。有些模型完全根据输入定价，有些根据您发送的文件长度或大小定价，另一些则按*每次运行的预估价格*收费：无论哪种情况，报价就是您支付的金额。少数模型（语言和视觉模型、语音、处理时间工具）按*用量计费*：报价是本次运行可能产生的最高费用，运行未使用的部分会自动退款。参见 [定价](https://doitong.com/developers/docs/pricing)。

## MCP 和 AI 智能体

使用同一个 Bearer 密钥将任何 MCP 客户端连接到 `https://api.doitong.com/v1/mcp`。您的智能体可以搜索目录、获取报价并运行模型，还可以以 Markdown 格式读取所有文档页面。

```bash Claude Code
claude mcp add --transport http doitong-models https://api.doitong.com/v1/mcp \
  --header "Authorization: Bearer YOUR_API_KEY"
```

## 后续步骤

- 浏览[所有模型](https://doitong.com/developers/models)。每个页面都有一个请求构建器，可为您生成代码。
- 了解请求生命周期：[请求](https://doitong.com/developers/docs/requests)。
- 在每个模型中使用相同的参数名称：[参数](https://doitong.com/developers/docs/parameters)。
- 正在使用 AI 编程智能体？将 [https://doitong.com/developers/llms.txt](https://doitong.com/developers/llms.txt) 提供给它，或连接 [MCP 服务器](https://doitong.com/developers/docs/mcp)。
