Skip to content

KlingAI

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on official KlingAI OpenAPI specification using AutoSDK
  • Same day update to support new features
  • Updated and supported automatically if there are no breaking changes
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Supports .NET 10.0

Usage

1
2
3
using KlingAI;

using var client = new KlingAIClient(apiKey);

Text to Video

Shows how to create a text-to-video generation task and retrieve its status.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using var client = new KlingAIClient(apiKey);

// Create a text-to-video task using the Kling v2 model with a 5-second duration.
var createResponse = await client.CreateTextToVideoAsync(
    prompt: "A golden retriever running through a sunlit meadow, cinematic slow motion",
    modelName: CreateTextToVideoRequestModelName.KlingV2,
    mode: CreateTextToVideoRequestMode.Std,
    aspectRatio: CreateTextToVideoRequestAspectRatio.x16_9,
    duration: CreateTextToVideoRequestDuration.x5);

Console.WriteLine($"Task ID: {createResponse.Data.TaskId}");
Console.WriteLine($"Task Status: {createResponse.Data.TaskStatus}");

// Retrieve the task to check its status and get the result when complete.
var taskResponse = await client.GetTextToVideoTaskAsync(
    id: createResponse.Data.TaskId!);

Console.WriteLine($"Status: {taskResponse.Data.TaskStatus}");

// When the task succeeds, the result will contain video URLs.
if (taskResponse.Data.TaskStatus == TaskDataTaskStatus.Succeed)
{
    Console.WriteLine($"Video URL: {taskResponse.Data.TaskResult.Videos![0].Url}");
}

Image to Video

Shows how to create an image-to-video task from an image URL with prompt guidance.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using var client = new KlingAIClient(apiKey);

// Create an image-to-video task by providing an image URL and a motion prompt.
var createResponse = await client.CreateImageToVideoAsync(
    image: "https://example.com/landscape.jpg",
    prompt: "Camera slowly pans across the landscape with gentle wind blowing through the trees",
    modelName: CreateImageToVideoRequestModelName.KlingV2,
    mode: CreateImageToVideoRequestMode.Std,
    duration: CreateImageToVideoRequestDuration.x5);

Console.WriteLine($"Task ID: {createResponse.Data.TaskId}");

// Query the task status using the returned task ID.
var taskResponse = await client.GetImageToVideoTaskAsync(
    id: createResponse.Data.TaskId!);

Console.WriteLine($"Status: {taskResponse.Data.TaskStatus}");

// When complete, the result contains generated video URLs.
if (taskResponse.Data.TaskStatus == TaskDataTaskStatus.Succeed)
{
    Console.WriteLine($"Video URL: {taskResponse.Data.TaskResult.Videos![0].Url}");
    Console.WriteLine($"Duration: {taskResponse.Data.TaskResult.Videos[0].Duration}s");
}

Image Generation

Shows how to create an image generation task from a text prompt.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
using var client = new KlingAIClient(apiKey);

// Create an image generation task with a text prompt.
var createResponse = await client.CreateImageGenerationAsync(
    prompt: "A futuristic cityscape at sunset with flying vehicles, digital art",
    modelName: CreateImageGenerationRequestModelName.KlingV2,
    aspectRatio: CreateImageGenerationRequestAspectRatio.x16_9,
    n: 1);

Console.WriteLine($"Task ID: {createResponse.Data.TaskId}");
Console.WriteLine($"Task Status: {createResponse.Data.TaskStatus}");

// Retrieve the task to check the generation status and result.
var taskResponse = await client.GetImageGenerationTaskAsync(
    id: createResponse.Data.TaskId!);

Console.WriteLine($"Status: {taskResponse.Data.TaskStatus}");

// When the task succeeds, the result will contain image URLs.
if (taskResponse.Data.TaskStatus == TaskDataTaskStatus.Succeed)
{

    foreach (var image in taskResponse.Data.TaskResult.Images!)
    {
        Console.WriteLine($"Image {image.Index}: {image.Url}");
    }
}

Support

Priority place for bugs: https://github.com/tryAGI/KlingAI/issues
Priority place for ideas and general questions: https://github.com/tryAGI/KlingAI/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.