Kling AI API Examples
Kling 系列模型需要通过 HTTP API 直接调用。结合当前仓库里的 klingai-api.html 与参数说明文档,建议优先使用以下四类接口:文生视频、图生视频、运动控制、Omni Video。
基础配置
在开始调用前,请先到平台控制台创建 API Key,具体可参考创建 API Key。
请求信息
- API Base URL:
https://api.agentsflare.com - 认证方式:
Authorization: Bearer <API_KEY> - 内容类型:
application/json
参考原厂文档:Kling 详细参数与字段说明(原厂): https://zhipu-ai.feishu.cn/wiki/FcKlwx326iYa5fkZ9dscba8KnCf
能力与模型矩阵
| 能力 | 路径 | 推荐模型 | 说明 |
|---|---|---|---|
| 文生视频 | /klingai/v1/videos/text2video | kling-v3、kling-v2-5-turbo、kling-v1-6 | 纯文本生成视频 |
| 图生视频 | /klingai/v1/videos/image2video | kling-v3、kling-v2-5-turbo、kling-v1-6 | 单图或首尾帧生成视频 |
| 运动控制 | /klingai/v1/videos/motion-control | kling-v2-6 | 用参考动作视频驱动主体 |
| Omni Video | /klingai/v1/videos/omni-video | kling-video-o1、kling-v3-omni | 视频续写、多镜头、元素引用 |
| 任务查询 | GET /klingai/v1/videos/<endpoint>/{id} | - | 轮询生成状态与结果 |
通用请求头
http
Authorization: Bearer <YOUR_API_KEY>
Content-Type: application/json常用字段说明
| 字段 | 类型 | 说明 |
|---|---|---|
model_name | string | 模型名 |
prompt | string | 正向提示词,建议写清主体、动作、镜头、光线 |
negative_prompt | string | 反向提示词,可留空字符串 |
duration | string | 视频时长,常用 5 或 10 |
mode | string | 质量档位,常用 std / pro |
aspect_ratio | string | 画幅比,如 16:9、9:16、1:1 |
sound | string | 是否保留/生成音频,常用 off |
callback_url | string | 异步回调地址,可空 |
external_task_id | string | 业务系统自定义任务 ID,可空 |
提示:站内旧版示例里
image2video主要演示 Base64 图片;klingai-api.html当前示例更多采用可公网访问的image/image_tailURL。两种方式请以你的网关实际开通能力为准。
文生视频(Text to Video)
适合通过提示词直接生成 5~10 秒短视频。
请求参数
| 参数 | 必填 | 说明 |
|---|---|---|
model_name | 是 | 推荐 kling-v3 |
prompt | 是 | 画面主体与动作描述 |
negative_prompt | 否 | 不希望出现的内容 |
duration | 是 | 例如 5 |
mode | 否 | pro 质量更高 |
sound | 否 | 建议先传 off |
aspect_ratio | 否 | 如 16:9 |
bash
curl -X POST "https://api.agentsflare.com/klingai/v1/videos/text2video" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model_name": "kling-v3",
"prompt": "A cute little rabbit running on the road fast as a racing car",
"negative_prompt": "",
"duration": "5",
"mode": "pro",
"sound": "off",
"aspect_ratio": "1:1",
"callback_url": "",
"external_task_id": ""
}'javascript
const response = await fetch('https://api.agentsflare.com/klingai/v1/videos/text2video', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
model_name: 'kling-v3',
prompt: 'A cute little rabbit running on the road fast as a racing car',
negative_prompt: '',
duration: '5',
mode: 'pro',
sound: 'off',
aspect_ratio: '1:1',
callback_url: '',
external_task_id: ''
})
});
console.log(await response.json());python
import os
import requests
API_BASE = 'https://api.agentsflare.com/klingai/v1/videos/text2video'
API_KEY = os.getenv('API_KEY')
payload = {
'model_name': 'kling-v3',
'prompt': 'A cute little rabbit running on the road fast as a racing car',
'negative_prompt': '',
'duration': '5',
'mode': 'pro',
'sound': 'off',
'aspect_ratio': '1:1'
}
headers = {'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json'}
resp = requests.post(API_BASE, json=payload, headers=headers)
print(resp.status_code)
print(resp.text)java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class KlingText2V {
public static void main(String[] args) throws Exception {
String apiBase = "https://api.agentsflare.com/klingai/v1/videos/text2video";
String apiKey = System.getenv("API_KEY");
String json = "{\"model_name\":\"kling-v3\",\"prompt\":\"A cute little rabbit running on the road fast as a racing car\",\"negative_prompt\":\"\",\"duration\":\"5\",\"mode\":\"pro\",\"sound\":\"off\",\"aspect_ratio\":\"1:1\"}";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(apiBase))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(resp.statusCode());
System.out.println(resp.body());
}
}响应示例
json
{
"code": 0,
"message": "success",
"data": {
"task_id": "2026040901144560ed39e7182144d8"
}
}图生视频(Image to Video)
适合给一张首帧图生成动态视频,也可以追加 image_tail 做首尾帧过渡。
请求参数
| 参数 | 必填 | 说明 |
|---|---|---|
model_name | 是 | 推荐 kling-v3 |
image | 是 | 首帧图片 URL 或网关支持的图片内容 |
image_tail | 否 | 结束帧图片,用于过渡/补间 |
prompt | 是 | 动作与镜头描述 |
duration | 是 | 如 5 |
mode | 否 | std / pro |
sound | 否 | 建议 off |
bash
curl -X POST "https://api.agentsflare.com/klingai/v1/videos/image2video" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model_name": "kling-v3",
"image": "https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-2.png",
"image_tail": "https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-1.png",
"prompt": "Camera zooms out, the girl smiles",
"negative_prompt": "",
"duration": "5",
"mode": "pro",
"sound": "off",
"callback_url": "",
"external_task_id": ""
}'javascript
const payload = {
model_name: 'kling-v3',
image: process.env.KLING_IMAGE_URL,
image_tail: process.env.KLING_IMAGE_TAIL_URL || '',
prompt: 'Camera zooms out, the girl smiles',
negative_prompt: '',
duration: '5',
mode: 'pro',
sound: 'off',
callback_url: '',
external_task_id: ''
};
const response = await fetch('https://api.agentsflare.com/klingai/v1/videos/image2video', {
method: 'POST',
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`,
'Content-Type': 'application/json'
},
body: JSON.stringify(payload)
});
console.log(await response.json());python
import os
import requests
API_BASE = 'https://api.agentsflare.com/klingai/v1/videos/image2video'
API_KEY = os.getenv('API_KEY')
payload = {
'model_name': 'kling-v3',
'image': 'https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-2.png',
'image_tail': 'https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-1.png',
'prompt': 'Camera zooms out, the girl smiles',
'duration': '5',
}
headers = {'Authorization': f'Bearer {API_KEY}', 'Content-Type': 'application/json'}
resp = requests.post(API_BASE, json=payload, headers=headers)
print(resp.status_code)
print(resp.text)java
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class KlingImage2V {
public static void main(String[] args) throws Exception {
String apiBase = "https://api.agentsflare.com/klingai/v1/videos/image2video";
String apiKey = System.getenv("API_KEY");
String json = "{\"model_name\":\"kling-v3\",\"image\":\"https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-2.png\",\"image_tail\":\"https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-1.png\",\"prompt\":\"Camera zooms out, the girl smiles\",\"duration\":\"5\"}";
HttpRequest req = HttpRequest.newBuilder()
.uri(URI.create(apiBase))
.header("Authorization", "Bearer " + apiKey)
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofString(json))
.build();
HttpResponse<String> resp = HttpClient.newHttpClient().send(req, HttpResponse.BodyHandlers.ofString());
System.out.println(resp.statusCode());
System.out.println(resp.body());
}
}运动控制(Motion Control)
kling-v2-6 适合“角色图 + 动作参考视频”这类场景,例如让人物跟随参考视频完成一致动作。
推荐参数
| 参数 | 必填 | 说明 |
|---|---|---|
model_name | 是 | 固定为 kling-v2-6 |
image_url | 是 | 角色/主体图片 |
video_url | 是 | 动作参考视频 |
prompt | 否 | 补充服装、镜头、背景等描述 |
keep_original_sound | 否 | yes / no |
character_orientation | 否 | 示例值为 image |
mode | 否 | 推荐 pro |
bash
curl -X POST "https://api.agentsflare.com/klingai/v1/videos/motion-control" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model_name": "kling-v2-6",
"image_url": "https://p2-kling.klingai.com/kcdn/cdn-kcdn112452/kling-qa-test/multi-3.ng.png",
"video_url": "https://your-public-video.mp4",
"prompt": "The girl is wearing a loose gray T-shirt and denim shorts",
"keep_original_sound": "yes",
"character_orientation": "image",
"mode": "pro",
"callback_url": "",
"external_task_id": "demo-motion-001"
}'Omni Video(Omni / 多镜头续写)
kling-video-o1 与 kling-v3-omni 适合“基于已有视频继续生成”“多镜头脚本串联”“元素控制”等高级场景。
关键参数
| 参数 | 说明 |
|---|---|
model_name | kling-video-o1 或 kling-v3-omni |
prompt | 可在文本中用 <<<video_1>>>、<<<image_1>>> 引用素材 |
video_list | 输入素材列表,常见字段有 video_url、refer_type、keep_original_sound |
multi_shot | 是否多镜头模式 |
shot_type | 多镜头组织方式;启用 multi_shot=true 时需要结合使用 |
multi_prompt | 每个分镜的 prompt / duration 配置 |
aspect_ratio | 视频画幅 |
mode | 质量档位 |
bash
curl -X POST "https://api.agentsflare.com/klingai/v1/videos/omni-video" \
-H "Authorization: Bearer $API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model_name": "kling-video-o1",
"prompt": "Based on <<<video_1>>>, generate the next shot.",
"video_list": [
{
"video_url": "https://your-public-video.mp4",
"refer_type": "feature",
"keep_original_sound": "yes"
}
],
"aspect_ratio": "16:9",
"mode": "pro"
}'多镜头示例结构
json
{
"model_name": "kling-v3-omni",
"multi_shot": true,
"shot_type": "customize",
"multi_prompt": [
{ "index": 1, "prompt": "Wide shot of the city at dusk", "duration": 2 },
{ "index": 2, "prompt": "Camera pushes toward the balcony", "duration": 3 }
],
"aspect_ratio": "16:9",
"mode": "pro"
}任务查询(Query Task)
创建任务后,请使用返回的 task_id 轮询结果。
查询路径
GET /klingai/v1/videos/text2video/{id}GET /klingai/v1/videos/image2video/{id}GET /klingai/v1/videos/motion-control/{id}GET /klingai/v1/videos/omni-video/{id}
bash
curl -X GET "https://api.agentsflare.com/klingai/v1/videos/text2video/$TASK_ID" \
-H "Authorization: Bearer $API_KEY"兼容旧版多图生视频
如果你的通道仍开启旧版 multi-image2video,可以继续沿用原有写法;新项目更建议优先迁移到 image2video + image_tail 或 omni-video。
json
{
"model_name": "kling-v1-6",
"image_list": [
{ "image": "<image_1>" },
{ "image": "<image_2>" }
],
"prompt": "smooth transition between the two images",
"aspect_ratio": "16:9",
"duration": "5"
}调试建议
- 先提交任务,再用
GET /{id}轮询,不要长时间阻塞单个请求。 - 图片 / 视频 URL 需要公网可访问,且尽量稳定。
prompt建议同时描述 主体 + 动作 + 镜头 + 光线 + 风格。- 如果需要业务侧追踪,请始终填写
external_task_id。 - 示例调用以 cURL、Python、Node、Go 四种形式给出;详细参数请参阅原厂文档(上方链接)。
查询任务状态
视频生成是异步任务,提交后需要通过任务 ID 查询生成进度和结果。
请求示例
bash
curl -X GET "https://api.agentsflare.com/klingai/v1/videos/text2video/$TASK_ID" \
-H "Authorization: Bearer $API_KEY"python
import requests
API_KEY = "你的APIKey"
TASK_ID = "xxxxxxxxxxxxxx"
url = f"https://api.agentsflare.com/klingai/v1/videos/text2video/{TASK_ID}"
headers = {
"Authorization": f"Bearer {API_KEY}"
}
response = requests.get(url, headers=headers)
print(response.status_code)
print(response.json())javascript
const fetch = require('node-fetch');
const API_KEY = '你的APIKey';
const TASK_ID = 'xxxxxxxxxxxxxx';
const url = `https://api.agentsflare.com/klingai/v1/videos/text2video/${TASK_ID}`;
const options = {
method: 'GET',
headers: {
'Authorization': `Bearer ${API_KEY}`
}
};
fetch(url, options)
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));go
package main
import (
"fmt"
"io"
"net/http"
)
func main() {
taskID := "xxxxxxxxxxxxxx"
url := "https://api.agentsflare.com/klingai/v1/videos/text2video/" + taskID
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println("Error:", err)
return
}
req.Header.Set("Authorization", "Bearer {API-KEY}")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Println("Error:", err)
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println("Error:", err)
return
}
fmt.Println("Status:", resp.StatusCode)
fmt.Println("Response:", string(body))
}提示
查询不同接口生成的任务,请使用对应的路径:
- 文生视频:
/klingai/v1/videos/text2video/{task_id} - 图生视频:
/klingai/v1/videos/image2video/{task_id} - 多图生视频:
/klingai/v1/videos/multi-image2video/{task_id}
响应示例
json
{
"code": 0,
"message": "success",
"data": {
"task_id": "xxxxxxxxxxxxxx",
"task_status": "succeed",
"task_result": {
"videos": [
{
"url": "https://cdn.example.com/video.mp4",
"duration": "5"
}
]
}
}
}请求参数说明
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
model_name | string | 是 | 模型名称,如 kling-v2-5-turbo、kling-v1-6 |
prompt | string | 是 | 视频描述文字 |
aspect_ratio | string | 否 | 画面比例,如 16:9、9:16、1:1 |
duration | string | 否 | 视频时长(秒),如 "5" |
image | string | 否 | Base64 编码的图片(图生视频时必填) |
image_list | array | 否 | 图片列表,每项包含 image 字段(多图生视频时必填) |