Skip to main content

Introduction

Kling is an AI video service provided by Ace Data Cloud, featuring Kuaishou’s flexible video generation model. Through the unified API of Ace Data Cloud, you can quickly integrate Kling with Python to achieve text-to-video, image-to-video, video continuation, various durations, multiple aspect ratios, and more.

Prerequisites

  • Have an Ace Data Cloud account and obtain an API Token
  • Python 3.7 or above environment
  • Install the requests library: pip install requests

Basic Usage

The main endpoint to call the Kling API is:
POST https://api.acedata.cloud/kling/videos
This example uses the kling-v2-5-turbo model.
Available models include: kling-v1, kling-v1-6, kling-v2-master, kling-v2-1-master, kling-v2-5-turbo, kling-video-o1.
Complete Python code example:
import requests

url = "https://api.acedata.cloud/kling/videos"
headers = {
    "Authorization": "Bearer YOUR_API_TOKEN",
    "Content-Type": "application/json"
}
data = {
    "action": "text2video",
    "prompt": "A white cat walking in the snow",
    "model": "kling-v2-5-turbo",
    "duration": 5,
    "aspect_ratio": "16:9"
}

response = requests.post(url, headers=headers, json=data)
result = response.json()
print(result)
Please replace YOUR_API_TOKEN with the actual Token you obtained from the Ace Data Cloud platform.

Response Handling

After a successful call, the API returns data in JSON format. It is recommended to check the HTTP status code:
if response.status_code == 200:
    result = response.json()
    print("Call succeeded:", result)
else:
    print(f"Call failed, status code: {response.status_code}")
    print(response.text)

Advanced Usage

For APIs that support asynchronous tasks, you can get results via a callback URL:
data['callback_url'] = 'https://your-server.com/callback'
response = requests.post(url, headers=headers, json=data)
# The result will be pushed to your server via the callback URL

Error Handling

Common error codes:
Status CodeDescription
401Authentication failed, please check API Token
403Insufficient balance or no access permission
429Request rate limit exceeded
500Internal server error

Other Endpoints

Kling service also provides the following endpoints:
  • POST https://api.acedata.cloud/kling/tasks
  • POST https://api.acedata.cloud/kling/motion

Next Steps