# How to use Kling AI via API?

Source: https://zilliz.com/ai-faq/how-to-use-kling-ai-via-api
Author: zilliz

You can use Kling AI via API by following the common pattern used by hosted video generation services: **create an async generation task**, then **poll (or receive a webhook) for completion**, and finally **download the result**. In practical terms, your backend sends an authenticated POST request containing your prompt and parameters (for text-to-video) or a reference image plus prompt (for image-to-video). The API returns a `task_id` (or similar). Your system then checks task status until it becomes `succeeded` or `failed`, at which point you fetch the output URL(s). This async design is important because video generation can take long enough that synchronous HTTP requests would time out. If you’re building a product, you should plan your UI around job states: queued → running → finished, with clear failure reasons and retry options.

Implementation details vary by provider and model version, but the core request shape is usually consistent. You’ll typically supply:
- **Authentication**: an API key (often in `Authorization: Bearer ...` or a vendor-specific header)
- **Prompt**: text describing subject, scene, camera, and motion
- **Optional negative prompt**: what must not appear (text overlays, logos, extra limbs)
- **Duration / length**: commonly short clips like 5s or 10s
- **Aspect ratio**: `16:9`, `9:16`, or `1:1`
- **Mode/quality**: standard vs pro/high quality
- **Image input** (for image-to-video): either a direct upload or a URL to a stored image
Operationally, you should add idempotency (so retries don’t create duplicate jobs), rate-limit handling (429 responses), and structured error parsing (invalid params, insufficient credits). If the API supports webhooks, prefer them over polling: webhooks reduce latency and cost, and they scale better when you have many concurrent jobs.
