> ## 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.

# Kling 2.6 API：生成帶聲音的影片

> 透過文字或圖片生成含音訊的影片

## 概覽

Kling 2.6 是快手的音影片生成模型，可生成短影片，並按需加入對白、音效、環境聲或背景音訊。在 APIXO Playground 試用後，可依本頁範例呼叫 API。

| 功能 | 值 |
| - | - |
| 模型 ID | `kling-2-6` |
| 模式 | `text-to-video`, `image-to-video` |
| 提示詞長度 | 1 至 1000 個字元 |
| 參考圖片 | 1-2 個 `image-to-video` 的 URL |
| 縱橫比 | `1:1`、`9:16`、`16:9` 對應 `text-to-video` |
| 持續時間 | `5`、`10` 秒 |
| 音訊 | 所需的 `sound` 切換：`true` 或 `false` |

## 端點與身分驗證

API 基底網址：

```text theme={null}
https://api.apixo.ai/api/v1
```

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/kling-2-6` | 送出生成任務 |
| `GET` | `/statusTask/kling-2-6?taskId={taskId}` | 查詢任務狀態並取得結果 |

每個請求都必須附上 APIXO API 金鑰：

```http theme={null}
Authorization: Bearer YOUR_API_KEY
```

送出任務時，還需設定以下請求標頭：

```http theme={null}
Content-Type: application/json
```

## 非同步請求範例

以下範例提交一個最簡單的文字生成影片任務，回應中會傳回 `taskId`。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/kling-2-6" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "text-to-video",
      "prompt": "a rainy neon street at night with reflections on the pavement and soft traffic ambience",
      "duration": 5,
      "sound": true,
      "aspect_ratio": "16:9"
    }
  }'
```

成功回應：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678"
  }
}
```

請保留 `taskId`，後續輪詢結果時會用到。

## 輪詢結果

```bash theme={null}
curl -X GET "https://api.apixo.ai/api/v1/statusTask/kling-2-6?taskId=task_12345678" \
  -H "Authorization: Bearer YOUR_API_KEY"
```

任務處理中的回應範例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "processing",
    "createTime": 1767965610929
  }
}
```

成功回應：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "success",
    "resultJson": "{\"resultUrls\":[\"https://file.apixo.ai/video.mp4\"]}",
    "createTime": 1767965610929,
    "completeTime": 1767965652317,
    "costTime": 41388
  }
}
```

任務失敗時的回應範例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "SensitiveContentDetected",
    "failMsg": "The input or output was flagged as sensitive. Please try again with different inputs.",
    "createTime": 1767965610929,
    "completeTime": 1767965652317
  }
}
```

等 `state` 變為 `success` 後，再解析 `resultJson`：

```javascript theme={null}
const payload = JSON.parse(data.resultJson);
const videoUrls = payload.resultUrls;
```

## 請求體

### 文生影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "text-to-video",
    "prompt": "a calm seaside sunrise with waves, distant birds, and soft narration",
    "duration": 5,
    "sound": true,
    "aspect_ratio": "16:9",
    "negative_prompt": "blur, low quality",
    "cfg_scale": 0.6
  }
}
```

### 圖生影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "image-to-video",
    "prompt": "animate this product photo with a slow camera push and subtle ambient music",
    "duration": 10,
    "sound": true,
    "image_urls": [
      "https://example.com/start.jpg",
      "https://example.com/end.jpg"
    ],
    "negative_prompt": "jitter, distortion",
    "cfg_scale": 0.4
  }
}
```

## 參數

**request\_type** `string` *required* (default: async)

取得結果的方式。選擇 `async` 時，透過 `statusTask` 輪詢結果；選擇 `callback` 時，透過 Webhook 接收結果。

**callback\_url** `string`

當 `request_type` 為 `callback` 時必填。請提供可從網際網路存取的 HTTPS 網址，以接收任務的最終結果。詳見 [Webhook](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)。

**input** `object` *required*

Kling 2.6 的輸入參數。

**屬性**

**mode** `string` *required*

生成模式。可用值為 `text-to-video`、`image-to-video`。

**prompt** `string` *required*

描述所需影片和可選音訊的文字提示。支援1-1000字元。

**duration** `integer` *required*

影片時長（以秒為單位）。支援的值：`5`、`10`。為了相容，接受 `"5"` 和 `"10"` 等數字字串。

**sound** `boolean` *required*

是否產生音訊。使用 `true` 表示語音、音效、氣氛或音樂；使用 `false` 進行無聲影片。

**aspect\_ratio** `string` (default: 1:1)

`text-to-video` 的輸出縱橫比。支援的值：`1:1`、`9:16`、`16:9`。不會為 `image-to-video` 轉送此欄位。

**image\_urls** `string[]`

參考影像 URL。`image-to-video` 所需。支援 1-2 個 URL。第一個影像是起始/參考影格；第二個影像可用作支援開始/結束影格控制的路線上的結束影格。每個影像必須為 JPG、JPEG 或 PNG，不大於 10 MB，且每邊至少為 300 個像素。

**negative\_prompt** `string`

選填的負面提示可減少不必要的偽影。最大長度為 500 個字元。

**cfg\_scale** `number`

可選指導尺度。支援的範圍：`0` 到 `1`（以 `0.1` 為增量）。

## 回應格式

### 送出任務時的回應

任務受理後，`POST /generateTask/kling-2-6` 會傳回任務 ID：

**code** `integer`

API 狀態碼。`200` 表示任務已受理。

**message** `string`

狀態說明。

**data.taskId** `string`

任務的唯一 ID，用於查詢任務狀態。

### 狀態回應欄位

**taskId** `string`

任務的唯一識別碼。

**state** `string`

目前的任務狀態，可能的值為 `pending`、`processing`、`success`、`failed`。

**resultJson** `string`

內含生成影片 URL 的 JSON 字串。`state` 為 `success` 時傳回。

**failCode** `string`

供程式判讀的錯誤碼，僅在 `state` 為 `failed` 時傳回。

**failMsg** `string`

失敗原因說明，僅在 `state` 為 `failed` 時傳回。

**createTime** `integer`

任務建立時的 Unix 時間戳記，單位為毫秒。

**completeTime** `integer`

任務完成時的 Unix 時間戳記，單位為毫秒。任務完成後才會傳回。

**costTime** `integer`

處理時間，以毫秒為單位。任務成功後，若有此資料便會傳回。

## Webhook 回呼模式

若希望後端自動收到最終結果，可使用回呼模式，無須輪詢。

```bash theme={null}
curl -X POST "https://api.apixo.ai/api/v1/generateTask/kling-2-6" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "callback",
    "callback_url": "https://your-server.com/webhooks/apixo",
    "input": {
      "mode": "image-to-video",
      "prompt": "animate this mountain view with moving clouds, wind, and distant birds",
      "duration": 5,
      "sound": true,
      "image_urls": [
        "https://example.com/mountain-start.jpg"
      ]
    }
  }'
```

回呼有效負載使用與狀態回應相同的 `code`、`message` 和 `data` 形狀。請參閱 [Webhooks](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md) 以了解傳送要求和重試行為。

## 計費

新提交的非 LLM 請求按 **1 點 = `$0.001`** 計費。先依用量和加購項目計算總額，再套用帳戶折扣，最後將請求總額無條件進位至整數點數。若已約定固定單價，則保留該單價的精度，不再使用標準價或另行打折；最終點數仍須進位。詳見[積分換算與進位](https://apixo.ai/docs/zh-hant/pricing.md#credit-conversion-and-rounding)。

Kling 2.6 按輸出秒數計費。所選的 `duration` 和所需的 `sound` 值決定總成本。

| 音訊設定 | 單價 | 總計範例 |
| - | - | - |
| `sound: false` | `$0.066 / second` | 5s: `$0.330`; 10s: `$0.660` |
| `sound: true` | `$0.132 / second` | 5s: `$0.660`; 10s: `$1.320` |

最新的美元費率請見 [API 價目表](https://apixo.ai/pricing/api)。

## 耗時與輪詢

實際延遲可能會因提示複雜性、音訊要求、提供者路由和目前佇列負載而異。

| 長度 | 一般生成時間 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - | - |
| `5`秒 | 50s-70s | 任務建立 50 秒後 | 5s-10s |
| `10`秒 | 80s-100s | 任務建立 80 秒後 | 5s-10s |

> [!TIP]
>
> 對於生產工作負載，請使用回呼模式以避免在影片和音訊產生仍在運行時頻繁輪詢。

請求頻率與並行數上限會依帳戶、API 金鑰和路由而異。收到 `429` 時，請降低請求頻率，並逐步拉長重試間隔。帳戶層級的限制請見[系統 API](https://apixo.ai/docs/zh-hant/api-reference/system.md)。

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體、模式、參數、提示或媒體無效 URL | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `403` | 鍵或路由無法存取模型，或內容被拒絕 | 檢查權限並調整內容 |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 伺服器錯誤或未知任務失敗 | 逐步拉長間隔後重試 |
| `502` | 服務錯誤 | 逐步拉長間隔後重試 |
| `504` | 服務超時 | 重新嘗試；耗時較長的任務可改用回呼模式 |

### 任務失敗代碼

| 錯誤碼 | 含義 | 處理方式 |
| - | - | - |
| `SensitiveContentDetected` | 提示、輸入影像或輸出被標記為敏感 | 更改提示或輸入影像 |
| `InputOutputSensitiveContentDetected` | 輸入或輸出未通過安全檢查 | 嘗試不同的輸入 |
| `NSFW` | 檢測到 NSFW 含量 | 使用符合政策的輸入 |
| `ProhibitedContentDetected` | 內容違反內容安全要求 | 調整提示或影像 |
| `PromptLengthExceeded` | 提示超出支援的限制 | 將提示縮短至 1000 個字元或更少 |
| `PromptInvalid` | 提示詞無效或遭拒 | 修改提示 |
| `ImageFormatIncorrect` | 無法處理參考影像 | 使用公共直接的 JPG、PNG 或 WebP URL |
| `Upload error` | 媒體上傳失敗或超出大小限制 | 壓縮或替換影像 |
| `RateLimited` | 請求頻率已達上限 | 稍後重試並退避 |
| `Timeout` | 任務逾時 | 重試、簡化請求或使用回呼方式 |

完整列表見[錯誤碼](https://apixo.ai/docs/zh-hant/api-reference/errors.md)。

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/video/kling-2-6.json) 中的 `mcp.result` 指定結果的解析方式，`mcp.attachments` 列出可繫結檔案的欄位。這些中繼資料供用戶端整合使用，無須當作生成參數提交。附件欄位為 `image_urls`。請保留使用者指定的檔案順序。模型參數由 API 驗證。

## 相關連結

* [生成 API 總覽](https://apixo.ai/docs/zh-hant/models.md)
* [影片模型](https://apixo.ai/docs/zh-hant/models/video)
* [送出生成任務](https://apixo.ai/docs/zh-hant/api-reference/generate-task.md)
* [查詢任務狀態](https://apixo.ai/docs/zh-hant/api-reference/status-task.md)
* [Webhook](https://apixo.ai/docs/zh-hant/api-reference/webhooks.md)
* [錯誤碼](https://apixo.ai/docs/zh-hant/api-reference/errors.md)
* [參數規範](https://apixo.ai/docs/zh-hant/api-reference/parameters.md)
* [路由策略](https://apixo.ai/docs/zh-hant/concepts/routing-strategies.md)
* [在 APIXO Playground 嘗試 Kling 2.6](https://apixo.ai/models/kling-2-6)
* [API定價](https://apixo.ai/pricing/api)


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