> ## 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.

# Kling Image Generation

> Submit a Kling Image 4.0 generation task using the native contents, settings and options fields.

Send a MixRoute API key as a Bearer token. Use the exact path below: the model catalog lists `kling-image-4.0`, but `/kling/image-generation/kling-image-4.0` is not accepted. Do not add `/v1`, a body `model` field, or a `metadata` wrapper.

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

## Request Body

| Field | Type | Required | Description |
| - | - | - | - |
| `contents` | array | Yes | Input items. Include a nonempty `prompt` item. |
| `settings` | object | No | Controls image count, resolution, and aspect ratio. |
| `options` | object | No | Optional webhook, external task ID, and watermark settings. |

### Input Items

| `type` | Other fields |
| - | - |
| `prompt` | Required. Put the prompt in `text`, not a top-level `prompt` field. |
| `refer_image` | Use `url` for a reachable image URL or Base64 image data and `id` for a unique reference name. |
| `element` | Use `element_id` for a multi-image Element available to the routed upstream account. Set a unique reference `id`. |

Reference images and Elements share a limit of 10 inputs. To reference one explicitly, use `@id` in the prompt. Images must be JPG, JPEG, or PNG, at most 30 MB each; both sides must be at least 300 px and their width-to-height ratio must be between 2:5 and 5:2.

Prompt limit: up to 50,000 English characters or 21,000 non-English characters. For mixed text, the English count divided by 50,000 plus the non-English count divided by 21,000 must not exceed 1.

### Settings

| Field | Type | Default | Description |
| - | - | - | - |
| `result_type` | string | `single` | `single` returns independent images; `series` returns related images. |
| `n` | integer | `1` | 1–9 images; only used when `result_type` is `single`. |
| `series_amount` | integer / string | `4` | 2–10 images or `auto`; only used in `series` mode. `auto` lets the service choose the count. |
| `resolution` | string | `2k` | Choose `2k`, `4k`, or `8k`. |
| `aspect_ratio` | string | `auto` | `auto`, `16:9`, `9:16`, `1:1`, `4:3`, `3:4`, `3:2`, `2:3`, or `21:9`. |

### Options

| Field | Type | Required | Description |
| - | - | - | - |
| `callback_url` | string | No | Webhook URL. Use the gateway task ID from `data.id` for polling. |
| `external_task_id` | string | No | Optional custom ID. It does not replace the gateway ID in task queries. |
| `watermark_info.enabled` | boolean | No | Set to `true` to request a watermarked copy; defaults to `false`. |

## Generate One Image

```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"
    }
  }'
```

An accepted request returns `code: 0` and a gateway task ID in `data.id`; it does not return the image yet.

## Retrieve the Image

Copy `data.id` to `KLING_TASK_ID`. Poll with one ID per call; see [Query Kling Task](https://mixroute.ai/docs/api-reference/endpoint/kling-tasks.md) for statuses and errors.

```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"
        }
      ]
    }
  ]
}
```

When the matching task reports `succeeded`, read the `url` of each `type: "image"` entry in `outputs`. Download from the result URL without forwarding your MixRoute API key.


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