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

# 可靈圖片生成

> 使用原生 contents、settings 和 options 欄位送出可靈 Image 4.0 生成任務。

使用 MixRoute API Key 進行 Bearer 驗證。模型目錄列的是 `kling-image-4.0`，但必須使用下方的請求路徑；`/kling/image-generation/kling-image-4.0` 無法使用。不需要 `/v1` 前綴、請求內文的 `model` 欄位或 `metadata` 包裝。

`POST https://api.mixroute.ai/kling/image-generation/kling-4.0`

## 請求內文

| 欄位 | 型別 | 必填 | 說明 |
| - | - | - | - |
| `contents` | array | 是 | 輸入項目陣列。必須包含一個非空的 `prompt` 項目。 |
| `settings` | object | 否 | 設定圖片數量、解析度和長寬比。 |
| `options` | object | 否 | 選填的回呼、外部任務 ID 和浮水印設定。 |

### 輸入項目

| `type` | 其他欄位 |
| - | - |
| `prompt` | 必填。提示詞放在 `text`，不要使用最外層的 `prompt` 欄位。 |
| `refer_image` | `url` 填入可存取的圖片網址或 Base64 圖片資料，`id` 為任務內唯一的引用名稱。 |
| `element` | `element_id` 填入路由對應的上游帳戶可存取的多圖主體 ID，`id` 為唯一的引用名稱。 |

參考圖與主體合計最多 10 個。可在提示詞內用 `@id` 指定要參考的項目。圖片須為 JPG、JPEG 或 PNG，每張不超過 30 MB，寬高均至少 300 px，長寬比介於 2:5 和 5:2。

純英文提示詞最多 50,000 個字元，純非英文最多 21,000 個。混合輸入時，英文字元數除以 50,000，加上非英文字元數除以 21,000，合計不得超過 1。

### 輸出設定

| 欄位 | 型別 | 預設值 | 說明 |
| - | - | - | - |
| `result_type` | string | `single` | `single` 生成獨立圖片，`series` 生成相互關聯的圖片。 |
| `n` | integer | `1` | 1–9 張，僅在 `result_type` 為 `single` 時生效。 |
| `series_amount` | integer / string | `4` | 2–10 張或 `auto`，僅在 `series` 模式生效。`auto` 由服務決定張數。 |
| `resolution` | string | `2k` | 可選 `2k`、`4k` 或 `8k`。 |
| `aspect_ratio` | string | `auto` | 可選 `auto`、`16:9`、`9:16`、`1:1`、`4:3`、`3:4`、`3:2`、`2:3` 或 `21:9`。 |

### 其他選項

| 欄位 | 型別 | 必填 | 說明 |
| - | - | - | - |
| `callback_url` | string | 否 | 回呼網址。定期查詢時使用提交回應 `data.id` 內的閘道任務 ID。 |
| `external_task_id` | string | 否 | 選填的自訂任務 ID，不能取代查詢所需的閘道 ID。 |
| `watermark_info.enabled` | boolean | 否 | 設為 `true` 可要求另一份帶浮水印的圖片，預設為 `false`。 |

## 生成一張圖片

```bash theme={null}
curl --request POST 'https://api.mixroute.ai/kling/image-generation/kling-4.0' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "contents": [
      {
        "type": "prompt",
        "text": "A single blue ceramic mug centered on a white table, studio product photograph."
      }
    ],
    "settings": {
      "result_type": "single",
      "n": 1,
      "resolution": "2k",
      "aspect_ratio": "1:1"
    }
  }'
```

送出成功後，回應會在 `data.id` 提供閘道任務 ID，並傳回 `code: 0`，此時尚未產生圖片結果。

## 查詢並取得圖片

把 `data.id` 設為 `KLING_TASK_ID`，每次只查一個 ID。任務狀態和錯誤處理請見[查詢 Kling 任務](https://mixroute.ai/docs/zh-Hant/api-reference/endpoint/kling-tasks.md)。

```bash theme={null}
curl --get 'https://api.mixroute.ai/kling/tasks' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --data-urlencode "task_ids=$KLING_TASK_ID"
```

```json theme={null}
{
  "code": 0,
  "message": "SUCCEED",
  "data": [
    {
      "id": "task_example",
      "status": "succeeded",
      "outputs": [
        {
          "type": "image",
          "url": "https://example.com/generated.png"
        }
      ]
    }
  ]
}
```

對應任務的狀態變為 `succeeded` 後，讀取 `outputs` 中 `type: "image"` 項目的 `url`。存取圖片網址時不要附上 MixRoute API Key。


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