Skip to Content
Log in

Kling 4.0

Generate a 3–30 second video from text, frames or reference media.

kuaishou/kling-4.0KuaishouVideo~822s

Overview

Generate a 3–30 second video from text, frames or reference media.

  • Model ID: kuaishou/kling-4.0
  • Maker: Kuaishou
  • Type: video
  • Timeout: 43200 s
  • Typical time: ~822 s
  • Endpoint: POST https://api.kerox.ai/v1/tasks

Request example

Submit a task and get its id at once; get the result by polling (below) or by webhook. webhook_url and metadata are optional. To use a webhook, provide your own public HTTPS address in webhook_url. Every field is in Submit a task.

Set KEROX_API_KEY first. The polling example needs Bash, curl and jq. For Python, install requests with pip install requests.

curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "a red fox running through fresh snow, cinematic lighting",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9"
  }
}'
Reference image
curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "Show the red paper sailboat under warm copper lights",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9",
    "images": [
      "https://cdn.kerox.ai/assets/samples/kling-4.0/paper-sail-frame.jpg"
    ]
  }
}'
First frame
curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "Track the red paper sailboat as it drifts across the pool",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9",
    "first_frame": "https://cdn.kerox.ai/assets/samples/kling-4.0/paper-sail-frame.jpg"
  }
}'
First and last frames
curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "Circle the red paper sailboat and return to the starting view",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9",
    "first_frame": "https://cdn.kerox.ai/assets/samples/kling-4.0/paper-sail-frame.jpg",
    "last_frame": "https://cdn.kerox.ai/assets/samples/kling-4.0/paper-sail-frame.jpg"
  }
}'
Reference video
curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "Use the camera motion of Video 1 for a flyover of a rocky coastline",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9",
    "videos": [
      "https://cdn.kerox.ai/assets/samples/kling-4.0-flash/motion-reference.mp4"
    ]
  }
}'
Reference audio
curl --fail-with-body -X POST https://api.kerox.ai/v1/tasks \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "Show a dinosaur at dusk and time its roar to Audio 1",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9",
    "audios": [
      "https://interactive-examples.mdn.mozilla.net/media/cc0-audio/t-rex-roar.mp3"
    ]
  }
}'

Parameters

The parameter names mean the same on every model.

ParameterDescriptionValues and example
promptstring · RequiredText describing what to generate.Default · UnspecifiedValues at least one non-whitespace character; at least 1 characters
Example "A lighthouse at dawn"
keep_original_soundboolean · OptionalUse the sound from the reference video. Requires videos.Default · UnspecifiedValues true, false
Example true
imagesstring | string[] · OptionalImages for the model to draw on, in the order the prompt refers to them.Default · UnspecifiedValues one public HTTPS URL; one string or an array of 1–10 items
Example "https://example.com/photo.png"
videosstring | string[] · OptionalVideos for the model to draw on, in the order the prompt refers to them.Default · UnspecifiedValues one public HTTPS URL; one string or an array of 1–5 items; Model file limits: Up to five MP4 or MOV files, at most 30 seconds in total. Duration must be readable. Total reference seconds are rounded up.
Example "https://example.com/clip.mp4"
audiosstring | string[] · OptionalAudio clips for the model to draw on, in the order the prompt refers to them.Default · UnspecifiedValues one public HTTPS URL; one string or an array of 1–3 items; Model file limits: Up to three clips of 5–30 seconds each.
Example "https://example.com/music.wav"
first_framestring · OptionalThe image a video starts from.Default · UnspecifiedValues one public HTTPS URL; Model file limits: JPEG or PNG. An end frame requires a first frame. Frames cannot be combined with reference media.
Example "https://example.com/start.png"
last_framestring · OptionalThe image a video ends on. Needs first_frame.Default · UnspecifiedValues one public HTTPS URL; Model file limits: JPEG or PNG. An end frame requires a first frame. Frames cannot be combined with reference media.
Example "https://example.com/end.png"
aspect_ratiostring · OptionalWidth to height of the output.Default · UnspecifiedValues 16:9, 9:16, 1:1, 21:9, auto
Example "16:9"
resolutionstring · OptionalOutput resolution tier. The exact pixels follow from it and aspect_ratio.Default · "720p"Values 720p, 1080p, 4K
Example "720p"
durationinteger · OptionalLength of the output in whole seconds.Default · 5Values 3–30
Example 5
ninteger · OptionalHow many outputs one task may produce.Default · 1Values 1
Example 1
  • last_frame requires first_frame
  • keep_original_sound requires videos
  • When first_frame: omit images or videos or audios
  • When aspect_ratio = "auto": supply images or videos or audios

Use a public HTTPS URL directly for images, audio and video. For local media, upload with POST /v1/files and pass the returned url. For upload MIME and size limits, see Files & inputs.

Not supported yet: watermark.

Polling and output

Poll GET /v1/tasks/{id}, or pass webhook_url when you submit and receive the same task object when it finishes: see Webhooks for the setup. Output files expire at expires_at (7 days after completion), so download anything you want to keep. See every task field in Tasks & polling.

Set TASK_ID to the id returned on submission. The script waits up to 10 minutes, prints error on failure and downloads every outputs[].url on success.

(
  set -eu
  # Set TASK_ID to the id returned by the submit request.
  : "${TASK_ID:?Set TASK_ID}"
  deadline=$((SECONDS + 600))
  while (( SECONDS < deadline )); do
    task=$(curl --fail-with-body --silent --show-error --max-time "$((deadline - SECONDS))" https://api.kerox.ai/v1/tasks/"$TASK_ID" \
      -H "Authorization: Bearer $KEROX_API_KEY")
    case $(jq -er .status <<< "$task") in
      succeeded)
        if ! jq -e '.outputs | length > 0' <<< "$task" >/dev/null; then
          echo "Result files are unavailable." >&2
          exit 1
        fi
        i=0
        while IFS= read -r url; do
          file_path=${url%%\?*}
          filename="output-${i}.${file_path##*.}"
          curl --fail-with-body --silent --show-error --max-time 120 "$url" -o "$filename"
          printf "%s\n" "$filename"
          i=$((i + 1))
        done < <(jq -r '.outputs[].url' <<< "$task")
        exit 0 ;;
      failed) jq -c .error <<< "$task" >&2; exit 1 ;;
      processing) sleep 2 ;;
      *) echo "Unexpected task status." >&2; exit 1 ;;
    esac
  done
  echo "Poll timed out. Keep TASK_ID to check again later." >&2
  exit 1
)

Pricing

TierOfficial / secondKerox / secondBelow official price
4K$0.5490$0.494110%
720p$0.1570$0.141310%
1080p$0.2350$0.211510%
  • With a reference video, bill output seconds + max(output seconds, total reference seconds) at 4K $0.2961/s (official $0.3290); 720p $0.0846/s (official $0.0940); 1080p $0.1269/s (official $0.1410).

Estimate before submitting

The returned amount is what submitting would hold. An estimate creates no task and charges nothing. When exact is false, the final charge may differ from the hold.

curl --fail-with-body -X POST https://api.kerox.ai/v1/estimate \
  -H "Authorization: Bearer $KEROX_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
  "model": "kuaishou/kling-4.0",
  "input": {
    "prompt": "a red fox running through fresh snow, cinematic lighting",
    "resolution": "720p",
    "duration": 5,
    "aspect_ratio": "16:9"
  }
}'

Input schema (JSON)

The full JSON Schema, nested parameters included
{
  "type": "object",
  "allOf": [
    {
      "if": {
        "required": [
          "first_frame"
        ],
        "properties": {
          "first_frame": true
        }
      },
      "then": {
        "not": {
          "anyOf": [
            {
              "required": [
                "images"
              ],
              "properties": {
                "images": true
              }
            },
            {
              "required": [
                "videos"
              ],
              "properties": {
                "videos": true
              }
            },
            {
              "required": [
                "audios"
              ],
              "properties": {
                "audios": true
              }
            }
          ]
        }
      }
    },
    {
      "if": {
        "required": [
          "aspect_ratio"
        ],
        "properties": {
          "aspect_ratio": {
            "const": "auto"
          }
        }
      },
      "then": {
        "anyOf": [
          {
            "required": [
              "images"
            ],
            "properties": {
              "images": true
            }
          },
          {
            "required": [
              "videos"
            ],
            "properties": {
              "videos": true
            }
          },
          {
            "required": [
              "audios"
            ],
            "properties": {
              "audios": true
            }
          }
        ]
      }
    }
  ],
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "required": [
    "prompt"
  ],
  "properties": {
    "prompt": {
      "type": "string",
      "pattern": "\\S",
      "minLength": 1,
      "description": "Text describing what to generate.",
      "x-label-en": "Prompt",
      "x-label-zh": "提示词",
      "x-tier": "common"
    },
    "keep_original_sound": {
      "type": "boolean",
      "description": "Use the sound from the reference video. Requires videos.",
      "x-label-en": "Keep original sound",
      "x-label-zh": "保留参考视频原声",
      "x-tier": "advanced"
    },
    "images": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^https://"
        },
        {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^https://"
          },
          "maxItems": 10,
          "minItems": 1
        }
      ],
      "description": "Images for the model to draw on, in the order the prompt refers to them.",
      "x-label-en": "Reference images",
      "x-label-zh": "参考图",
      "x-tier": "common"
    },
    "videos": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^https://"
        },
        {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^https://"
          },
          "maxItems": 5,
          "minItems": 1
        }
      ],
      "description": "Videos for the model to draw on, in the order the prompt refers to them.",
      "x-label-en": "Reference videos",
      "x-label-zh": "参考视频",
      "x-tier": "advanced"
    },
    "audios": {
      "oneOf": [
        {
          "type": "string",
          "pattern": "^https://"
        },
        {
          "type": "array",
          "items": {
            "type": "string",
            "pattern": "^https://"
          },
          "maxItems": 3,
          "minItems": 1
        }
      ],
      "description": "Audio clips for the model to draw on, in the order the prompt refers to them.",
      "x-label-en": "Reference audio",
      "x-label-zh": "参考音频",
      "x-tier": "advanced"
    },
    "first_frame": {
      "type": "string",
      "pattern": "^https://",
      "description": "The image a video starts from.",
      "x-label-en": "First frame",
      "x-label-zh": "首帧",
      "x-tier": "common"
    },
    "last_frame": {
      "type": "string",
      "pattern": "^https://",
      "description": "The image a video ends on. Needs `first_frame`.",
      "x-label-en": "Last frame",
      "x-label-zh": "尾帧",
      "x-tier": "advanced"
    },
    "aspect_ratio": {
      "enum": [
        "16:9",
        "9:16",
        "1:1",
        "21:9",
        "auto"
      ],
      "type": "string",
      "description": "Width to height of the output.",
      "x-label-en": "Aspect ratio",
      "x-label-zh": "画面比例",
      "x-tier": "common"
    },
    "resolution": {
      "enum": [
        "720p",
        "1080p",
        "4K"
      ],
      "type": "string",
      "default": "720p",
      "description": "Output resolution tier. The exact pixels follow from it and `aspect_ratio`.",
      "x-label-en": "Resolution",
      "x-label-zh": "分辨率",
      "x-tier": "common"
    },
    "duration": {
      "type": "integer",
      "default": 5,
      "maximum": 30,
      "minimum": 3,
      "description": "Length of the output in whole seconds.",
      "x-label-en": "Duration",
      "x-label-zh": "时长",
      "x-tier": "common"
    },
    "n": {
      "type": "integer",
      "const": 1,
      "default": 1,
      "description": "How many outputs one task may produce.",
      "x-label-en": "Number of outputs",
      "x-label-zh": "生成数量",
      "x-tier": "advanced"
    }
  },
  "x-unsupported": [
    "watermark"
  ],
  "dependentRequired": {
    "last_frame": [
      "first_frame"
    ],
    "keep_original_sound": [
      "videos"
    ]
  },
  "additionalProperties": false
}