# Kling 图片生成 API 对接文档

## 概述

Kling 图片生成系列共 7 个模型，覆盖文生图、参考图生图、参考主体生图、关联组图和长图设计。全系共用同一套请求体与任务体系，**切换模型只需换接口路径**，其余字段不变。

各模型能力不同（清晰度档、参考图数量、是否支持组图），下单前请对照「模型能力对照表」确认。传了该模型不支持的参数会返回参数错误，**不会静默忽略**。

| 项 | 说明 |
|----|------|
| 协议形态 | 异步任务：建单返回 `taskId`，轮询或回调取结果 |
| 计费 | 按出图张数计费 |
| 参考图 | 支持公网 URL 或 Base64，单张不超过 30MB |
| 提示词 | 不超过 2500 个字符 |

**Base URL**: `https://mm-internal-cn.leonecloud.com`（国内专线）

---

## 认证方式

所有接口均需要在请求头中携带 API Key：

```
Authorization: Bearer {YOUR_API_KEY}
```

---

## 模型能力对照表

| 模型 | 接口路径 | 清晰度 | 画面比例 | 参考图 | 参考主体 | 组图 |
|------|---------|--------|---------|--------|---------|------|
| `kling-image-v3` | `/api/v2/open/aigc/kling-image-v3` | `1k` / `2k` | 不支持 `auto` | 1 张生效 | ✅ | ❌ |
| `kling-image-v3-omni` | `/api/v2/open/aigc/kling-image-v3-omni` | `1k` / `2k` / `4k` | 支持 `auto`（默认） | 多张 | ✅ | ✅ 最多 9 张 |
| `kling-image-o1` | `/api/v2/open/aigc/kling-image-o1` | `1k` / `2k` | 支持 `auto`（默认） | 多张 | ❌ | ❌ |
| `kling-image-v2.1` | `/api/v2/open/aigc/kling-image-v2.1` | **无此参数** | 不支持 `auto` | 1 张生效 | ❌ | ❌ |
| `kling-image-v2.1-ref` | `/api/v2/open/aigc/kling-image-v2.1-ref` | **无此参数** | 不支持 `auto` | **必填 1-4 张** | ❌ | ❌ |
| `kling-image-v4` | `/api/v2/open/aigc/kling-image-v4` | **`2k` / `4k` / `8k`** | 支持 `auto`（默认） | 多张 | ✅ | ✅ **最多 10 张** |
| `kling-image-v4-long` | `/api/v2/open/aigc/kling-image-v4-long` | `1k` / `2k` | **无此参数** | 多张 | ❌ | ❌ 恒出 1 张 |

**几处容易踩的差异**

- `kling-image-v4` **没有 `1k` 档**，最低 `2k`。按其他模型的习惯传 `1k` 会被拒。
- `kling-image-v2.1` 与 `kling-image-v2.1-ref` **没有清晰度参数**，传 `resolution` 会返回参数错误。
- `aspectRatio: "auto"` 仅 `kling-image-v3-omni` / `kling-image-o1` / `kling-image-v4` 支持，其余模型传了会被拒（不会降级成默认比例）。
- 组图上限不统一：`kling-image-v3-omni` 是 9 张，`kling-image-v4` 是 10 张。
- `kling-image-v4-long` 每次只出 1 张完整长图，传 `n > 1` 或 `seriesAmount` 会被拒。

---

## 快速开始

**文生图**
```bash
curl -X POST "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/kling-image-v3" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "白色背景下的蓝色陶瓷杯，影棚产品摄影，柔和侧光",
    "resolution": "2k",
    "aspectRatio": "1:1",
    "n": 2
  }'
```

**参考图生图**
```bash
curl -X POST "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/kling-image-v3-omni" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "参考这张产品图，换成户外草地场景，自然光",
    "imageUrls": ["https://example.com/mug.png"],
    "resolution": "2k",
    "aspectRatio": "auto"
  }'
```

**关联组图**
```bash
curl -X POST "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/kling-image-v3-omni" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "同一只陶瓷杯的多个视角，光线保持一致",
    "imageUrls": ["https://example.com/mug.png"],
    "resolution": "2k",
    "resultType": "series",
    "seriesAmount": 4
  }'
```

**Kling 4.0 图片：用 `@id` 在提示词里指定素材**
```bash
curl -X POST "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/kling-image-v4" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "以 @mug 中的陶瓷杯为主体，放在 @desk 的木桌上，生成一组不同视角的产品图",
    "imageUrls": [
      "https://example.com/mug.png",
      "https://example.com/desk.png"
    ],
    "refImageIds": ["mug", "desk"],
    "resolution": "4k",
    "resultType": "series",
    "seriesAmount": 6
  }'
```

**长图：电商详情页**
```bash
curl -X POST "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/kling-image-v4-long" \
  -H "Authorization: Bearer your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "为 @product 中的咖啡机设计电商详情页，包含主视觉、功能介绍和厨房使用场景",
    "imageUrls": ["https://example.com/coffee-machine.png"],
    "refImageIds": ["product"],
    "skill": "ecommerce",
    "length": "medium",
    "resolution": "2k"
  }'
```

**查询任务**
```bash
curl -X GET "https://mm-internal-cn.leonecloud.com/api/v2/open/aigc/task_20261120182953_bx5gm0k5" \
  -H "Authorization: Bearer your_api_key_here"
```

---

## 接口列表

| Method | 路径 | 用途 |
|--------|------|------|
| POST | `/api/v2/open/aigc/{模型}` | 创建图片生成任务，`{模型}` 取值见能力对照表 |
| GET | `/api/v2/open/aigc/{taskId}` | 查询单个任务 |
| POST | `/api/v2/open/aigc/batch` | 批量查询任务（最多 100 个） |
| GET | `/api/v2/open/balance` | 查询账户余额 |

---

## 创建任务

**POST** `/api/v2/open/aigc/{模型}`

**Content-Type**: `application/json`

### 请求参数

| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| prompt | string | 是 | 提示词，不超过 2500 个字符 |
| negativePrompt | string | 否 | 负向提示词，不超过 2500 个字符。**仅纯文生图生效**，详见下文说明 |
| imageUrls | string[] | 否 | 参考图，支持公网 URL 或 Base64，单张不超过 30MB |
| image | string[] | 否 | `imageUrls` 的别名，两者等价；同时传时以 `imageUrls` 为准 |
| sceneImage | string | 否 | 场景参考图，**仅 `kling-image-v2.1-ref` 支持** |
| styleImage | string | 否 | 风格参考图，**仅 `kling-image-v2.1-ref` 支持** |
| elementIds | string[] | 否 | 参考主体 ID（来自主体素材库），仅支持参考主体的模型可用 |
| resolution | string | 否 | 清晰度档，取值范围按模型不同，见能力对照表 |
| aspectRatio | string | 否 | 画面比例：`16:9` / `9:16` / `1:1` / `4:3` / `3:4` / `3:2` / `2:3` / `21:9`，部分模型支持 `auto` |
| n | integer | 否 | 出图张数，1-9，默认 1。`resultType` 为 `series` 时本字段无效 |
| resultType | string | 否 | `single`（默认）/ `series`。仅支持组图的模型可用 |
| seriesAmount | integer | 否 | 组图张数，默认 4。上限按模型不同（见能力对照表）。仅 `resultType=series` 时生效 |
| refImageIds | string[] | 否 | 与 `imageUrls` 一一对应的素材标识，供提示词里 `@标识` 引用。**仅 Kling 4.0 图片两档支持** |
| styleRefImage | string | 否 | 风格参考图，**仅 `kling-image-v4-long` 支持**，最多 1 张 |
| subjectRefImage | string | 否 | 主体参考图，**仅 `kling-image-v4-long` 支持**，最多 1 张 |
| skill | string | 否 | 长图类型：`webpage` 网页设计 / `ecommerce` 电商详情页。**`kling-image-v4-long` 必填**，其他模型不可传 |
| length | string | 否 | 内容长度：`short` / `medium` / `long`，**仅 `kling-image-v4-long` 支持** |
| callbackUrl | string | 否 | 任务完成后的回调通知 URL |
| taskNickname | string | 否 | 任务备注名，便于在控制台识别 |

### 关于 `negativePrompt`

负向提示词仅在**纯文生图**（未传任何参考图）时生效。带参考图时该字段会被忽略，**不会报错**。需要排除某些元素时，建议把否定描述直接写进 `prompt`。

支持 `negativePrompt` 的模型：`kling-image-v3`、`kling-image-v2.1`。其余模型不支持该参数。

### 关于 `refImageIds`（Kling 4.0 图片两档）

Kling 4.0 图片支持在提示词里用 `@标识` 精确指定某张参考图，适合多图组合的场景：

```json
{
  "prompt": "把 @person 的人物放进 @scene 的场景里",
  "imageUrls": ["https://example.com/person.png", "https://example.com/scene.png"],
  "refImageIds": ["person", "scene"]
}
```

- `refImageIds` 按下标与 `imageUrls` 对应，标识在单次任务内需唯一。
- 不传 `refImageIds` 时素材仍会正常参与生成，只是无法在提示词里精确引用。

### 关于长图（`kling-image-v4-long`）

长图用于生成**一张完整的长幅图像**，而非多张独立图片。

| skill | 用途 | 参考图 | length |
|-------|------|--------|--------|
| `webpage` | 网页设计长图 | 可选，支持 `styleRefImage` 参考排版风格、`subjectRefImage` 围绕主体设计 | 不生效 |
| `ecommerce` | 电商详情页 | 建议提供 1-3 张商品图 | `short` 约 2-5 个内容区块、`medium` 约 6-10 个、`long` 约 11-18 个 |

- `skill` 为必填，不传会返回参数错误。
- 恒定输出 1 张，不接受 `n > 1` 和 `seriesAmount`。
- 没有 `aspectRatio` 参数，版式由 `skill` 与内容决定。

### 响应参数

| 参数 | 类型 | 说明 |
|------|------|------|
| code | int | 状态码，0 表示成功 |
| msg | string | 状态信息 |
| data.taskId | string | 任务 ID，用于查询任务状态 |
| data.status | string | 任务状态，建单成功为 `processing` |
| data.createdAt | string | 创建时间 |

### 响应示例

**成功**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20261120182953_bx5gm0k5",
    "status": "processing",
    "createdAt": "2026-11-20 18:29:53"
  }
}
```

**参数错误（清晰度不支持）**
```json
{
  "code": 10002,
  "msg": "该模型不支持的清晰度，可选: 2k / 4k / 8k",
  "data": null
}
```

**参数错误（长图不支持多张）**
```json
{
  "code": 10002,
  "msg": "该模型每次仅生成 1 张长图",
  "data": null
}
```

> 组图张数越界时返回 `10002`，错误文案里会带该模型的允许范围（`kling-image-v3-omni` 为 2-9，`kling-image-v4` 为 2-10）。

---

## 查询任务状态

**GET** `/api/v2/open/aigc/{taskId}`

### 响应示例

**生成中**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20261120182953_bx5gm0k5",
    "status": "processing",
    "createdAt": "2026-11-20 18:29:53",
    "updatedAt": "2026-11-20 18:29:55"
  }
}
```

**成功**
```json
{
  "code": 0,
  "msg": "success",
  "data": {
    "taskId": "task_20261120182953_bx5gm0k5",
    "status": "success",
    "result": [
      "https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/2026/11/20/output_001.png",
      "https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/2026/11/20/output_002.png"
    ],
    "createdAt": "2026-11-20 18:29:53",
    "updatedAt": "2026-11-20 18:30:41"
  }
}
```

`status` 取值：`processing` 生成中 / `success` 成功 / `failed` 失败。失败时 `errorMsg` 带原因。

---

## 批量查询任务状态

**POST** `/api/v2/open/aigc/batch`

一次最多查询 100 个任务。

---

## 回调通知

创建任务时传了 `callbackUrl`，任务终态（成功或失败）会向该地址发 POST 请求。

**Headers**
```
Content-Type: application/json
X-Funcloud-Event: task.completed
X-Funcloud-Signature: {签名}
```

**Body**
```json
{
  "event": "task.completed",
  "taskId": "task_20261120182953_bx5gm0k5",
  "status": "success",
  "result": ["https://fc-gw-sh.oss-accelerate.aliyuncs.com/images/output_001.png"],
  "errorMsg": "",
  "timestamp": "2026-11-20T18:30:41+08:00",
  "signature": "a1b2c3d4e5f6..."
}
```

---

## 计费说明

**按出图张数计费**，单价由模型与清晰度档决定。

计费张数的取法：

| 场景 | 计费张数 |
|------|---------|
| 普通出图 | `n` 的值（不传按 1） |
| 组图（`resultType=series`） | `seriesAmount` 的值（不传按 4） |
| 长图（`kling-image-v4-long`） | 恒为 1 |

> 组图模式下 `n` 不参与计费，普通模式下 `seriesAmount` 不参与计费 —— 两者互斥，计费只认生效的那一个。

下单时按计费张数预先冻结额度，任务成功后扣费，失败则全额解冻。具体单价见控制台定价页。

---

## 错误码

| code | 说明 |
|------|------|
| 0 | 成功 |
| 10002 | 参数缺失或格式错误 |
| 10005 | API Key 无效或缺失 |
| 30003 | 任务不存在 |
| 40001 | 余额不足 |
| 60001 | 该模型暂未开放 |
| 90003 | 服务器内部错误 |

---

## 最佳实践

### 1. 轮询策略

- 前 30 秒：每 3 秒查询一次
- 30 秒后：每 5 秒查询一次

生产环境建议用回调通知代替轮询。

### 2. 模型选型

| 需求 | 推荐模型 |
|------|---------|
| 常规文生图 / 单图参考 | `kling-image-v3` |
| 多图参考 + 组图 | `kling-image-v3-omni` |
| 最高画质（8K） | `kling-image-v4` |
| 多图组合、提示词精确引用素材 | `kling-image-v4` |
| 主体 + 场景 + 风格三类参考图 | `kling-image-v2.1-ref` |
| 网页设计 / 电商详情页长图 | `kling-image-v4-long` |

### 3. 参考图准备

- 建议 JPEG / PNG，单张不超过 30MB。
- 参考图与参考主体的数量之和有上限（多数模型为 10），超出会被拒。
- 参考图用公网可访问的 URL 最稳妥；Base64 会增大请求体，大图建议先上传取 URL。
