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

> Use Kling 4.0 through AnyFast API for text-to-video, image-to-video, Omni references, and video editing.

Use Kling 4.0 through AnyFast API to generate or edit videos from text prompts, keyframe images, and image or video references. Kling 4.0 uses a new interface: the model name is in the request path, output settings go in `settings`, and general options go in `options`.

> [!NOTE]
>
> Use the `/kling/v2/.../kling-4.0` creation endpoints shown here. Do not include `model` or `model_name` in the request body. Do not mix in Kling Legacy parameters such as `mode: "std"`, a top-level string `duration`, or `multi_prompt`.

## Key capabilities

* **Text to video** — Describe the scene, people, action, camera work, and sound in a prompt.
* **Image to video** — Submit a prompt and first, middle, or last frame images in `contents`.
* **Omni reference generation** — Combine prompts, reference images, and reference videos, then assign each asset a role in the prompt using its `id`.
* **Video editing** — Use the Omni reference endpoint with a `base_video` and describe what to change and preserve.
* **Asynchronous task query** — Query status and results using the system task ID from the create response.

| Capability | Creation endpoint | Main input |
| - | - | - |
| Text to video | `POST /kling/v2/text-to-video/kling-4.0` | `prompt` |
| Image to video | `POST /kling/v2/image-to-video/kling-4.0` | Prompt and keyframe images in `contents` |
| Omni reference generation | `POST /kling/v2/omni-video/kling-4.0` | Prompt, reference images, and reference videos |
| Video editing | `POST /kling/v2/omni-video/kling-4.0` | Prompt and `base_video` |

## Base URL and authentication

Use `https://www.anyfast.ai` as the base URL. Include these request headers:

```http theme={null}
Authorization: Bearer YOUR_ANYFAST_API_KEY
Content-Type: application/json
```

Replace `YOUR_ANYFAST_API_KEY` with an AnyFast API key that has access to this model.

## Quick examples

```bash cURL — Text to video 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": "A red car drives slowly along a rain-soaked street as the camera pans right. Realistic style, natural rain sound, no subtitles, and no sudden cuts.",
    "settings": {
      "resolution": "720p",
      "aspect_ratio": "21:9",
      "duration": 3
    },
    "options": {
      "watermark_info": {
        "enabled": false
      }
    }
  }'
```

```bash cURL — Image to video 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 from @start and preserve the person's identity and composition. The person smiles and raises a hand as a breeze moves their hair. Add natural wind sound. Use one continuous shot, no subtitles."
      },
      {
        "type": "first_frame",
        "url": "https://example.com/first-frame.jpg",
        "id": "start"
      }
    ],
    "settings": {
      "resolution": "720p",
      "duration": 3
    },
    "options": {
      "watermark_info": {
        "enabled": false
      }
    }
  }'
```

```bash cURL — Omni reference generation 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": "Preserve the face, hairstyle, and clothes of the person in @actor. Use the action pacing from @motion. Show the person moving naturally in a bright training studio. Keep the camera steady and add no subtitles."
      },
      {
        "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
      }
    }
  }'
```

```bash cURL — Video editing 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": "Edit @base: change only the red ceramic cup in the person's hand to blue. Preserve the person's face, clothes, hand movement, background, camera motion, dialogue, and ambient sound. Do not add objects or subtitles."
      },
      {
        "type": "base_video",
        "url": "https://example.com/source-video.mp4",
        "id": "base"
      }
    ],
    "settings": {
      "resolution": "720p",
      "aspect_ratio": "adaptive"
    },
    "options": {
      "watermark_info": {
        "enabled": false
      }
    }
  }'
```

## Query task results

Video generation is asynchronous. A successful create response means the task was accepted, not that the video is ready. Save `data.id` from the create response and query it with the same AnyFast API key:

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

The create response's `data` is an object; the query response's `data` is an array. Create response example:

```json theme={null}
{
  "code": 0,
  "message": "",
  "request_id": "REQUEST_ID",
  "data": {
    "id": "asyntask_EXAMPLE",
    "status": "submitted",
    "create_time": 1793672435967,
    "update_time": 1793672435967,
    "external_id": ""
  }
}
```

| Field | Description |
| - | - |
| `code` | API business status code; the success example is `0` |
| `message` | API message |
| `request_id` | Request tracing identifier, when returned |
| `data.id` | System task ID; save it for querying |
| `data.status` | Current task status |
| `data.create_time` | Creation time as a Unix timestamp in milliseconds |
| `data.update_time` | Update time as a Unix timestamp in milliseconds |
| `data.external_id` | Caller-defined task identifier; may be empty if unset |

| Status | Meaning | Client action |
| - | - | - |
| `submitted` | Submitted | Keep querying |
| `processing` | Generating | Keep querying |
| `succeeded` | Complete | Read `outputs` |
| `failed` | Failed | Save the error and stop polling |

We recommend querying every 10–20 seconds and setting an application-level wait limit. If the limit is reached, keep the task ID and query it later; do not immediately submit the same generation request again.

Successful query response example:

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

Select the output with `type: "video"` from the matching task's `outputs`:

| Field | Description |
| - | - |
| `url` | Video result URL |
| `duration` | Actual duration in seconds; may be a decimal string |
| `watermark_url` | Watermarked result URL, present only when provided |

The query path does not include a model name or require an upstream task ID. Result URLs may expire or include access signatures. Download and save the video soon after generation; do not treat the URL as permanent storage.

## Parameters

### Create request

| Parameter | Type | Required | Description |
| - | - | - | - |
| `prompt` | string | Required for text-to-video | Video description; can include both desired and unwanted content |
| `contents` | object\[] | Used for image-to-video, Omni reference, and video editing | Prompt and keyframe or reference assets; must include a prompt item |
| `settings` | object | No | Output resolution, duration, and aspect ratio |
| `settings.resolution` | string | No | `720p` or `1080p`; explicit value recommended |
| `settings.duration` | integer | No | Integer from 3 to 30 seconds; explicit value recommended |
| `settings.aspect_ratio` | string | No | `21:9`, `16:9`, `9:16`, `1:1`, or `adaptive` |
| `options` | object | No | General options |
| `options.watermark_info.enabled` | boolean | No | Whether to additionally generate a watermarked version; defaults to `false` |
| `options.external_task_id` | string | No | Caller-defined task identifier; keep it unique within the current user scope |
| `options.callback_url` | string | No | Task-status callback URL defined by the official protocol; confirm support on the current channel before use |

### `contents` asset types

| `contents[].type` | Purpose | Main fields |
| - | - | - |
| `prompt` | Describe generation or editing requirements | `text` |
| `first_frame` | First-frame image or constraint | `url`, `id` |
| `middle_frame` | Middle keyframe or constraint | `url`, `id` |
| `last_frame` | Last-frame image or constraint | `url`, `id` |
| `refer_image` | Reference image for appearance, scene, or style | `url`, `id` |
| `feature_video` | Reference video for motion or other features | `url`, `id` |
| `base_video` | Source video to edit | `url`, `id` |

Asset IDs must be unique within a task. Assign separate unique IDs to multiple `middle_frame` or `refer_image` items. The official protocol also defines `element` and `voice` asset types for referencing created characters and voices. These require the corresponding assets and management capabilities, so this page does not include asset-management examples.

## Output and input limits

### Output settings

When `settings.aspect_ratio` is specified, the output uses that ratio. With `adaptive` or when the field is omitted, the system first checks the prompt for an aspect-ratio request, then uses the edited video, keyframes, or other input videos to determine the ratio. If none applies, it uses `16:9`.

For text-to-video, an explicit `settings.duration` takes precedence. If omitted, the system checks the prompt for a duration request and defaults to 10 seconds when none is clear.

For the Omni reference endpoint, duration is determined in this order:

1. Explicit `settings.duration`.
2. A clear duration request in the prompt.
3. The duration of `base_video`.
4. A default of 10 seconds if none of the above is available.

Submit at most one `base_video` to the Omni reference endpoint. The video-editing example omits `settings.duration`; set it to control the output length explicitly.

### Input asset requirements

The following are official parameter constraints; actual availability can also depend on the connected resources.

| Asset | Requirement |
| - | - |
| Image formats | JPG, JPEG, PNG |
| Image size | Up to 30 MB each |
| Image dimensions | At least 300 px wide and 300 px high |
| Image aspect ratio | 2:5–5:2 |
| Number of images | Up to 10 |
| Video formats | MP4, MOV |
| Video size | Up to 300 MB each |
| Video duration | Each video and the total input-video duration must be 3–30 seconds |
| Video dimensions | 700–4553 px wide and high; up to 8,294,400 total pixels |
| Video aspect ratio | 2:5–5:2 |
| Input frame rate | 24–60 fps |
| Output frame rate | 24 fps |

Images can be submitted by URL or supported Base64 content. Videos use URLs. The server must be able to access each URL directly and retrieve the asset file.

The Omni reference endpoint also has these limits:

* Images, videos, elements, and voices combined: up to 15 assets.
* With no video-character elements, up to 5 reference videos.
* With video-character elements, the combined number of video-character elements and reference videos must not exceed 5.
* Total input-video duration includes feature reference videos, the video being edited, and video-character elements.

### Prompt length

These limits apply to the text-to-video `prompt` or the prompt item's `text` in `contents`:

| Text | Limit |
| - | -: |
| English only | 26,000 characters |
| Non-English only | 11,000 characters |
| Mixed text | English characters ÷ 26,000 + non-English characters ÷ 11,000 ≤ 1 |

### Prompting tips

Describe the following in order:

```text theme={null}
Subject and setting → action sequence → camera movement → sound requirements → things to avoid
```

You can describe multiple shots directly in the prompt. Prompts state the intended result; exact action timing and shot cuts are not guaranteed. We recommend explicitly setting `settings.duration` to control output length and cost.

### General options

* `external_task_id` does not replace the system task ID or guarantee duplicate-charge prevention.
* This guide uses `data.id` from the create response to query tasks; callbacks and external-ID queries are not required.

> [card] [API Reference](https://docs.anyfast.ai/api-reference/model-api/kuaishou/kling-v4.md)
>
> View Kling 4.0 request and response fields for each capability.


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