Kling Motion Generation API Integration Guide

This article introduces how to integrate with the Kling Motion Generation API, which can generate official Kling videos by entering custom parameters.

Application Process

To use the Kling Motion Generation API, first go to the Ace Data Cloud Console to obtain your API Token for later use.

If you have not logged in or registered, you will be automatically redirected to the login page to register and log in. Once completed, you will automatically return to the current page.

The API Key for a general application can be used for services enabled for the account; the Key for a dedicated application can be used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is shown in your account. If the balance is insufficient, click the "Recharge" button.

📘 Full documentation: Kling Motion Generation API →

Basic Usage

First, let's look at the basic usage: enter a prompt, a reference image image_url, and a reference video link video_url to obtain the processed result. We also need to enter the model mode, which currently has two main options, std and pro. The details are as follows:

You can see that we have set the Request Headers here, including:

  • accept: The format in which you want to receive the response. Enter application/json here, which means JSON format.
  • authorization: The key for calling the API. After applying for it, you can select it directly from the dropdown.

We have also set the Request Body, including:

  • image_url: The URL of the character appearance reference image. Supports JPG/JPEG/PNG, file size ≤50MB, width and height both ≥300px, and aspect ratio 1:2.5–2.5:1; the character's upper body or full body and head should be clearly visible.
  • video_url: The URL of the motion reference video. Supports MP4/MOV, file size ≤100MB, width and height each 340–3850px, and at least 3 seconds long; the maximum length is 10 seconds when character_orientation=image and 30 seconds when character_orientation=video. A continuous single-shot video in which the character remains in the frame is recommended.
  • mode: The mode for generating the video, mainly the standard mode std and the high-speed mode pro.
  • keep_original_sound: Whether to retain the video's original sound. Enum values: yes, no.
  • character_orientation: The character's orientation in the generated video, which can match the image or the video. Enum values: image, video.
  • prompt: The prompt.
  • callback_url: The URL to which the result should be sent.
  • async: Optional. When set to true, the API immediately returns a task_id without requiring a callback_url. The result can then be obtained by polling the corresponding task query API.

After making the selections, you can see that the corresponding code has also been generated on the right, as shown below:

Click the "Try" button to test it. As shown above, we obtained the following result:

{
  "success": true,
  "video_id": "842578800134742051",
  "video_url": "https://cdn.acedata.cloud/assets/examples/gemini/04a043bd-6b23-4b4e-945c-ce48158c3eee-3a89912507c7.mp4",
  "duration": "5.066",
  "state": "succeed",
  "task_id": "363c7a84-e880-472e-a4d4-098e50cfc292"
}

The returned result contains several fields, described as follows:

  • success, the status of the video generation task.
  • task_id, the ID of the video generation task.
  • video_id, the video ID of the video generation task.
  • video_url, the video link of the video generation task.
  • duration, the duration of the video for the video generation task.
  • state, the status of the video generation task.

You can see that we have obtained satisfactory video information. We only need to use the video link in data in the result to retrieve the generated Kling video.

Additionally, if you want to generate the corresponding integration code, you can copy the generated code directly. For example, the CURL code is as follows:

curl -X POST 'https://api.acedata.cloud/kling/motion' \
-H 'accept: application/json' \
-H 'authorization: Bearer {token}' \
-H 'content-type: application/json' \
-d '{
  "image_url": "https://cdn.acedata.cloud/e724d7f13d.png",
  "video_url": "https://cdn.acedata.cloud/odwfm5.mp4",
  "prompt": "让画面生动起来",
  "mode": "std",
  "character_orientation": "image"
}'

Asynchronous Callback

Since the Kling Motion Generation API takes a relatively long time to generate a video, approximately 1–2 minutes, the HTTP request will remain connected if the API does not respond for a long time, leading to additional system resource consumption. Therefore, this API also supports asynchronous callbacks.

The overall process is as follows: when the client initiates a request, it additionally specifies a callback_url field. After the client initiates the API request, the API immediately returns a result containing a task_id field, which represents the current task ID. When the task is complete, the generated video result is sent as POST JSON to the callback_url specified by the client. It also includes the task_id field, so the task result can be associated using the ID.

Below, we will use an example to see how this works.

First, a Webhook callback is a service that can receive HTTP requests. Developers should replace it with the URL of their own HTTP server. For convenience, this demonstration uses a public Webhook example website, https://webhook.site/. Open the website to obtain a Webhook URL, as shown below:

Copy this URL to use it as a Webhook. The example here is https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3.

Next, we can set the callback_url field to the Webhook URL above and enter the corresponding parameters, as shown below:

Click run, and you will see that a result is returned immediately, as follows:

{
  "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
}

After waiting a moment, we can see the generated video result at https://webhook.site/624b2c78-6dbd-4618-9d2b-b32eade6d8c3, as shown below:

The content is as follows:

{
    "success": true,
    "video_id": "030bb06d-98d4-4044-9042-0aa0822e8c8c",
    "video_url": "https://cdn.acedata.cloud/assets/examples/gemini/04a043bd-6b23-4b4e-945c-ce48158c3eee-3a89912507c7.mp4",
    "duration": "5.1",
    "state": "succeed",
    "task_id": "20068983-0cc9-4c6a-aeb6-9c6a3c668be0"
}

You can see that the result contains a task_id field. The other fields are similar to those above, and this field can be used to associate tasks.

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 Motion Generation API to implement Kling's official motion control functionality. We hope this document helps you better integrate and use the API. If you have any questions, please feel free to contact our technical support team.