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

# Kling 4.0

> 通过 AnyFast 调用 Kling 4.0 视频生成与编辑接口。

Kling 4.0 按能力使用不同的创建路径。模型名称包含在请求路径中，请求体中不需要填写 `model` 或 `model_name`。输出配置使用 `settings`，通用选项使用 `options`。

> [tab] 文生视频
>
> ## 创建任务
>
> `POST /kling/v2/text-to-video/kling-4.0`
>
> ```bash cURL theme={null}
> curl --request POST \
>   --url 'https://www.anyfast.ai/kling/v2/text-to-video/kling-4.0' \
>   --header 'Authorization: Bearer YOUR_ANYFAST_API_KEY' \
>   --header 'Content-Type: application/json' \
>   --data '{
>     "prompt": "雨后的街道，一辆红色汽车缓慢驶过，镜头向右平移。写实风格，真实雨声，不要字幕，不要突然切镜头。",
>     "settings": {
>       "resolution": "720p",
>       "aspect_ratio": "21:9",
>       "duration": 3
>     },
>     "options": {
>       "watermark_info": {
>         "enabled": false
>       }
>     }
>   }'
> ```
>
> ## 请求头
>
> **Authorization** `string` *required*
>
> Bearer 身份验证，格式为 `Bearer YOUR_ANYFAST_API_KEY`。
>
> **Content-Type** `string` *required* (default: application/json)
>
> 请求体格式。
>
> ## 请求体
>
> **prompt** `string` *required*
>
> 视频内容描述，可同时写入希望出现及希望避免的内容。字符长度限制按提示词语言类型计算，详见提示词长度。
>
> **settings** `object`
>
> 输出清晰度、时长和画面比例。
>
> **settings.resolution** `string`
>
> `720p` 或 `1080p`，建议显式填写。
>
> **settings.duration** `integer`
>
> 3–30 秒之间的整数，建议显式填写。省略时先识别提示词中的时长意图；没有明确意图时默认 10 秒。
>
> **settings.aspect\_ratio** `string`
>
> `21:9`、`16:9`、`9:16` 或 `1:1`。通用输出设置也支持 `adaptive`。
>
> **options** `object`
>
> 通用任务选项。
>
> **options.watermark\_info.enabled** `boolean` (default: false)
>
> 是否额外生成含水印版本。
>
> **options.external\_task\_id** `string`
>
> 调用方自定义任务标识；当前用户范围内应唯一。此字段不会替代系统任务 ID，也不保证自动防止重复扣费。
>
> **options.callback\_url** `string`
>
> 官方协议定义的任务状态通知地址；使用前需确认当前通道支持情况。
>
> ## 创建响应
>
> ```json 200 theme={null}
> {
>   "code": 0,
>   "message": "",
>   "request_id": "REQUEST_ID",
>   "data": {
>     "id": "asyntask_EXAMPLE",
>     "status": "submitted",
>     "create_time": 1793672435967,
>     "update_time": 1793672435967,
>     "external_id": ""
>   }
> }
> ```
>
> **code** `integer`
>
> 接口业务状态码，成功示例为 `0`。
>
> **message** `string`
>
> 接口消息。
>
> **request\_id** `string`
>
> 请求追踪标识，返回时可用于排查。
>
> **data.id** `string`
>
> 系统任务 ID，需保存并用于查询。
>
> **data.status** `string`
>
> 当前任务状态。
>
> **data.create\_time** `integer`
>
> 创建时间，Unix 毫秒时间戳。
>
> **data.update\_time** `integer`
>
> 更新时间，Unix 毫秒时间戳。
>
> **data.external\_id** `string`
>
> 自定义任务标识，未设置时可能为空。

> [tab] 图生视频
>
> ## 创建任务
>
> `POST /kling/v2/image-to-video/kling-4.0`
>
> ```bash cURL theme={null}
> curl --request POST \
>   --url 'https://www.anyfast.ai/kling/v2/image-to-video/kling-4.0' \
>   --header 'Authorization: Bearer YOUR_ANYFAST_API_KEY' \
>   --header 'Content-Type: application/json' \
>   --data '{
>     "contents": [
>       {
>         "type": "prompt",
>         "text": "从@start开始，保持人物身份和构图。人物微笑抬手，微风吹动头发，有自然风声。连续单镜头，不要字幕。"
>       },
>       {
>         "type": "first_frame",
>         "url": "https://example.com/first-frame.jpg",
>         "id": "start"
>       }
>     ],
>     "settings": {
>       "resolution": "720p",
>       "duration": 3
>     },
>     "options": {
>       "watermark_info": {
>         "enabled": false
>       }
>     }
>   }'
> ```
>
> ## 请求头
>
> **Authorization** `string` *required*
>
> Bearer 身份验证，格式为 `Bearer YOUR_ANYFAST_API_KEY`。
>
> **Content-Type** `string` *required* (default: application/json)
>
> 请求体格式。
>
> ## 请求体
>
> **contents** `object[]` *required*
>
> 提示词和关键帧图片。必须包含 `prompt` 项；最多提交 10 张图片。
>
> **contents\[].type** `string` *required*
>
> 此工作流使用 `prompt`、`first_frame`、`middle_frame` 或 `last_frame`。
>
> **contents\[].text** `string`
>
> 当 `type` 为 `prompt` 时填写提示词。可使用 `@id` 在提示词中引用图片。
>
> **contents\[].url** `string`
>
> 可公开访问的图片 URL，或官方支持的 Base64 图片内容。
>
> **contents\[].id** `string`
>
> 可选的唯一素材 ID，供提示词引用。同一任务内不得重复。
>
> **settings** `object`
>
> 输出设置。
>
> **settings.resolution** `string`
>
> `720p` 或 `1080p`。
>
> **settings.duration** `integer`
>
> 3–30 秒之间的整数。
>
> **settings.aspect\_ratio** `string`
>
> 可选输出比例。使用关键帧时应提供符合目标构图的图片；本示例省略此字段。
>
> **options.watermark\_info.enabled** `boolean` (default: false)
>
> 是否额外生成含水印版本。
>
> **options.external\_task\_id** `string`
>
> 可选的调用方任务标识。此字段不会替代系统任务 ID，也不保证自动防止重复扣费。
>
> **options.callback\_url** `string`
>
> 可选回调 URL；使用前确认当前通道支持情况。
>
> ## 创建响应
>
> ```json 200 theme={null}
> {
>   "code": 0,
>   "message": "",
>   "request_id": "REQUEST_ID",
>   "data": {
>     "id": "asyntask_EXAMPLE",
>     "status": "submitted",
>     "create_time": 1793672435967,
>     "update_time": 1793672435967,
>     "external_id": ""
>   }
> }
> ```
>
> **data.id** `string`
>
> 系统任务 ID，需保存并用于查询。
>
> **data.status** `string`
>
> 初始任务状态，例如 `submitted`。
>
> **data.create\_time** `integer`
>
> 创建时间，Unix 毫秒时间戳。
>
> **data.update\_time** `integer`
>
> 更新时间，Unix 毫秒时间戳。
>
> ### 关键帧类型
>
> | 类型 | 用途 | 字段 |
> | - | - | - |
> | `prompt` | 描述动作、镜头和声音 | `text` |
> | `first_frame` | 首帧图片 | `url`、可选 `id` |
> | `middle_frame` | 中间关键帧 | `url`、可选 `id` |
> | `last_frame` | 尾帧图片 | `url`、可选 `id` |
>
> 提示词需要引用关键帧时为其设置唯一 ID。最多支持 10 张图片。图片要求见“输入要求”选项卡。

> [tab] 全能参考
>
> ## 创建任务
>
> `POST /kling/v2/omni-video/kling-4.0`
>
> ```bash cURL theme={null}
> curl --request POST \
>   --url 'https://www.anyfast.ai/kling/v2/omni-video/kling-4.0' \
>   --header 'Authorization: Bearer YOUR_ANYFAST_API_KEY' \
>   --header 'Content-Type: application/json' \
>   --data '{
>     "contents": [
>       {
>         "type": "prompt",
>         "text": "保持@actor中人物的面容、发型和服装，参考@motion中的动作节奏。人物在明亮的训练场自然活动，镜头平稳，不要字幕。"
>       },
>       {
>         "type": "refer_image",
>         "url": "https://example.com/character.jpg",
>         "id": "actor"
>       },
>       {
>         "type": "feature_video",
>         "url": "https://example.com/motion.mp4",
>         "id": "motion"
>       }
>     ],
>     "settings": {
>       "resolution": "720p",
>       "aspect_ratio": "1:1",
>       "duration": 3
>     },
>     "options": {
>       "watermark_info": {
>         "enabled": false
>       }
>     }
>   }'
> ```
>
> ## 请求头
>
> **Authorization** `string` *required*
>
> Bearer 身份验证，格式为 `Bearer YOUR_ANYFAST_API_KEY`。
>
> **Content-Type** `string` *required* (default: application/json)
>
> 请求体格式。
>
> ## 请求体
>
> **contents** `object[]` *required*
>
> 提示词和参考素材。必须包含提示词项；同一任务中的素材 ID 必须唯一。
>
> **contents\[].type** `string` *required*
>
> 可选 `prompt`、`refer_image`、`first_frame`、`middle_frame`、`last_frame`、`feature_video` 或 `base_video`。
>
> **contents\[].text** `string`
>
> `prompt` 项的提示词。使用 `@id` 说明参考素材的用途。
>
> **contents\[].url** `string`
>
> 图片或视频素材的公开 URL。图片也可使用官方支持的 Base64 内容。
>
> **contents\[].id** `string`
>
> 提示词引用素材时使用的唯一 ID。
>
> **settings** `object`
>
> 输出设置。
>
> **settings.resolution** `string`
>
> `720p` 或 `1080p`。
>
> **settings.duration** `integer`
>
> 3–30 秒之间的整数。省略时依次使用提示词明确的时长、`base_video` 时长；否则默认 10 秒。
>
> **settings.aspect\_ratio** `string`
>
> `21:9`、`16:9`、`9:16`、`1:1` 或 `adaptive`。
>
> **options.watermark\_info.enabled** `boolean` (default: false)
>
> 是否额外生成含水印版本。
>
> **options.external\_task\_id** `string`
>
> 可选的调用方任务标识。此字段不会替代系统任务 ID，也不保证自动防止重复扣费。
>
> **options.callback\_url** `string`
>
> 可选回调 URL；使用前确认当前通道支持情况。
>
> ## 创建响应
>
> ```json 200 theme={null}
> {
>   "code": 0,
>   "message": "",
>   "request_id": "REQUEST_ID",
>   "data": {
>     "id": "asyntask_EXAMPLE",
>     "status": "submitted",
>     "create_time": 1793672435967,
>     "update_time": 1793672435967,
>     "external_id": ""
>   }
> }
> ```
>
> **data.id** `string`
>
> 系统任务 ID，需保存并用于查询。
>
> **data.status** `string`
>
> 初始任务状态，例如 `submitted`。
>
> **data.create\_time** `integer`
>
> 创建时间，Unix 毫秒时间戳。
>
> **data.update\_time** `integer`
>
> 更新时间，Unix 毫秒时间戳。
>
> ### 参考素材类型
>
> | 类型 | 用途 | 主要字段 |
> | - | - | - |
> | `prompt` | 描述生成或编辑要求 | `text` |
> | `refer_image` | 人物外观、场景或风格参考 | `url`、`id` |
> | `first_frame` | 首帧约束 | `url`、`id` |
> | `middle_frame` | 中间关键帧约束 | `url`、`id` |
> | `last_frame` | 尾帧约束 | `url`、`id` |
> | `feature_video` | 动作等特征参考 | `url`、`id` |
> | `base_video` | 待编辑的原视频 | `url`、`id` |
>
> 协议还定义了 `element` 和 `voice` 素材，用于引用已创建的主体和音色。相关调用需要对应资产及管理能力，本页不介绍这些管理接口。
>
> ### 时长确定方式
>
> 时长按以下顺序确定：显式传入的 `settings.duration`、提示词中的明确时长意图、`base_video` 时长；均未提供时默认 10 秒。每个任务最多提交一段 `base_video`。

> [tab] 视频编辑
>
> ## 创建任务
>
> `POST /kling/v2/omni-video/kling-4.0`
>
> 将源视频作为 `base_video` 提交，并同时描述需要修改和需要保留的内容。
>
> ```bash cURL theme={null}
> curl --request POST \
>   --url 'https://www.anyfast.ai/kling/v2/omni-video/kling-4.0' \
>   --header 'Authorization: Bearer YOUR_ANYFAST_API_KEY' \
>   --header 'Content-Type: application/json' \
>   --data '{
>     "contents": [
>       {
>         "type": "prompt",
>         "text": "编辑@base，只把人物手中红色陶瓷杯改成蓝色。保持人物面容、服装、手部动作、背景、镜头运动、台词和环境声不变。不要新增物体或字幕。"
>       },
>       {
>         "type": "base_video",
>         "url": "https://example.com/source-video.mp4",
>         "id": "base"
>       }
>     ],
>     "settings": {
>       "resolution": "720p",
>       "aspect_ratio": "adaptive"
>     },
>     "options": {
>       "watermark_info": {
>         "enabled": false
>       }
>     }
>   }'
> ```
>
> ## 请求头
>
> **Authorization** `string` *required*
>
> Bearer 身份验证，格式为 `Bearer YOUR_ANYFAST_API_KEY`。
>
> **Content-Type** `string` *required* (default: application/json)
>
> 请求体格式。
>
> ## 请求体
>
> **contents** `object[]` *required*
>
> 提示词和待编辑视频。必须包含提示词项；每个任务最多提交一段 `base_video`。
>
> **contents\[].type** `string` *required*
>
> 本编辑示例使用 `prompt` 和 `base_video`。其他 Omni 参考素材类型见“全能参考”选项卡。
>
> **contents\[].text** `string` *required*
>
> 描述需要修改和保留的内容。使用 `@id` 引用源视频。
>
> **contents\[].url** `string` *required*
>
> 可公开访问的源视频 URL。
>
> **contents\[].id** `string` *required*
>
> 提示词引用的唯一素材 ID。
>
> **settings.resolution** `string`
>
> `720p` 或 `1080p`。
>
> **settings.aspect\_ratio** `string`
>
> 使用 `adaptive`，根据提示词或输入视频确定画面比例。
>
> **settings.duration** `integer`
>
> 可选的 3–30 秒整数。省略时依次使用提示词明确的时长、源视频时长；否则默认 10 秒。
>
> **options.watermark\_info.enabled** `boolean` (default: false)
>
> 是否额外生成含水印版本。
>
> **options.external\_task\_id** `string`
>
> 可选的调用方任务标识。此字段不会替代系统任务 ID，也不保证自动防止重复扣费。
>
> **options.callback\_url** `string`
>
> 可选回调 URL；使用前确认当前通道支持情况。
>
> ## 创建响应
>
> ```json 200 theme={null}
> {
>   "code": 0,
>   "message": "",
>   "request_id": "REQUEST_ID",
>   "data": {
>     "id": "asyntask_EXAMPLE",
>     "status": "submitted",
>     "create_time": 1793672435967,
>     "update_time": 1793672435967,
>     "external_id": ""
>   }
> }
> ```
>
> **data.id** `string`
>
> 系统任务 ID，需保存并用于查询。
>
> **data.status** `string`
>
> 初始任务状态。
>
> **data.create\_time** `integer`
>
> 创建时间，Unix 毫秒时间戳。
>
> **data.update\_time** `integer`
>
> 更新时间，Unix 毫秒时间戳。
>
> 需要控制输出时长时填写 `settings.duration`。本示例有意省略该字段；提示词没有明确时长意图时，输出时长采用源视频时长。

> [tab] 查询任务
>
> ## 查询任务
>
> `GET /kling/v2/tasks?task_ids=YOUR_TASK_ID`
>
> 使用创建响应中的系统任务 ID `data.id`。查询时使用提交任务时相同的 AnyFast API Key。查询路径不需要模型名称或原厂任务 ID。
>
> ```bash cURL theme={null}
> curl --request GET \
>   --url 'https://www.anyfast.ai/kling/v2/tasks?task_ids=YOUR_TASK_ID' \
>   --header 'Authorization: Bearer YOUR_ANYFAST_API_KEY'
> ```
>
> ## 查询响应
>
> ```json 200 theme={null}
> {
>   "code": 0,
>   "message": "",
>   "data": [
>     {
>       "id": "asyntask_EXAMPLE",
>       "status": "succeeded",
>       "create_time": 1793672435967,
>       "update_time": 1793672563000,
>       "outputs": [
>         {
>           "type": "video",
>           "url": "https://example.com/generated-video.mp4",
>           "duration": "3.042"
>         }
>       ]
>     }
>   ]
> }
> ```
>
> ## 响应字段
>
> **code** `integer`
>
> 接口业务状态码，成功示例为 `0`。
>
> **message** `string`
>
> 接口消息。
>
> **data** `object[]`
>
> 与任务 ID 匹配的任务记录。查询响应中的 `data` 是数组，创建响应中的 `data` 是对象。
>
> **data\[].id** `string`
>
> 系统任务 ID。
>
> **data\[].status** `string`
>
> `submitted`、`processing`、`succeeded` 或 `failed`。
>
> **data\[].create\_time** `integer`
>
> 创建时间，Unix 毫秒时间戳。
>
> **data\[].update\_time** `integer`
>
> 最后更新时间，Unix 毫秒时间戳。
>
> **data\[].outputs** `object[]`
>
> 任务成功后返回的生成结果。
>
> **data\[].outputs\[].type** `string`
>
> 结果类型；选择 `video` 获取生成视频。
>
> **data\[].outputs\[].url** `string`
>
> 视频结果地址。
>
> **data\[].outputs\[].duration** `string`
>
> 实际结果时长，单位秒，可能为带小数的字符串。
>
> **data\[].outputs\[].watermark\_url** `string`
>
> 含水印版本地址，仅在提供该结果时存在。
>
> 建议每隔 10–20 秒查询一次，并设置应用自己的等待上限。达到上限后保留任务 ID，之后继续查询；不要直接重新提交相同请求。结果地址可能有效期有限或带访问签名，请及时下载保存。

> [tab] 输入要求
>
> ## 图片要求
>
> | 属性 | 要求 |
> | - | - |
> | 格式 | JPG、JPEG、PNG |
> | 大小 | 每张不超过 30 MB |
> | 尺寸 | 宽、高均不小于 300 px |
> | 宽高比 | 2:5–5:2 |
> | 数量 | 最多 10 张 |
>
> 图片可使用 URL 或官方支持的 Base64 内容。服务端必须能直接访问 URL 并取得图片文件。
>
> ## 视频要求
>
> | 属性 | 要求 |
> | - | - |
> | 格式 | MP4、MOV |
> | 大小 | 单个视频不超过 300 MB |
> | 时长 | 单个视频及输入视频总时长均须在 3–30 秒内 |
> | 尺寸 | 宽、高均在 700–4553 px；像素总面积不超过 8,294,400 |
> | 宽高比 | 2:5–5:2 |
> | 输入帧率 | 24–60 fps |
> | 输出帧率 | 24 fps |
>
> 视频使用 URL。服务端必须能直接访问 URL 并取得视频文件。
>
> ## 全能参考数量限制
>
> * 图片、视频、主体和音色总数不超过 15。
> * 无视频角色主体时，参考视频最多 5 个。
> * 存在视频角色主体时，视频角色主体与参考视频合计不超过 5 个。
> * 输入视频总时长包含特征参考视频、待编辑视频及视频角色主体的时长。
> * 每个任务最多提交一段 `base_video`。
>
> ## 提示词长度
>
> 限制适用于文生视频的 `prompt` 或 `contents` 中提示词项的 `text`：
>
> | 内容 | 上限 |
> | - | -: |
> | 纯英文 | 26,000 个字符 |
> | 纯非英文 | 11,000 个字符 |
> | 混合文本 | 英文字符数 ÷ 26,000 + 非英文字符数 ÷ 11,000 ≤ 1 |

> [!NOTE]
>
> 视频生成是异步流程。创建成功表示任务已受理；请使用返回的 `data.id` 查询任务状态和结果。


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