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

# 可灵图像生成

> 使用原生 contents、settings 和 options 字段提交可灵 Image 4.0 生成任务。

使用 MixRoute API Key 进行 Bearer 认证。模型目录列出的是 `kling-image-4.0`，但请求必须使用下方路径；`/kling/image-generation/kling-image-4.0` 无法调用。无需 `/v1` 前缀、请求体中的 `model` 字段或 `metadata` 包装。

`POST https://api.mixroute.ai/kling/image-generation/kling-4.0`

## 请求体

| 字段 | 类型 | 必填 | 说明 |
| - | - | - | - |
| `contents` | array | 是 | 输入项数组。必须包含一个非空的 `prompt` 项。 |
| `settings` | object | 否 | 设置图片数量、分辨率和宽高比。 |
| `options` | object | 否 | 可选的回调、自定义任务 ID 和水印设置。 |

### 输入项

| `type` | 其他字段 |
| - | - |
| `prompt` | 必填。把提示词放在 `text`，不要使用顶层 `prompt` 字段。 |
| `refer_image` | `url` 填可访问的图片地址或 Base64 图像数据，`id` 填任务内唯一的引用名。 |
| `element` | `element_id` 填路由对应上游账户有权使用的多图主体 ID，`id` 填唯一的引用名。 |

参考图和主体合计最多 10 个。提示词中可用 `@id` 指定引用。图片格式为 JPG、JPEG 或 PNG，单张不超过 30 MB，宽高均至少 300 px，宽高比须介于 2:5 和 5:2 之间。

纯英文提示词最多 50,000 个字符，纯非英文最多 21,000 个。混合输入时，英文字符数除以 50,000 加上非英文字符数除以 21,000 的和不得超过 1。

### 输出设置

| 字段 | 类型 | 默认值 | 说明 |
| - | - | - | - |
| `result_type` | string | `single` | `single` 生成独立图片；`series` 生成相互关联的图片。 |
| `n` | integer | `1` | 1–9 张，仅在 `result_type` 为 `single` 时生效。 |
| `series_amount` | integer / string | `4` | 2–10 张或 `auto`，仅在 `series` 模式生效。`auto` 由服务决定张数。 |
| `resolution` | string | `2k` | 可选 `2k`、`4k` 或 `8k`。 |
| `aspect_ratio` | string | `auto` | 可选 `auto`、`16:9`、`9:16`、`1:1`、`4:3`、`3:4`、`3:2`、`2:3` 或 `21:9`。 |

### 通用选项

| 字段 | 类型 | 必填 | 说明 |
| - | - | - | - |
| `callback_url` | string | 否 | 回调 URL。轮询时使用提交响应 `data.id` 中的网关任务 ID。 |
| `external_task_id` | string | 否 | 可选的自定义任务 ID，不能代替查询时所需的网关 ID。 |
| `watermark_info.enabled` | boolean | 否 | 设为 `true` 可请求带水印的副本，默认为 `false`。 |

## 生成一张图片

```bash theme={null}
curl --request POST 'https://api.mixroute.ai/kling/image-generation/kling-4.0' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "contents": [
      {
        "type": "prompt",
        "text": "A single blue ceramic mug centered on a white table, studio product photograph."
      }
    ],
    "settings": {
      "result_type": "single",
      "n": 1,
      "resolution": "2k",
      "aspect_ratio": "1:1"
    }
  }'
```

提交成功后返回 `code: 0` 和 `data.id` 中的网关任务 ID，此时尚未返回图片。

## 查询并获取图片

将 `data.id` 保存为 `KLING_TASK_ID`，每次查询只传一个 ID。状态和错误处理见[查询 Kling 任务](https://mixroute.ai/docs/zh-Hans/api-reference/endpoint/kling-tasks.md)。

```bash theme={null}
curl --get 'https://api.mixroute.ai/kling/tasks' \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --data-urlencode "task_ids=$KLING_TASK_ID"
```

```json theme={null}
{
  "code": 0,
  "message": "SUCCEED",
  "data": [
    {
      "id": "task_example",
      "status": "succeeded",
      "outputs": [
        {
          "type": "image",
          "url": "https://example.com/generated.png"
        }
      ]
    }
  ]
}
```

对应任务的状态变成 `succeeded` 后，读取 `outputs` 中 `type: "image"` 项的 `url`。访问结果地址时不要附带 MixRoute API Key。


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