Back to Blog

How to Generate Videos with Kling AI via API

Andrew Adams

Andrew Adams

·11 min read
How to Generate Videos with Kling AI via API

Kling AI has become one of the most capable video generation models available, producing cinematic clips with realistic motion, lighting, and camera control. If you want to integrate Kling video generation into your own applications or automation pipelines, Wireflow gives you a straightforward way to access Kling's API through a unified REST interface, letting you chain it with other AI models in a single workflow. This guide walks you through every step of calling Kling via API, from authentication to polling for finished video output.

What Is Kling AI and Why Use It via API?

Kling AI (developed by Kuaishou) is a text-to-video and image-to-video model that generates 5- to 10-second clips at up to 1080p resolution. It supports camera trajectory control, aspect ratio selection, and multiple quality tiers (standard and pro). Kling 2.5 and 3.0 introduced native audio generation and lip-sync capabilities, making it a strong fit for marketing content, product demos, and social media clips.

Using Kling through an API rather than the web interface unlocks several practical advantages:

  • Batch processing: generate dozens of videos from a spreadsheet of prompts without manual interaction
  • Pipeline integration: connect Kling to upstream image generators, prompt builders, or downstream editing tools
  • Scheduling: trigger video generation from cron jobs, webhooks, or CI/CD pipelines
  • Version control: store prompts, parameters, and outputs programmatically for reproducibility

For a hands-on look at this in action, check out the Kling AI video generation feature page.

Prerequisites: What You Need Before Starting

Before you make your first API call, gather these items:

  1. A Wireflow account with an API key. Sign up at wireflow.ai/pricing, then navigate to Settings > API Keys. Your key starts with sk- and is shown only once, so store it securely.
  2. A tool for making HTTP requests: curl, Postman, or any language with an HTTP client (Python requests, Node.js fetch, etc.)
  3. A base image (for image-to-video mode) or a text prompt (for text-to-video mode)
  4. Familiarity with async APIs: Kling video generation is asynchronous. You submit a job, receive an execution ID, then poll until the result is ready.

The Wireflow API base URL is https://www.wireflow.ai/api/v1 and uses Bearer token authentication. Every request includes this header:

Authorization: Bearer sk-your-api-key

Missing or malformed keys return a 401 error with the message "Invalid API key format. Expected: Bearer sk-...". Expired keys also return 401 with "API key has expired". Full authentication docs are available in the Wireflow developer portal.

API authentication flow

Step 1: Create a Workflow with a Kling Video Node

Wireflow workflows are directed graphs built on React Flow, with hard limits of 100 nodes and 500 edges per workflow. To generate video with Kling, you need at minimum two nodes: an input node and a Kling video node. Each node has a type of "basedNode" with a data object containing the category, nodeType, and params. Here is a minimal workflow creation request using the Wireflow workflows API:

curl -X POST https://www.wireflow.ai/api/v1/workflows \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Kling Video Generator",
    "nodes": [
      {
        "id": "input-1",
        "type": "basedNode",
        "position": {"x": 0, "y": 0},
        "data": {
          "label": "Text Prompt",
          "category": "input",
          "nodeType": "input:text",
          "params": {
            "prompt": "A golden sunset over ocean waves, cinematic slow motion"
          }
        }
      },
      {
        "id": "kling-1",
        "type": "basedNode",
        "position": {"x": 400, "y": 0},
        "data": {
          "label": "Kling Video",
          "category": "video",
          "nodeType": "video:kling_video_2_5_i2v",
          "params": {
            "aspect_ratio": "16:9",
            "generate_audio": false
          }
        }
      }
    ],
    "edges": [
      {
        "id": "edge-1",
        "source": "input-1",
        "target": "kling-1",
        "sourceHandle": "out-prompt",
        "targetHandle": "in-prompt"
      }
    ]
  }'

The response returns a workflow ID that you will use in subsequent calls. The nodeType value video:kling_video_2_5_i2v selects the Kling 2.5 image-to-video model. If you pass a text prompt without an image, Kling generates video directly from text. If you connect an image generation node upstream, the output image becomes the first frame of the video. You can also add optional fields like description, tags, and isActive when creating the workflow.

Step 2: Execute the Workflow

With your workflow created, trigger execution by sending a POST to the execute endpoint. The request body accepts nodes, edges, and optional fields like targetNodeId (to run a specific node) and triggerData. You can turn any video pipeline into a callable endpoint this way:

curl -X POST https://www.wireflow.ai/api/v1/workflows/YOUR_WORKFLOW_ID/execute \
  -H "Authorization: Bearer sk-your-api-key" \
  -H "Content-Type: application/json" \
  -d '{"nodes": [], "edges": []}'

This returns a 201 status with an executionId. You can also include an Idempotency-Key header to prevent duplicate executions if your client retries on network failure. Identical keys within 24 hours replay the original response without running the workflow again. Every response carries an X-Request-Id header for troubleshooting and support.

Workflow execution diagram

Rate limits depend on your plan. Free accounts get 10 requests per minute and 50 daily executions. Pro accounts get 60 requests per minute and 1,000 daily executions. All plans share a 10 executions-per-minute cap to prevent automation overload. Every response includes X-RateLimit-Remaining and X-RateLimit-Reset headers so you can throttle your client accordingly. See the full rate limit reference for all tiers.

Step 3: Poll for Results

Video generation takes 30 to 120 seconds depending on clip length and quality tier. Poll the execution status using the dedicated poll endpoint with exponential backoff:

curl https://www.wireflow.ai/api/v1/workflows/executions/EXECUTION_ID/poll \
  -H "Authorization: Bearer sk-your-api-key"

The response status field transitions through these states:

Status Meaning
RUNNING Nodes are still processing
COMPLETED All nodes finished; outputs available
FAILED An error occurred; check the error field

A recommended polling pattern: start at 1-second intervals, multiply by 1.5 after each poll, and cap at 10 seconds between requests. Once status is COMPLETED, the response includes node outputs with URLs to the generated video file. You can also call GET /workflows/executions/{id} for full execution details including timing and credit usage per node.

import time
import requests

api_key = "sk-your-api-key"
headers = {"Authorization": f"Bearer {api_key}"}
execution_id = "exec_456"

interval = 1
while True:
    resp = requests.get(
        f"https://www.wireflow.ai/api/v1/workflows/executions/{execution_id}/poll",
        headers=headers
    )
    data = resp.json()
    if data["status"] == "COMPLETED":
        print("Video URL:", data["nodeOutputs"]["kling-1"]["video"])
        break
    if data["status"] == "FAILED":
        print("Error:", data["error"])
        break
    time.sleep(interval)
    interval = min(interval * 1.5, 10)

If you are building a production system, consider using webhooks instead of polling. Wireflow supports webhook triggers that can start workflows without an API key and notify your server when execution completes.

Step 4: Image-to-Video Mode with Kling

For higher-quality results, many teams generate a base image first and then animate it with Kling. This is the image-to-video (i2v) approach. In a Wireflow workflow, you chain an image generation model into the Kling node's image_url input.

The Kling node accepts these image-related inputs:

  • image_url (Start Frame): the image that becomes the first frame of the video
  • end_image_url (End Frame): optional target image for the final frame, giving you control over the motion trajectory
  • prompt: text describing the desired motion, camera movement, and scene dynamics

This two-step pattern produces more consistent results because you control the visual composition of the starting frame before any motion is applied. You can also use a batch generation approach to create multiple starting frames and generate video variants from each one.

Image-to-video pipeline

Step 5: Handling Errors and Edge Cases

Production API integrations need to handle several common scenarios. All error responses follow the shape {"error": "message"} with additional context fields for structured errors. Here are the most relevant ones for video generation workflows:

  • 402 Insufficient Credits: the response includes requiredCredits, availableCredits, and a breakdown array showing each node's credit cost by nodeId and nodeLabel. Top up your account or reduce the workflow complexity.
  • 429 Rate Limited: back off and retry after the Retry-After header value. Do not hammer the endpoint, as repeated 429s may trigger temporary blocks.
  • 500 Server Error: retry once after a short delay. If the error persists, check the X-Request-Id header and contact support.

For video-specific issues, common problems include prompts that are too vague (resulting in minimal motion), images that are too low-resolution for the i2v input, and aspect ratios that don't match the input image. Kling works best with 16:9 or 9:16 aspect ratios and input images of at least 1024px on the longest edge. You can preview results before committing to batch runs by testing individual prompts in the visual node editor.

Advanced: Webhooks and No-Auth Triggers

If you want external services like Zapier, Make, or CI pipelines to trigger Kling video generation without exposing your API key, use Wireflow's webhook endpoints. Create a webhook for your workflow, then trigger it with:

curl -X POST https://www.wireflow.ai/api/workflow/WEBHOOK_ID/trigger \
  -H "Content-Type: application/json" \
  -d '{"prompt": "A timelapse of city traffic at night, neon reflections"}'

This returns a 202 with an executionId. Note the path uses singular /workflow/ (not /workflows/). No API key is required on this call. The webhook endpoint supports CORS with Access-Control-Allow-Origin: *, so you can call it from browser-based applications or form submissions. Poll for results using your API key as described in Step 3. This pattern is useful for building AI game asset generation pipelines where video content needs to be produced on demand.

Webhook trigger flow

Try it yourself: Explore the Wireflow API docs to build your own Kling video generation workflow with the exact endpoints and node types covered in this guide.

Conclusion

Generating videos with Kling AI via API opens up automation possibilities that the web interface simply cannot match. From single prompt-to-video calls to multi-step pipelines with upstream image generation, the async execution pattern gives you full control over every stage of the process. Wireflow makes this accessible through a unified REST API that handles authentication, rate limiting, and webhook triggers, so you can focus on building your video pipeline rather than managing infrastructure.

Frequently Asked Questions

What video resolutions does Kling AI support via API?

Kling generates video at up to 1080p resolution at 24 frames per second. You can select between 16:9 and 9:16 aspect ratios. Standard-tier generation is faster but lower fidelity; pro-tier generation takes longer but produces smoother motion and better detail.

How long does it take to generate a video with Kling via API?

Typical generation time is 30 to 120 seconds for a 5-second clip, depending on the quality tier and current server load. Longer clips (up to 10 seconds) take proportionally more time. Use the polling pattern described above to check status without blocking your application.

Can I generate video from both text and images?

Yes. Kling supports text-to-video (provide only a text prompt) and image-to-video (provide a starting frame image plus a motion prompt). Image-to-video generally produces more predictable results because you control the initial composition.

What are the rate limits for the Wireflow API?

Free accounts allow 10 requests per minute and 50 executions per day. Starter plans increase to 200 daily executions. Pro plans allow 60 requests per minute and 1,000 daily executions. Enterprise plans have unlimited daily executions. All plans share a 10 executions-per-minute cap to prevent automation overload.

Is there an official SDK for calling the Wireflow API?

There is no official SDK. Wireflow's API is plain REST with JSON payloads, so it works with any HTTP client: curl, Python requests, Node.js fetch, Go net/http, or similar. This keeps integration simple with no dependency management.

Can I chain Kling with other AI models in a single API call?

Yes. Wireflow workflows let you connect multiple model nodes in sequence. A common pattern is: text prompt to image generator (such as generate:flux_2 or generate:imagen4) to Kling video node (video:kling_video_2_5_i2v). Each node's output feeds into the next node's input automatically. You execute the entire chain with a single API call to the workflow execute endpoint.

How do I handle failed video generations?

Check the execution status via the poll endpoint. Failed executions include an error field describing what went wrong. Common causes include insufficient credits (402 with a breakdown array), invalid input images, or overly generic prompts. You can retry the same execution by calling the execute endpoint again with the same parameters, or use an Idempotency-Key header to prevent accidental duplicates.

Does Kling support audio in generated videos?

Kling 2.5 and 3.0 support native audio generation, including ambient sound effects, music, and voice narration. Enable it by setting generate_audio: true in the Kling node parameters. Lip-sync capabilities are also available for portrait videos with synchronized speech.