> ## 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.1 API：Standard、Pro 和 Master 影片模式

> 快手 Kling 2.1 影片生成 API，適用於標準、專業和主影片工作流程

## 概覽

Kling 2.1 是快手的影片模型，提供多個質量檔位，支援圖片生成影片，也支援以 Master 畫質進行文字生成影片。在 APIXO Playground 試用後，可依本頁範例呼叫 API。

| 功能 | 值 |
| - | - |
| 模型 ID | `kling-2-1` |
| 模式 | `standard-image-to-video`, `pro-image-to-video`, `master-text-to-video`, `master-image-to-video` |
| 提示詞長度 | 1 至 5000 個字元 |
| 長度 | `5` 或 `10` 秒 |
| 參考圖片 | 無用於 `master-text-to-video`；確切地 1 為 `standard-image-to-video` 與 `master-image-to-video`；1-2 為 `pro-image-to-video` |
| 縱橫比 | 僅適用於 `master-text-to-video`：`16:9`、`9:16`、`1:1` |
| 輸出格式 | MP4 的 URL 位於 `resultJson.resultUrls` 中 |

## 端點與身分驗證

API 基底網址：

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

| 方法 | 端點 | 用途 |
| - | - | - |
| `POST` | `/generateTask/kling-2-1` | 送出生成任務 |
| `GET` | `/statusTask/kling-2-1?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-1" \
  -H "Authorization: Bearer YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "request_type": "async",
    "input": {
      "mode": "standard-image-to-video",
      "prompt": "animate this still life scene with subtle camera movement",
      "duration": 5,
      "image_urls": [
        "https://example.com/source.jpg"
      ]
    }
  }'
```

成功回應：

```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-1?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/xxx.mp4\"]}",
    "createTime": 1767965610929,
    "completeTime": 1767965710132,
    "costTime": 99203
  }
}
```

任務失敗時的回應範例：

```json theme={null}
{
  "code": 200,
  "message": "success",
  "data": {
    "taskId": "task_12345678",
    "state": "failed",
    "failCode": "SensitiveContent",
    "failMsg": "Content was rejected by safety checks, please adjust the prompt",
    "createTime": 1767965610929,
    "completeTime": 1767965620132,
    "costTime": 9203
  }
}
```

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

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

## 請求體

### 標準圖生影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "standard-image-to-video",
    "prompt": "animate this still life scene with subtle camera movement",
    "duration": 5,
    "image_urls": [
      "https://example.com/source.jpg"
    ],
    "negative_prompt": "blur, low quality",
    "cfg_scale": 0.6
  }
}
```

### 專業影像轉影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "pro-image-to-video",
    "prompt": "city skyline timelapse from day to night",
    "duration": 10,
    "image_urls": [
      "https://example.com/start.jpg",
      "https://example.com/end.jpg"
    ],
    "cfg_scale": 0.4
  }
}
```

### 主文字轉影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "master-text-to-video",
    "prompt": "a futuristic drone flying through neon-lit streets at night",
    "duration": 5,
    "aspect_ratio": "9:16",
    "negative_prompt": "grainy, noisy, static",
    "cfg_scale": 0.3
  }
}
```

### 主圖生影片

```json theme={null}
{
  "request_type": "async",
  "input": {
    "mode": "master-image-to-video",
    "prompt": "turn this product photo into a premium cinematic reveal",
    "duration": 5,
    "image_urls": [
      "https://example.com/product.jpg"
    ]
  }
}
```

## 參數

**request\_type** `string` (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.1 的輸入參數。

**屬性**

**mode** `string` *required*

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

**prompt** `string` *required*

描述影片的文字提示。必須是包含 1-5000 個字元的非空字串。

**duration** `integer` *required*

影片時長（以秒為單位）。支援的值：`5` 或 `10`。

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

參考影像 URL。請勿將此欄位傳送給 `master-text-to-video`。準確發送 1 個 URL 給 `standard-image-to-video` 和 `master-image-to-video`。傳送 1-2 個 URL 給 `pro-image-to-video`；第二個 URL 作為支援尾影格連續性的路由中的結束影格參考。每個影像必須為 JPG、JPEG 或 PNG，大小不超過 10 MB，且每邊至少 300 像素。

**aspect\_ratio** `string` (default: 16:9)

`master-text-to-video` 的輸出縱橫比。支援的值：`16:9`、`9:16`、`1:1`。若在 `master-text-to-video` 中省略，後端將使用 `16:9`。影像轉影片模式不會傳遞此欄位。

**negative\_prompt** `string`

對不需要的視覺元素進行負面提示。最大長度為 500 個字元。空白值將不被傳遞。

**cfg\_scale** `number`

及時指導尺度。支援的範圍為 `0` 至 `1`，步長為 `0.1`。建議範圍：`0.3`-`0.7`。

## 回應格式

### 送出任務時的回應

任務受理後，`POST /generateTask/kling-2-1` 會傳回任務 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-1" \
  -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": "master-text-to-video",
      "prompt": "a futuristic drone flying through neon-lit streets at night",
      "duration": 5,
      "aspect_ratio": "16:9"
    }
  }'
```

回呼有效負載使用與狀態端點相同的統一回應格式。請參閱 [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.1 依產生的影片計費。所選的`mode`決定等級，`duration`決定單價。

| 模式 | 等級 | 5秒 | 10秒 |
| - | - | - | - |
| `standard-image-to-video` | Standard | `$0.20 / video` | `$0.40 / video` |
| `pro-image-to-video` | 專業版 | `$0.35 / video` | `$0.70 / video` |
| `master-text-to-video` | 掌握 | `$1.00 / video` | `$2.00 / video` |
| `master-image-to-video` | 掌握 | `$1.00 / video` | `$2.00 / video` |

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

## 耗時與輪詢

實際延遲可能會因提示複雜性、所選層、輸入影像大小、路由和目前佇列負載而異。

| 長度 | 一般生成時間 | 建議首次查詢時間 | 輪詢間隔 |
| - | - | - | - |
| `5`秒 | 60s-90s | 任務建立 60 秒後 | 5s-10s |
| `10`秒 | 90s-120s | 任務建立 60 秒後 | 5s-10s |

> [!TIP]
>
> 對於生產影片工作負載，請使用回呼模式以避免頻繁輪詢。結果 URL 透過 APIXO 存儲，通常可用 15 天，因此請立即下載重要輸出。

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

## 錯誤與排查

### HTTP 錯誤

| 代碼 | 含義 | 處理方式 |
| - | - | - |
| `400` | 請求主體無效、缺少必填欄位、模式不受支援或輸入被拒絕 | 修正請求後再試 |
| `401` | 缺少 API 金鑰，或金鑰無效 | 檢查 `Authorization` 請求標頭 |
| `402` | 餘額或配額不足 | 充值或更換帳號或金鑰 |
| `403` | 金鑰或路由無權訪問該模型 | 檢查權限和路由設定 |
| `429` | 已達到速率或並行限制 | 以指數退避方式重試 |
| `500` | 伺服器錯誤 | 逐步拉長間隔後重試 |
| `502` | 服務錯誤 | 逐步拉長間隔後重試 |
| `504` | 服務超時 | 重新嘗試；耗時較長的任務可改用回呼模式 |

### 任務失敗代碼

`failCode` 標識任務失敗。確切的值可能因故障而異。

| 錯誤碼 | 含義 | 處理方式 |
| - | - | - |
| `Missing required parameter` | 缺少 `input`、`mode`、`prompt`、`duration`、`image_urls` 或 `callback_url` 等必填欄位 | 新增缺失的欄位 |
| `Invalid parameter type` | 欄位的 JSON 類型錯誤 | 檢查`prompt`、`duration`、`image_urls`、`aspect_ratio`、`negative_prompt`和`cfg_scale` |
| `Invalid parameter value` | 欄位具有不受支援的值 | 檢查`mode`、`duration`、`aspect_ratio`和`cfg_scale` |
| `Param length exceeded` | 提示、負提示或圖片數量超出後端限制 | 縮短文字或減少影像數量 |
| `ImageNotAccess` | 無法取得輸入影像 URL | 使用公開的直接 HTTP(S) 影像 URL |
| `SensitiveContent`, `PromptInvalid`, `ProhibitedContentDetected`, `NSFW` | 提示、輸入影像或輸出未通過安全檢查 | 更改提示或影像 |
| `RateLimited` | 請求頻率已達上限 | 逐步拉長間隔後重試 |
| `Timeout` | 任務逾時 | 重試、降低輸入複雜度或使用回呼模式 |
| `Unknown error` | 任務因不明原因失敗 | 逐步拉長間隔後重試，或聯絡支援團隊並提供 `taskId` |

### 常見請求問題

| 問題現象 | 可能的原因 | 處理方式 |
| - | - | - |
| `mode` 被拒絕 | 該模式缺少或不是四個支援的值之一 | 使用`standard-image-to-video`、`pro-image-to-video`、`master-text-to-video`或`master-image-to-video` |
| 文字轉影片拒絕 `image_urls` | `master-text-to-video` 不允許參考影像 | 刪除`image_urls` |
| 影像轉影片拒絕請求 | 缺少所需的影像或發送的影像過多 | 標準/主影像模式精確發送 1 影像，專業模式精確發送 1-2 影像 |
| `aspect_ratio` 似乎被忽略 | 此欄位僅適用於`master-text-to-video` | 當需要控制縱橫比時使用`master-text-to-video` |
| 影像 URL 在生成之前失敗 | 後端驗證公共 URL 可及性 | 使用直接、可存取的 HTTP(S) URL 並避免私有或本地主機鏈接 |

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

## MCP 整合

[完整模型 Schema](https://apixo.ai/docs/models/schemas/video/kling-2-1.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)
* [嘗試 Kling 2.1 在 APIXO Playground](https://apixo.ai/models/kling-2-1)
* [API定價](https://apixo.ai/pricing/api)


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