# Kling 4.0

> Kling 4.0 API on PoYo. Generate 3–30 second videos from text, first and last frames, or image/video references and subjects. Edit source videos at 720p or 1080p.

## Overview

- Submit: `https://api.poyo.ai/api/generate/submit`
- Status: `https://api.poyo.ai/api/generate/status/{task_id}`
- Page: https://poyo.ai/models/kling-4-0
- Model page ID: `kling-4-0`
- API model ID: `kling-4.0`
- Category: Video
- Auth: `Authorization: Bearer $POYO_API_KEY`
- Create a key at https://poyo.ai/dashboard/api-key

This model uses PoYo's unified async API. POST the JSON body below, then poll status with the returned `task_id` or receive a webhook on `callback_url`. Failed tasks are not charged.

## Pricing

- 720p / No video input: 28.5 credits ($0.142) per second
- 720p / With video input: 17 credits ($0.085) per billed second
- 1080p / No video input: 42.5 credits ($0.212) per second
- 1080p / With video input: 25.5 credits ($0.128) per billed second

For more details, see https://poyo.ai/models/kling-4-0.

## API Information

The request envelope is always `{ model, input, callback_url? }`. Input fields below are nested under `input`.

### Request envelope

- `model` (`ModelEnum`, required):
  Public model identifier.
  - Options: `"kling-4.0"`
- `callback_url` (`string`, optional):
  Optional webhook URL for task completion or failure notifications.

### Input Schema

Selects editing when video_url is supplied, reference-to-video when reference images, reference videos, or subjects are supplied, image-to-video when only image_urls is supplied, and text-to-video otherwise. Generation requires exactly one of prompt and multi_prompt; editing requires prompt. All media URLs must be publicly accessible HTTP or HTTPS URLs.

The API accepts the following `input` parameters:

- `prompt` (`string`, optional):
  Video instructions containing at least one non-whitespace character. Provide either prompt or multi_prompt, never both. Editing requires prompt. Reference images, videos, and subjects as @Image1, @Video1, and @Element1 in array order. Existing @image_1 and @video_1 references remain supported.
  - Length: 1 to 26000 characters
- `duration` (`integer`, optional):
  Output duration in whole seconds. Ordinary generation defaults to 5 seconds; multi-shot generation follows the shot total. When supplied with multi_prompt, must equal the shot total. In editing, omit this field to retain the source video duration; chargeable output duration is rounded up to whole seconds.
  - Range: `3` to `30`
- `resolution` (`ResolutionEnum`, optional):
  Output video resolution.
  - Default: `"720p"`
  - Options: `"720p"`, `"1080p"`
- `aspect_ratio` (`AspectRatioEnum`, optional):
  Output aspect ratio for text and reference modes. When omitted, adapts to supplied materials; without materials, defaults to 16:9. Do not provide this field in image or editing modes.
  - Options: `"auto"`, `"16:9"`, `"9:16"`, `"1:1"`, `"21:9"`
- `reference_image_urls` (`list`, optional):
  Up to 10 publicly accessible HTTP or HTTPS reference images. Reference them as @Image1, @Image2, and so on; existing @image_1 references remain supported. In reference mode, these images and the optional starting frame together cannot exceed 10 images. Supported in reference and editing modes.
  - Array of string
  - Length: 0 to 10
- `reference_video_urls` (`list`, optional):
  Up to 5 publicly accessible HTTP or HTTPS reference videos, each lasting 3-30 seconds. Reference them as @Video1, @Video2, and so on; existing @video_1 references remain supported. Reference videos and video-based subjects must have a combined original duration of at most 30 seconds. Do not provide this field in editing mode.
  - Array of string
  - Length: 0 to 5
- `image_urls` (`list`, optional):
  Publicly accessible HTTP or HTTPS image URLs, starting frame first and optional ending frame second. Without references, selects image-to-video. In reference mode, at most one starting frame is allowed and counts toward the 10-image and 15-material limits. Do not provide this field in editing mode.
  - Array of string
  - Length: 1 to 2
- `video_url` (`string`, optional):
  Publicly accessible HTTP or HTTPS source video for editing, lasting 3-30 seconds. Selects editing mode. Supports reference images and image-based subjects; cannot be combined with image_urls, reference_video_urls, video-based subjects, multi_prompt, or aspect_ratio.
- `multi_prompt` (`list`, optional):
  Alternative to prompt for generation modes. Shot durations, including omitted durations defaulting to 5 seconds each, must total 3-30 seconds. If top-level duration is provided, it must equal that total. Cannot be combined with prompt or video_url.
  - Array of object
  - Length: 1 to unbounded
- `kling_elements` (`list`, optional):
  Up to 7 position-based subjects. Use @Element1, @Element2, and so on in the prompt. Starting frames, reference images, reference videos, and subjects combined cannot exceed 15 materials; images inside a subject count as one subject. At most 3 subjects may use videos. Editing supports image-based subjects only.
  - Array of object
  - Length: 0 to 7

Required Parameters Example:

```json
{
  "model": "kling-4.0",
  "input": {
    "prompt": "A paper boat floats through a miniature city at sunset."
  }
}
```

Full Example:

```json
{
  "model": "kling-4.0",
  "input": {
    "prompt": "A paper boat floats through a miniature city at sunset.",
    "resolution": "720p"
  }
}
```

Guide the video with a reference image:

```json
{
  "model": "kling-4.0",
  "input": {
    "prompt": "The character in @image_1 walks along a sunlit forest trail.",
    "reference_image_urls": [
      "https://example.com/character.png"
    ],
    "duration": 10,
    "resolution": "1080p",
    "aspect_ratio": "16:9"
  }
}
```

Guide movement with a reference video:

```json
{
  "model": "kling-4.0",
  "input": {
    "prompt": "Recreate the camera movement from @video_1 in a snowy mountain landscape.",
    "reference_video_urls": [
      "https://example.com/camera-motion.mp4"
    ],
    "duration": 10
  }
}
```

### Output Schema

Submit returns a `task_id`. Poll `GET /api/generate/status/{task_id}` until `status` is `finished` or `failed`. Completed media is in `data.files`.

Response fields:

- `code` (`CodeEnum`, required):
  Business status code. `200` means success; any other value means the request failed. Always check this field: some validation errors return HTTP 200 with `code: 400` in the body.
  
  - `200`: Success
  - `400`: Invalid request parameters
  - `401`: Missing or invalid API key
  - `402`: Insufficient credits
  - `403`: Permission denied
  - `404`: Resource not found
  - `408`: Request timeout
  - `429`: Rate limit exceeded
  - `500`: Internal server error
  - `502`: Upstream service error
  - `503`: Service unavailable
  
  See [Error Codes](/api-manual/error-codes) for details.
  - Options: `200`, `400`, `401`, `402`, `403`, `404`, `408`, `429`, `500`, `502`, `503`
  - Examples: `200`
- `data` (`object`, required):

Example Response:

```json
{
  "code": 200,
  "data": {
    "task_id": "task_example",
    "status": "finished",
    "files": [
      {
        "file_url": "https://storage.poyo.ai/models/kling-4-0/examples/kling-4-0-default-output-1-d8acadb85c8afb54.mp4",
        "file_type": "video"
      }
    ]
  }
}
```

## Usage Examples

### cURL

```bash
curl --request POST \
  --url https://api.poyo.ai/api/generate/submit \
  --header "Authorization: Bearer $POYO_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{"model":"kling-4.0","input":{"prompt":"A paper boat floats through a miniature city at sunset."}}'
```

### Python

```python
import json
import os
import time
import requests

API = "https://api.poyo.ai"
headers = {
    "Authorization": f"Bearer {os.environ['POYO_API_KEY']}",
    "Content-Type": "application/json",
}
payload = json.loads("{\n  \"model\": \"kling-4.0\",\n  \"input\": {\n    \"prompt\": \"A paper boat floats through a miniature city at sunset.\"\n  }\n}")

submit = requests.post(f"{API}/api/generate/submit", headers=headers, json=payload)
submit.raise_for_status()
task_id = submit.json()["data"]["task_id"]

while True:
    status = requests.get(f"{API}/api/generate/status/{task_id}", headers=headers)
    status.raise_for_status()
    data = status.json()["data"]
    if data["status"] in ("finished", "failed"):
        print(data)
        break
    time.sleep(2)
```

### JavaScript

```javascript
const headers = {
  Authorization: `Bearer ${process.env.POYO_API_KEY}`,
  "Content-Type": "application/json",
};

const submit = await fetch("https://api.poyo.ai/api/generate/submit", {
  method: "POST",
  headers,
  body: JSON.stringify({
  "model": "kling-4.0",
  "input": {
    "prompt": "A paper boat floats through a miniature city at sunset."
  }
}),
});

const submitted = await submit.json();
const taskId = submitted.data.task_id;

while (true) {
  const statusResponse = await fetch(`https://api.poyo.ai/api/generate/status/${taskId}`, { headers });
  const status = await statusResponse.json();
  if (["finished", "failed"].includes(status.data.status)) {
    console.log(status.data);
    break;
  }
  await new Promise((resolve) => setTimeout(resolve, 2000));
}
```

## Additional Resources

### Documentation

- [Model Playground](https://poyo.ai/models/kling-4-0)
- [API Documentation](https://docs.poyo.ai/api-manual/video-series/kling-4-0)
- [API Documentation (Markdown)](https://docs.poyo.ai/api-manual/video-series/kling-4-0.md)
- [Task status](https://docs.poyo.ai/api-manual/task-management/query-task-status)
- [Webhooks](https://docs.poyo.ai/api-manual/task-management/webhooks)

### Other agent-readable surfaces

- [Platform overview](https://poyo.ai/llms.txt)
- [Documentation index](https://docs.poyo.ai/llms.txt)
- [Full documentation text](https://docs.poyo.ai/llms-full.txt)
- Any other model: `https://poyo.ai/models/<model-id>/llms.txt`
