> ## 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-hant/api-reference/batch-status.md) 擷取整批，或使用 [批次 Webhooks](https://apixo.ai/docs/zh-hant/api-reference/batch-webhooks.md) 接收每個已接受項目的任務結束通知。

## 開始之前

* 建立 API 金鑰並驗證目標模型是否適用於單一任務 API。
* 為整批任務對應的業務操作生成一個固定的 `Idempotency-Key`。
* 為每一項分別生成唯一的 `client_item_id` 和 `idempotency_key`。重試同一項時，沿用原值。
* 如果您打算接收回呼，請先設定 [Batch Webhooks](https://apixo.ai/docs/zh-hant/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-hant/api-reference/batch-status.md)查詢整批。使用單一任務 > 狀態任務頁面查詢一個 `taskId`。
* 讀取 [批次 Webhooks](https://apixo.ai/docs/zh-hant/api-reference/batch-webhooks.md) 以接收每個項目的終端通知。


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