> ## Documentation Index
> Fetch the complete documentation index at: https://apixo.ai/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# 批量生成 API：一次提交多个异步任务

> 一次请求可向同一模型提交 1 至 50 个异步任务。整批请求和单个任务都支持幂等处理。

使用批量生成 API 为同一公共模型提交 1 到 50 个异步任务。每个任务使用模型的正常 `input` 字段。

使用 [批次状态](https://apixo.ai/docs/zh/api-reference/batch-status.md) 检索整个批次，或使用 [批次 Webhooks](https://apixo.ai/docs/zh/api-reference/batch-webhooks.md) 接收每个已接受任务的完成通知。

## 开始之前

* 创建 API 密钥，并验证目标模型是否支持单任务 API。
* 为整批任务对应的业务操作生成一个固定的 `Idempotency-Key`。
* 为每一项分别生成唯一的 `client_item_id` 和 `idempotency_key`。重试同一项时，沿用原值。
* 如果您计划接收回调，请首先设置 [Batch Webhooks](https://apixo.ai/docs/zh/api-reference/batch-webhooks.md)。

## 端点

```http theme={null}
POST https://api.apixo.ai/api/v1/generateTask/{model}/batches
```

将 `{model}` 替换为公共模型 ID，例如 `seedream-4-5`。

## 标头

**Authorization** `string` *required*

API 身份验证。格式：`Bearer YOUR_API_KEY`。

**Idempotency-Key** `string` *required*

标识整批业务操作的唯一幂等键。因超时或网络错误重试时，请使用原值。

**Content-Type** `string` *required*

必须为 `application/json`。

## 请求体

**items** `array` *required*

任务清单。它必须包含 1 到 50 项。

**项目字段**

**items\[].client\_item\_id** `string` *required*

您的物品定位器，例如订单 ID。它在该批次中必须是唯一的，并且由批次状态和 Webhook 负载返回。

**items\[].idempotency\_key** `string` *required*

用于标识单个任务的账户级幂等键。重试同一业务项时，请使用原值。

**items\[].input** `object` *required*

正常模型输入。有关支持的模式、字段和限制，请参阅相关模型页面。

**webhook\_url** `string`

可选的公开 HTTPS 端点，用于接收批次项的最终状态通知。配置后，每个已受理的批次项成功或失败时，都会发送一条 Webhook 通知。

> [!WARNING]
>
> 批量生成不使用`callback_url`。无论它出现在批处理请求中的何处，都将被忽略，包括根正文、项目或嵌套 `input`。仅使用根级别 `webhook_url` 进行批量通知。

## 提交成功

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/seedream-4-5/batches" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Idempotency-Key: batch-20260915-001" \
  -H "Content-Type: application/json" \
  -d '{
    "items": [
      {
        "client_item_id": "order-1001",
        "idempotency_key": "order-1001-v1",
        "input": {
          "mode": "text-to-image",
          "prompt": "An orange cat wearing an astronaut helmet, cinematic lighting",
          "aspect_ratio": "1:1"
        }
      },
      {
        "client_item_id": "order-1002",
        "idempotency_key": "order-1002-v1",
        "input": {
          "mode": "text-to-image",
          "prompt": "A futuristic city floating above a sea of clouds at sunrise",
          "aspect_ratio": "16:9"
        }
      }
    ]
  }'
```

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "batchId": "batch_123",
    "model": "seedream-4-5",
    "status": "queued",
    "replayed": false,
    "newlyAcceptedCount": 2,
    "reusedCount": 0,
    "rejectedCount": 0,
    "items": [
      {
        "ordinal": 0,
        "clientItemId": "order-1001",
        "taskId": "task_123",
        "accepted": true,
        "status": "queued"
      },
      {
        "ordinal": 1,
        "clientItemId": "order-1002",
        "taskId": "task_456",
        "accepted": true,
        "status": "queued"
      }
    ],
    "batchExpiresAt": "2026-10-15T12:00:00"
  }
}
```

### 成功提交响应字段

| 字段 | 类型 | 说明 |
| - | - | - |
| `code` | integer | API结果代码。`200`表示批量端点处理了请求；检查 `items[].accepted` 以确定每个项目是否被接受。 |
| `message` | string | API结果消息。成功提交请求将返回 `success`。 |
| `data.batchId` | string | 唯一批次 ID。将其与批次状态 API 结合使用。 |
| `data.model` | string | 本批次使用的公共模型ID。 |
| `data.status` | string | 当前批次状态，例如 `queued`、`running` 或 `completed`。 |
| `data.replayed` | boolean | 表示这次响应是否返回了先前使用同一个 `Idempotency-Key` 创建的批次。 |
| `data.newlyAcceptedCount` | integer | 新接受的项目数量。重用的现有任务被排除在外。 |
| `data.reusedCount` | integer | 通过 `idempotency_key` 重用现有任务的项目数量。 |
| `data.rejectedCount` | integer | 未被接受且因此没有可执行 `taskId` 的项目数量。 |
| `data.items` | array | 批次中的每个项目均按照请求中的相同顺序排列。 |
| `data.items[].ordinal` | integer | 从零开始的项目位置。仅用于显示或诊断，不用于业务关联。 |
| `data.items[].clientItemId` | string | 您的项目定位器。用于将 API 任务与您的订单或作业关联。 |
| `data.items[].taskId` | string \| null | 已接受项目的平台任务 ID；若项目未被接受，则为 `null`。 |
| `data.items[].accepted` | boolean | 该项目是否进入执行流程。 |
| `data.items[].status` | string | 当前公共项目状态：`queued`、`running`、`succeeded`、`failed` 或 `rejected`。 |
| `data.batchExpiresAt` | string | 批次记录的过期时间。请在过期前保存所需的任何业务关联和结果。 |

保存 `batchId` 用于批量级查找，`taskId` 用于单个任务查找，`clientItemId` 用于业务关联。

## 有效批次中的物品被拒绝

当单个项目不被接受时，请求本身可以成功返回。例如，模型输入无效的项目被标记为`rejected`：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "batchId": "batch_124",
    "status": "queued",
    "newlyAcceptedCount": 1,
    "reusedCount": 0,
    "rejectedCount": 1,
    "items": [
      {
        "ordinal": 0,
        "clientItemId": "order-1003",
        "taskId": "task_789",
        "accepted": true,
        "status": "queued"
      },
      {
        "ordinal": 1,
        "clientItemId": "order-1004",
        "taskId": null,
        "accepted": false,
        "status": "rejected",
        "error": {
          "code": "INVALID_PARAM_VALUE",
          "message": "The model input is invalid"
        }
      }
    ]
  }
}
```

| 字段 | 类型 | 说明 |
| - | - | - |
| `data.items[].accepted` | boolean | `false`表示该项没有进入执行流程。 |
| `data.items[].status` | string | `rejected` 表示该项目在提交过程中被拒绝。 |
| `data.items[].taskId` | null | 被拒绝的项目没有任务 ID，无法查询或接收 Webhook。 |
| `data.items[].error.code` | string | 程序性拒绝原因。 |
| `data.items[].error.message` | string | 面向开发人员的拒绝解释。 |

只继续处理标记为 `accepted: true` 的条目。对遭拒的条目，先修正输入，再作为新的业务操作提交，并为批次和条目分别生成新的幂等键。

## 整个请求失败

整个请求失败且不会创建可执行批处理，例如：

* `Idempotency-Key` 缺失
* `items` 为空或包含超过 50 个项目
* `client_item_id` 或 `idempotency_key` 在一批内重复
* JSON 主体超过 5 MiB
* `webhook_url` 不是公开的 HTTPS 地址

```json theme={null}
{
  "code": 400,
  "message": "client_item_id and idempotency_key must be present and unique in one batch",
  "data": null
}
```

| 字段 | 类型 | 说明 |
| - | - | - |
| `code` | integer | HTTP 或业务错误代码。这不是可接受的批处理结果。 |
| `message` | string | 整个批量请求失败的原因 |
| `data` | null | 信封验证失败后不会创建批次或项目列表。 |

## 幂等重试

如果超时或网络中断使您无法知道响应是否到达：

1. 使用 **相同的** `Idempotency-Key` 重试整个请求。
2. 对于每个原始项目，保持 **相同的** `idempotency_key`。
3. APIXO 重播现有批次。 `replayed: true` 表示安全批量重放。

> [!TIP]
>
> 一个幂等键对应一次业务操作。HTTP 请求超时后，请用原键重试。改用新键可能会重复创建批次或任务。

## 后续步骤

* 读取[批次状态](https://apixo.ai/docs/zh/api-reference/batch-status.md)查询整个批次。使用单个任务 > 状态任务页面查询一个 `taskId`。
* Read [批量 Webhooks](https://apixo.ai/docs/zh/api-reference/batch-webhooks.md) 以接收每个项目的任务完成通知。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.