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

# Kling Tasks API 的對接和使用

> Kling video generation 集成指南 - XHuoAPI

Kling Tasks API 的主要功能是通過輸入 Kling Videos Generation API 生成的任務ID來查詢該任務的執行情況。

本文檔將詳細介紹 Kling Tasks API 的對接說明，幫助您輕鬆集成並充分利用該 API 的強大功能。通過 Kling Tasks API，您可以輕鬆實現查詢 Kling Videos Generation API 的任務執行情況。

## 申請流程

要使用 Kling Tasks API，需要先到 申請頁面 [Kling Videos Generation API](https://api.xhuoapi.ai/documents/3b921a16-a411-4557-8335-53f21d3f9e46) 申請相應的服務，然後複製 Kling Videos Generation API 的任務ID，如圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/om6p6g.png" width="500" className="m-auto" />
</p>

最後進入 Tasks API 頁面 [Kling Tasks API](https://api.xhuoapi.ai/documents/6b43ff7b-8298-49c8-a390-83df85c2c5e5) 申請相應的服務，進入頁面之後，點擊「Acquire」按鈕，如圖所示：

![申請頁面](https://cdn.xhuoapi.ai/rci31i.png)

如果您尚未登錄或註冊，會自動跳轉到 [登錄頁面](https://api.xhuoapi.ai) 邀請您來註冊和登錄，登錄註冊之後會自動返回當前頁面。

首次申請時會有免費額度贈送，可以免費使用該 API。

## 請求示例

Kling Tasks API 可以用於查詢 Kling Videos Generation API 的結果。關於怎樣使用 Kling Videos Generation API，請參考文檔 [Kling Videos Generation API](https://api.xhuoapi.ai/documents/3b921a16-a411-4557-8335-53f21d3f9e46)。

我們以 Kling Videos Generation API 服務返回的任務ID 一個為例，演示如何使用該 API。假設我們有一個任務ID：20068983-0cc9-4c6a-aeb6-9c6a3c668be0，接下來演示如何通過傳入一個任務ID 來。

### 任務示例圖

<p>
  <img src="https://cdn.xhuoapi.ai/om6p6g.png" width="500" className="m-auto" />
</p>

### 設置請求頭和請求體

**Request Headers** 包括：

* `accept`：指定接收 JSON 格式的響應結果，這裡填寫為 `application/json`。
* `authorization`：調用 API 的密鑰，申請之後可以直接下拉選擇。

**Request Body** 包括：

* `id`：上傳的任務ID。
* `action`：對任務的操作方式。

設置如下圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/xztiqi.png" width="500" className="m-auto" />
</p>

### 代碼示例

可以發現，在頁面右側已經自動生成了各種語言的代碼，如圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/bsqh3i.png" width="500" className="m-auto" />
</p>

部分代碼示例如下：

#### CURL

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/kling/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0",
  "action": "retrieve"
}'
```

#### Python

```python theme={null}
import requests

url = "https://api.xhuoapi.ai/v1/kling/tasks"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0",
    "action": "retrieve"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
```

### 響應示例

請求成功後，API 將返回此處視頻任務的詳細信息。例如：

```json theme={null}
{
  "_id": "67c5163f550a4144a5b68698",
  "id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0",
  "api_id": "29187cb2-1acb-43b8-baf5-3f3f709292eb",
  "application_id": "f35762fe-e8a4-4613-bb70-e5c1be4f9fc2",
  "created_at": 1740969535.333,
  "credential_id": "ce81345f-7e2a-4871-b539-aefb5f725220",
  "request": {
    "action": "text2video",
    "model": "kling-v1",
    "prompt": "White ceramic coffee mug on glossy marble countertop with morning window light. Camera slowly rotates 360 degrees around the mug, pausing briefly at the handle.",
    "callback_url": "https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3"
  },
  "trace_id": "0a907f69-4ae2-4a08-b34c-ee15c1c47077",
  "type": "videos",
  "user_id": "ad7afe47-cea9-4cda-980f-2ad8810e51cf",
  "job_id": "CjJzzGfBfqcAAAAAAKdVMQ",
  "response": {
    "success": true,
    "video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c",
    "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7822108635/text2video/CjJzzGfBfqcAAAAAAKdVMQ-0_raw_video_1.mp4",
    "duration": "5.1",
    "state": "succeed",
    "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
  },
  "duration": 317.1300001144409,
  "finished_at": 1740969852.463
}
```

返回結果一共有多個字段，request 字段就是發起任務時的 request body，同時 response 字段是任務完成後返回的 response body。字段介紹如下。

* `id`，生成此視頻任務的 ID，用於唯一標識此次視頻生成任務。
* `request`，查詢視頻任務中的請求信息。
* `response`，查詢視頻任務中的返回信息。

## 批量查詢操作

這是針對多個任務ID來進行查詢視頻任務詳細信息，與上面不同的是需要將 action 選中為 retrieve\_batch。

**Request Body** 包括：

* `ids`：上傳的任務ID數組。
* `action`：對任務的操作方式。

設置如下圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/4p5uvq.png" width="500" className="m-auto" />
</p>

### 代碼示例

可以發現，在頁面右側已經自動生成了各種語言的代碼，如圖所示：

<p>
  <img src="https://cdn.xhuoapi.ai/g3b7g6.png" width="500" className="m-auto" />
</p>

部分代碼示例如下：

### 響應示例

請求成功後，API 將返回此次所有批量視頻任務的具體詳細信息。例如：

```json theme={null}
{
  "items": [
    {
      "_id": "67c5163f550a4144a5b68698",
      "id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0",
      "api_id": "29187cb2-1acb-43b8-baf5-3f3f709292eb",
      "application_id": "f35762fe-e8a4-4613-bb70-e5c1be4f9fc2",
      "created_at": 1740969535.333,
      "credential_id": "ce81345f-7e2a-4871-b539-aefb5f725220",
      "request": {
        "action": "text2video",
        "model": "kling-v1",
        "prompt": "白色陶瓷咖啡杯放在光滑的大理石台面上，晨光透过窗户照射。相机缓慢地围绕咖啡杯旋转360度，短暂停留在把手处。",
        "callback_url": "https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3"
      },
      "trace_id": "0a907f69-4ae2-4a08-b34c-ee15c1c47077",
      "type": "videos",
      "user_id": "ad7afe47-cea9-4cda-980f-2ad8810e51cf",
      "job_id": "CjJzzGfBfqcAAAAAAKdVMQ",
      "response": {
        "success": true,
        "video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c",
        "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7822108635/text2video/CjJzzGfBfqcAAAAAAKdVMQ-0_raw_video_1.mp4",
        "duration": "5.1",
        "state": "succeed",
        "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
      },
      "duration": 317.1300001144409,
      "finished_at": 1740969852.463
    },
    {
      "_id": "67c51415550a4144a5b442a5",
      "id": "e3a575aa-a4bd-49c8-9b12-cde38d5462e0",
      "api_id": "29187cb2-1acb-43b8-baf5-3f3f709292eb",
      "application_id": "f35762fe-e8a4-4613-bb70-e5c1be4f9fc2",
      "created_at": 1740968981.619,
      "credential_id": "ce81345f-7e2a-4871-b539-aefb5f725220",
      "request": {
        "action": "text2video",
        "model": "kling-v1",
        "prompt": "白色陶瓷咖啡杯放在光滑的大理石台面上，晨光透过窗户照射。相机缓慢地围绕咖啡杯旋转360度，短暂停留在把手处。"
      },
      "trace_id": "4f32ba2d-8846-4ea9-9253-997ec0b2e052",
      "type": "videos",
      "user_id": "ad7afe47-cea9-4cda-980f-2ad8810e51cf",
      "job_id": "Cjil4mfBfs0AAAAAAKbMQQ",
      "response": {
        "success": true,
        "video_id": "af9a1af0-9aa0-4638-81c1-d41d6143c508",
        "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7485378259/text2video/Cjil4mfBfs0AAAAAAKbMQQ-0_raw_video_1.mp4",
        "duration": "5.1",
        "state": "succeed",
        "task_id": "e3a575aa-a4bd-49c8-9b12-cde38d5462e0"
      },
      "duration": 316.3180000782013,
      "finished_at": 1740969297.937
    }
  ],
  "count": 2
}
```

返回結果一共有多個字段，其中items是包含了批量視頻任務的具體詳情信息，每個視頻任務的具體信息與上文的字段一樣，字段信息如下。

* `items`，批量視頻任務的所有具體詳情信息。它是一個數組，每個數組的元素和上文查詢單個任務的返回結果格式是一樣的。
* `count`，此處批量查詢視頻任務的個數。

#### CURL

```bash theme={null}
curl -X POST 'https://api.xhuoapi.ai/v1/kling/tasks' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "ids": ["e3a575aa-a4bd-49c8-9b12-cde38d5462e0","20068983-0cc9-4c6a-aeb6-9c6a3c668be0"],
  "action": "retrieve_batch"
}'
```

## 錯誤處理

在調用 API 時，如果遇到錯誤，API 會返回相應的錯誤代碼和信息。例如：

* `400 token_mismatched`：錯誤請求，可能是由於缺少或無效的參數。
* `400 api_not_implemented`：錯誤請求，可能是由於缺少或無效的參數。
* `401 invalid_token`：未授權，無效或缺少授權令牌。
* `429 too_many_requests`：請求過多，您已超過速率限制。
* `500 api_error`：內部伺服器錯誤，伺服器出現問題。

### 錯誤響應示例

```json theme={null}
{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}
```

## 結論

通過本文檔，您已經了解了如何使用 Kling Tasks API 進行查詢單個或批量視頻任務的所有具體詳情信息。希望本文檔能幫助您更好地對接和使用該 API。如有任何問題，請隨時聯繫我們的技術支持團隊。
