Skip to main content
This article will introduce the integration instructions for the Kling Videos Generation API, which can generate official Kling videos by inputting custom parameters.

Application Process

To use the API, you need to first apply for the corresponding service on the Kling Videos Generation API page. After entering the page, click the “Acquire” button, as shown in the image below: If you are not logged in or registered, you will be automatically redirected to the login page inviting you to register and log in. After logging in or registering, you will be automatically returned to the current page. Upon your first application, there will be a free quota available for you to use the API for free.

Basic Usage

First, understand the basic usage method, which involves inputting the prompt prompt, the action action, the starting frame reference image start_image_url, and the model model to obtain the processed result. You first need to simply pass a field action with the value text2video, which mainly includes three actions: text-to-video (text2video), image-to-video (image2video), and video extension (extend). Then, you also need to input the model model, which currently mainly includes kling-v1, kling-v1-6, kling-v2-master, kling-v2-1-master, kling-v2-5-turbo, and kling-video-o1 models, as detailed below:

Here we can see that we have set the Request Headers, including:
  • accept: the format of the response result you want to receive, filled in as application/json, which means JSON format.
  • authorization: the key to call the API, which can be directly selected after application.
Additionally, the Request Body is set, including:
  • model: the model for generating the video, mainly including kling-v1, kling-v1-6, kling-v2-master, kling-v2-1-master, kling-v2-5-turbo, and kling-video-o1 models.
  • mode: the mode for generating the video, mainly including standard mode std and fast mode pro.
  • action: the action for this video generation task, mainly including three actions: text-to-video (text2video), image-to-video (image2video), and video extension (extend).
  • start_image_url: when selecting the image-to-video action image2video, the starting frame reference image link must be uploaded.
  • end_image_url: optional for image-to-video, specifies the ending frame.
  • aspect_ratio: the aspect ratio of the video, optional, supports 16:9, 9:16, 1:1, default is 16:9.
  • cfg_scale: relevance strength, range [0,1], the larger the value, the more it fits the prompt.
  • camera_control: optional, controls the parameters of camera movement, supports type/simple presets as well as configurations like horizontal, vertical, pan, tilt, roll, zoom, etc.
  • negative_prompt: optional, reverse prompts that you do not want to appear, up to 200 characters.
  • element_list: subject reference list, applicable only to the kling-video-o1 model; refer to the official documentation for specific usage.
  • video_list: reference videos, obtained via URL, applicable only to the kling-video-o1 model; refer to the official documentation for specific usage.
  • prompt: the prompt.
  • callback_url: the URL for the callback result.
After selection, you can see that the corresponding code is also generated on the right side, as shown in the image below:

Click the “Try” button to test, as shown in the image above, and we get the following result:
{
  "success": true,
  "video_id": "af9a1af0-9aa0-4638-81c1-d41d6143c508",
  "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7485378259/text2video/Cjil4mfBfs0AAAAAAKbMQQ-0_raw_video_1.mp4",
  "duration": "5.1",
  "state": "succeed",
  "task_id": "e3a575aa-a4bd-49c8-9b12-cde38d5462e0"
}
The returned result contains multiple fields, described as follows:
  • success: the status of the video generation task at this time.
  • task_id: the ID of the video generation task at this time.
  • video_id: the video ID of the video generation task at this time.
  • video_url: the video link of the video generation task at this time.
  • duration: the duration of the video generation task at this time.
  • state: the status of the video generation task at this time.
We can see that we have obtained satisfactory video information, and we only need to obtain the generated Kling video based on the video link address in the data result. Additionally, if you want to generate the corresponding integration code, you can directly copy the generated code, for example, the CURL code is as follows:
curl -X POST 'https://api.acedata.cloud/kling/videos' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "action": "text2video",
  "model": "kling-v1",
  "prompt": "White ceramic coffee mug on glossy marble countertop with morning window light. Camera slowly rotates 360 degrees around the mug, pausing briefly at the handle."
}'

Video Extension Functionality

If you want to continue generating an already created Kling video, you can set the parameter action to extend and input the ID of the video you want to continue generating. The video ID can be obtained based on the basic usage, as shown in the image below:

At this point, you can see that the video ID is:
"video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c"
Note that the video_id here is the ID of the generated video. If you do not know how to generate a video, you can refer to the basic usage above to generate a video.
Next, you must fill in the prompt for the next step to customize the video generation, and you can specify the following content:
  • model: The model for generating videos, mainly including kling-v1, kling-v1-5, and kling-v1-6.
  • mode: The mode for generating videos, mainly including standard mode std and high-speed mode pro.
  • duration: The duration of the video generation task, mainly including 5s and 10s.
  • start_image_url: The link to the reference image for the first frame that must be uploaded when selecting the image-to-video action image2video.
  • prompt: The prompt words.
An example of filling out is as follows:

After filling it out, the generated code is as follows:

The corresponding Python code:
import requests

url = "https://api.acedata.cloud/kling/videos"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "action": "extend",
    "model": "kling-v1",
    "video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c",
    "prompt": "White ceramic coffee mug on glossy marble countertop with morning window light. Camera slowly rotates 360 degrees around the mug, pausing briefly at the handle.",
    "duration": 10
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)
By clicking run, you can find that a result is obtained, as follows:
{
  "success": true,
  "video_id": "bbc3b105-ac72-4de2-8390-0cb37dc7d41e",
  "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7822108635/extendVideo/Cjil4mfBfs0AAAAAAKhr6A-0_raw_video_1.mp4",
  "duration": "9.6",
  "state": "succeed",
  "task_id": "3ece87e6-3ee3-4f5e-bd70-5ae5eca89a23"
}
It can be seen that the result content is consistent with the above text, thus achieving the video extension function.

Asynchronous Callback

Since the Kling Videos Generation API takes a relatively long time to generate, approximately 1-2 minutes, if the API does not respond for a long time, the HTTP request will keep the connection open, leading to additional system resource consumption. Therefore, this API also provides support for asynchronous callbacks. The overall process is: when the client initiates a request, an additional callback_url field is specified. After the client initiates the API request, the API will immediately return a result containing a task_id field, representing the current task ID. When the task is completed, the result of the generated video will be sent to the client-specified callback_url in the form of a POST JSON, which also includes the task_id field, allowing the task result to be associated by ID. Let’s understand how to operate specifically through an example. First, the Webhook callback is a service that can receive HTTP requests, and developers should replace it with the URL of their own HTTP server. For demonstration purposes, a public Webhook sample site https://webhook.site/ is used, and opening this site will provide a Webhook URL, as shown in the image: Copy this URL, and it can be used as a Webhook. The sample here is https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3. Next, we can set the callback_url field to the above Webhook URL and fill in the corresponding parameters, as shown in the image:

By clicking run, you can find that an immediate result is obtained, as follows:
{
  "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
}
After a moment, we can observe the result of the generated video at https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3, as shown in the image: The content is as follows:
{
    "success": true,
    "video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c",
    "video_url": "https://cdn.klingai.com/bs2/upload-kling-api/7822108635/text2video/CjJzzGfBfqcAAAAAAKdVMQ-0_raw_video_1.mp4",
    "duration": "5.1",
    "state": "succeed",
    "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
}
It can be seen that the result contains a task_id field, and the other fields are similar to the above text, allowing the task to be associated through this field.

Error Handling

When calling the API, if an error occurs, the API will return the corresponding error code and message. For example:
  • 400 token_mismatched: Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented: Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token: Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests: Too many requests, you have exceeded the rate limit.
  • 500 api_error: Internal server error, something went wrong on the server.

Error Response Example

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

Conclusion

Through this document, you have learned how to use the Kling Videos Generation API to generate videos by inputting prompt words and a reference image for the first frame. It is hoped that this document will help you better integrate and use this API. If you have any questions, please feel free to contact our technical support team.