Kling Motion Generation API Integration Instructions

This article will introduce the integration instructions for the Kling Motion Generation API, which can generate official Kling videos by inputting custom parameters.

Application Process

To use the Kling Motion Generation API, first go to the 辰汐ai Console to obtain your API Token for future use.

If you are not logged in or registered, you will be automatically redirected to the login page to invite you to register and log in, and after completing this, you will be automatically returned to the current page.

One API Token can call all services on the platform without needing to apply separately for each service. The first application will grant a free quota for a trial experience; when the quota is insufficient, you can recharge the general balance in the console.

📘 Complete Documentation: Kling Motion Generation API →

Basic Usage

First, understand the basic usage method, which is to input the prompt prompt, reference image image_url, and reference video link video_url to obtain the processed result. We also need to input the model mode, which currently mainly includes std and pro 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 selected directly after application.

Additionally, we have set the Request Body, including:

  • image_url: URL of the reference image for character appearance. Supports JPG/JPEG/PNG, file ≤50MB, width and height ≥300px, aspect ratio 1:2.5 to 2.5:1; the character should clearly show the upper body or full body and head.
  • video_url: URL of the reference action video. Supports MP4/MOV, file ≤100MB, width and height each 340–3850px, at least 3 seconds; when character_orientation=image, the maximum is 10 seconds, and when character_orientation=video, the maximum is 30 seconds. It is recommended to use a continuous single-shot video where the character is always within the frame.
  • mode: the mode for generating the video, mainly including standard mode std and high-speed mode pro.
  • keep_original_sound: optional to choose whether to keep the original sound of the video, enumeration values: yes, no.
  • character_orientation: the orientation of the character in the generated video, which can be chosen to be consistent with the image or the video, enumeration values: image, video.
  • prompt: the prompt.
  • callback_url: the URL to receive the callback result.
  • async: optional, when set to true, the interface immediately returns task_id, and there is no need to provide callback_url, then the result can be polled through the corresponding task query interface.

After selection, you can see that the corresponding code is generated on the right side, as shown in the figure:

Click the "Try" button to test, as shown in the above figure, we obtained the following result:

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

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 access the generated Kling video using the video link address in the data of the result.

Additionally, if you want to generate the corresponding integration code, you can directly copy it, 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": "Bring the scene to life",
  "mode": "std",
  "character_orientation": "image"
}'

Asynchronous Callback

Since the time taken by the Kling Motion Generation API is relatively long, 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 information, 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, we use a public Webhook sample site https://webhook.site/, where you can open the site to get a Webhook URL, as shown in the figure:

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, while filling in the corresponding parameters, as shown in the figure:

Clicking run, you can find that you will immediately receive a result, 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 figure:

The content is as follows:

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

You can see that the result contains a task_id field, and the other fields are similar to the above text. 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 the action control features of Kling official. We hope this document can help you better integrate and use this API. If you have any questions, please feel free to contact our technical support team.