# Kling 4.0

> Kling 4.0 by Kuaishou: one model for text, frames (up to 10), photo/video/audio refs, edit and extend. 3–30 s, 720p or 1080p, always stereo.

Kling 4.0 is Kuaishou's flagship video model. One model instead of separate modes: clips from text, from the first and last frame plus up to 8 mid keyframes (up to 10 images total), from references (up to 10 photos, 5 videos, 3 audio clips), edit of an existing video, or extend from the last frame.

Duration 3 to 30 seconds, 720p or 1080p, aspects 16:9 · 9:16 · 1:1 · 21:9 or auto. Sound is always stereo: «auto» generates new audio, «origin» keeps your clip's sound.

You pay per second of the clip (with a video reference or edit — at the input rate). Failed generations are refunded.

One model, `kling-4.0` · ~4 min. The result follows the `input` fields (prompt, frames, references, `video_mode`, duration) — there are no separate «animate / edit» endpoints.




## Price

| Resolution | Price per second | With your video, per second |
|---|---|---|
| 720p | $0.182 | $0.182 |
| 1080p | $0.273 | $0.273 |

- Price = per-second price × clip length, rounded up to $0.001. Charged when the task is created and fully refunded if it fails.
- With your video, the seconds of the source clip are added to the seconds of the result.

## Media limits

| Type | Limits |
|---|---|
| Images | up to **10**; JPG / PNG / WebP; upload via [files/upload](https://docs.mixmedia.tech/en/endpoints/upload) — up to **30 MB** per file |
| Videos | up to **5**; total ≤ **30 s** (each clip 2…30 s); MP4 / MOV → MP4 on the server; upload ≤ **200 MB** |
| Audio | up to **3**; total ≤ **30 s**; MP3 / WAV; upload ≤ **200 MB** |

Photos — public https or upload; video and audio — files/upload only.


## Parameters (field `input`)

| Parameter | Description | Values (default in bold) |
|---|---|---|
| `prompt` | What happens in the clip: scene, motion, camera, light, style | text, up to 2500 chars |
| `first_frame_url` | First frame: a photo link (https); the video starts from it | `"https://…"` |
| `last_frame_url` | Last frame (optional, only together with the first one) | `"https://…"` |
| `image_urls` | Reference photos (https) | `["https://…"]`, up to 10 |
| `video_urls` | Your videos (MP4): upload them via files/upload first and pass the returned links | `["https://…"]`, up to 5 |
| `audio_urls` | Audio (MP3): upload via files/upload first | `["https://…"]`, up to 3 |
| `video_mode` | Task type for your video: `auto` (default) — the model works it out from the description, `reference` — reference, `edit` — edit (length and aspect ratio follow your video), `extend` — extend. Omitted = `auto` | **`auto`** · `reference` · `edit` · `extend` |
| `resolution` | Video resolution | **`720p`** · `1080p` |
| `aspect_ratio` | Aspect ratio | **`16:9`** · `9:16` · `1:1` · `21:9` · `auto` |
| `duration` | Video duration in seconds | 3 to 30 (default **`5`**) |
| `audio_setting` | Sound | **`auto`** `origin` |
| `trim_video_to` | Trim each of your clips to N seconds: the beginning stays. Shorter clips are not changed | whole number, 2 to 30 |
| `trim_to_duration` | Trim each of your clips to the chosen `duration`: the beginning stays. With `duration: "auto"` or `video_mode: "edit"` — to the model limit | `true` `false` (default `false`) |
| `auto_trim` | If your clips together are longer than 30 s, trim them instead of returning an error (the longest first) | `true` `false` (default `false`) |

## Examples

### Prompt only

```bash
curl -X POST https://mixmedia.tech/api/v1/jobs/createTask \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"kling-4.0","input":{"prompt":"A drone flies over an autumn forest at sunrise, fog, cinematic","resolution":"720p","aspect_ratio":"16:9","duration":"5"}}'
```

```python
import os, requests

r = requests.post(
    "https://mixmedia.tech/api/v1/jobs/createTask",
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    json={
      "model": "kling-4.0",
      "input": {
        "prompt": "A drone flies over an autumn forest at sunrise, fog, cinematic",
        "resolution": "720p",
        "aspect_ratio": "16:9",
        "duration": "5"
      }
    },
)
print(r.json())  # {"code": 200, "data": {"taskId": "..."}}
```

```javascript
const r = await fetch("https://mixmedia.tech/api/v1/jobs/createTask", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    "model": "kling-4.0",
    "input": {
      "prompt": "A drone flies over an autumn forest at sunrise, fog, cinematic",
      "resolution": "720p",
      "aspect_ratio": "16:9",
      "duration": "5"
    }
  }),
});
console.log(await r.json()); // { code: 200, data: { taskId: "..." } }
```

### With a first frame

```bash
curl -X POST https://mixmedia.tech/api/v1/jobs/createTask \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"kling-4.0","input":{"prompt":"The character slowly turns to the camera, the wind moves the hair","first_frame_url":"https://mixmedia.tech/static/img/previews/gpt-t2i.jpg","resolution":"720p","duration":"5"}}'
```

```python
import os, requests

r = requests.post(
    "https://mixmedia.tech/api/v1/jobs/createTask",
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    json={
      "model": "kling-4.0",
      "input": {
        "prompt": "The character slowly turns to the camera, the wind moves the hair",
        "first_frame_url": "https://mixmedia.tech/static/img/previews/gpt-t2i.jpg",
        "resolution": "720p",
        "duration": "5"
      }
    },
)
print(r.json())  # {"code": 200, "data": {"taskId": "..."}}
```

```javascript
const r = await fetch("https://mixmedia.tech/api/v1/jobs/createTask", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    "model": "kling-4.0",
    "input": {
      "prompt": "The character slowly turns to the camera, the wind moves the hair",
      "first_frame_url": "https://mixmedia.tech/static/img/previews/gpt-t2i.jpg",
      "resolution": "720p",
      "duration": "5"
    }
  }),
});
console.log(await r.json()); // { code: 200, data: { taskId: "..." } }
```

### With references

```bash
curl -X POST https://mixmedia.tech/api/v1/jobs/createTask \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"kling-4.0","input":{"prompt":"A drone flies over an autumn forest at sunrise, fog, cinematic","image_urls":["https://mixmedia.tech/static/img/previews/gpt-t2i.jpg"],"resolution":"720p","aspect_ratio":"16:9","duration":"5","audio_setting":"auto"}}'
```

```python
import os, requests

r = requests.post(
    "https://mixmedia.tech/api/v1/jobs/createTask",
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    json={
      "model": "kling-4.0",
      "input": {
        "prompt": "A drone flies over an autumn forest at sunrise, fog, cinematic",
        "image_urls": [
          "https://mixmedia.tech/static/img/previews/gpt-t2i.jpg"
        ],
        "resolution": "720p",
        "aspect_ratio": "16:9",
        "duration": "5",
        "audio_setting": "auto"
      }
    },
)
print(r.json())  # {"code": 200, "data": {"taskId": "..."}}
```

```javascript
const r = await fetch("https://mixmedia.tech/api/v1/jobs/createTask", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    "model": "kling-4.0",
    "input": {
      "prompt": "A drone flies over an autumn forest at sunrise, fog, cinematic",
      "image_urls": [
        "https://mixmedia.tech/static/img/previews/gpt-t2i.jpg"
      ],
      "resolution": "720p",
      "aspect_ratio": "16:9",
      "duration": "5",
      "audio_setting": "auto"
    }
  }),
});
console.log(await r.json()); // { code: 200, data: { taskId: "..." } }
```

### With your video

```bash
curl -X POST https://mixmedia.tech/api/v1/jobs/createTask \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"kling-4.0","input":{"prompt":"Make the clip anime style, keep everything else","video_urls":["https://mixmedia.tech/files/<id>.mp4"],"video_mode":"edit","resolution":"720p","duration":"5","audio_setting":"auto"}}'
```

```python
import os, requests

r = requests.post(
    "https://mixmedia.tech/api/v1/jobs/createTask",
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    json={
      "model": "kling-4.0",
      "input": {
        "prompt": "Make the clip anime style, keep everything else",
        "video_urls": [
          "https://mixmedia.tech/files/<id>.mp4"
        ],
        "video_mode": "edit",
        "resolution": "720p",
        "duration": "5",
        "audio_setting": "auto"
      }
    },
)
print(r.json())  # {"code": 200, "data": {"taskId": "..."}}
```

```javascript
const r = await fetch("https://mixmedia.tech/api/v1/jobs/createTask", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    "model": "kling-4.0",
    "input": {
      "prompt": "Make the clip anime style, keep everything else",
      "video_urls": [
        "https://mixmedia.tech/files/<id>.mp4"
      ],
      "video_mode": "edit",
      "resolution": "720p",
      "duration": "5",
      "audio_setting": "auto"
    }
  }),
});
console.log(await r.json()); // { code: 200, data: { taskId: "..." } }
```

## Trim your video

Your clips together: up to 30 s. No need to cut a long clip beforehand:

- `trim_to_duration: true` — trim each clip to the chosen `duration`;
- `trim_video_to: N` — keep N seconds from the start;
- `auto_trim: true` — if the clips together are over the limit, cut them exactly to it instead of an error.

Trimming is free, and the price uses the trimmed seconds — fewer seconds cost less.

```bash
curl -X POST https://mixmedia.tech/api/v1/jobs/createTask \
  -H "Authorization: Bearer $API_KEY" \
  -H "Content-Type: application/json" \
  -d '{"model":"kling-4.0","input":{"prompt":"Make the clip anime style, keep everything else","video_urls":["https://mixmedia.tech/files/<id>.mp4"],"video_mode":"edit","resolution":"720p","duration":"5","audio_setting":"auto","trim_to_duration":true,"auto_trim":true}}'
```

```python
import os, requests

r = requests.post(
    "https://mixmedia.tech/api/v1/jobs/createTask",
    headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
    json={
      "model": "kling-4.0",
      "input": {
        "prompt": "Make the clip anime style, keep everything else",
        "video_urls": [
          "https://mixmedia.tech/files/<id>.mp4"
        ],
        "video_mode": "edit",
        "resolution": "720p",
        "duration": "5",
        "audio_setting": "auto",
        "trim_to_duration": True,
        "auto_trim": True
      }
    },
)
print(r.json())  # {"code": 200, "data": {"taskId": "..."}}
```

```javascript
const r = await fetch("https://mixmedia.tech/api/v1/jobs/createTask", {
  method: "POST",
  headers: { Authorization: `Bearer ${process.env.API_KEY}`, "Content-Type": "application/json" },
  body: JSON.stringify({
    "model": "kling-4.0",
    "input": {
      "prompt": "Make the clip anime style, keep everything else",
      "video_urls": [
        "https://mixmedia.tech/files/<id>.mp4"
      ],
      "video_mode": "edit",
      "resolution": "720p",
      "duration": "5",
      "audio_setting": "auto",
      "trim_to_duration": true,
      "auto_trim": true
    }
  }),
});
console.log(await r.json()); // { code: 200, data: { taskId: "..." } }
```

One uploaded file: up to 30 s. Without these options, clips that are too long return a `400` error with a hint.

## Create and result

1. `POST /api/v1/jobs/createTask` with `model` and `input`.
2. Poll `GET /api/v1/jobs/recordInfo?taskId=…` every 3–5 s (or a [callback](https://docs.mixmedia.tech/en/callbacks)).

### Successful createTask (HTTP 200)

```json
{
  "code": 200,
  "msg": "success",
  "data": {
    "taskId": "task_...",
    "task_id": "task_...",
    "status": "submitted"
  }
}
```

### Completed task (recordInfo)

```json
{
  "code": 200,
  "msg": "success",
  "data": {
    "taskId": "task_...",
    "task_id": "task_...",
    "state": "success",
    "status": "completed",
    "costUsd": "0.384",
    "refunded": false,
    "resultJson": {
      "videos": [
        {
          "url": "https://…/out.mp4"
        }
      ],
      "duration": 8
    }
  }
}
```

Aliases: `task_id` = `taskId`; `status`: `pending` / `processing` / `completed` / `failed` alongside `state`. Clip URL is in `resultJson`. `size` is an alias of `aspect_ratio` in `input`.


Photos can be any public https links or uploads via [files/upload](https://docs.mixmedia.tech/en/endpoints/upload). Videos and audio — via files/upload only.

## Error handling

API responses are JSON `{"code", "msg", "data"}`; errors also have a string `error` field (type). **On `/api/v1/*` the HTTP status matches body `code`.**

### Example response bodies

**400**

```json
{
  "code": 400,
  "msg": "input.duration: must be between 4 and 30, or \"auto\" / -1",
  "data": null,
  "error": "invalid_request_error"
}
```

**401**

```json
{
  "code": 401,
  "msg": "Authentication failed. Please check your API key",
  "data": null,
  "error": "authentication_error"
}
```

**402**

```json
{
  "code": 402,
  "msg": "Insufficient balance. Please top up and try again. Available: $0.01, required: $0.015. Top up at https://mixmedia.tech/cabinet/billing",
  "data": {
    "balance": "0.0100",
    "required": "0.0150",
    "currency": "USD",
    "topUpUrl": "https://mixmedia.tech/cabinet/billing"
  },
  "error": "payment_required"
}
```

**429**

```json
{
  "code": 429,
  "msg": "Rate limit exceeded: 10 createTask/upload requests per 10 s per key. Retry after 8s",
  "data": null,
  "error": "rate_limit_error"
}
```

**500**

```json
{
  "code": 500,
  "msg": "Internal server error",
  "data": null,
  "error": "server_error"
}
```

### Codes on createTask / upload

| code | error | Meaning | What to do |
|---|---|---|---|
| 400 | `invalid_request_error` | Invalid parameter or body | See `msg` |
| 400 | `unsupported_model` | Unknown model | [Model list](https://docs.mixmedia.tech/en/models) |
| 400 | `nsfw_content_detected` | Moderation / content policy — `msg` / `failMsg` is the upstream text verbatim | Read the refusal text, change the prompt or files |
| 401 | `authentication_error` | Missing or invalid key | Check the key |
| 402 | `payment_required` | Not enough balance | Top up |
| 403 | `permission_error` | No access | Contact support |
| 429 | `rate_limit_error` | Too many requests | Wait for `Retry-After` |
| 505 | `model_disabled` | Model temporarily disabled | Pick another |
| 500 | `server_error` | Our failure | Retry later |

Example `msg` values: `Authentication failed. Please check your API key` (401); `Insufficient balance. Please top up and try again…` (402).

### Task errors (recordInfo)

If the task was created and then failed: `state: "fail"` (alias `status: "failed"`), `failCode` / `failMsg`, and `error: { code, message, type }`. Money is refunded (`refunded: true`).

| failCode | error.type | Meaning |
|---|---|---|
| 400 | `nsfw_content_detected` / `invalid_request_error` | Moderation — `failMsg` is upstream text verbatim |
| 422 | `invalid_request_error` | The model rejected the parameters (length, mode, resolution, file) |
| 501 | `server_error` | The model could not generate (`failMsg`) |
| 504 | `timeout_error` | Timed out — retry |
| 500 / 503 | `server_error` | Service temporarily unavailable |

### Rate limits

| What | Limit |
|---|---|
| createTask and upload | 20 per 10 s per key |
| recordInfo / list / balance | 100 per 10 s per key |

Over the limit — HTTP 429 and `Retry-After`.
