> ## Documentation Index
> Fetch the complete documentation index at: https://apidoc.cometapi.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kling API quickstart: Generate videos with CometAPI

> Create a Kling text-to-video task through CometAPI, poll task status, and handle callback-based completion.

## What you will build

You will submit a Kling text-to-video task, store the returned task ID, poll the matching Kling task endpoint, and decide when to add `callback_url` for push notifications.

## Prerequisites

* A CometAPI API key stored in `COMETAPI_KEY`
* Python 3.10+ with `requests`, or Node.js 18+
* A server-side worker for polling or an HTTPS callback endpoint for webhooks

## API key, base URL, authentication

Create a Kling text-to-video task with:

```text theme={null}
POST https://api.cometapi.com/kling/v1/videos/text2video
```

Poll the task with:

```text theme={null}
GET https://api.cometapi.com/kling/v1/videos/text2video/<task_id>
```

Authenticate with a Bearer token:

```text theme={null}
Authorization: Bearer $COMETAPI_KEY
```

## Code examples

Use the tabs below for copyable examples in cURL, Python, and Node.js.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/kling/v1/videos/text2video \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "prompt": "A small ceramic cup on a wooden table, steam rising in soft morning light",
      "model_name": "kling-v3",
      "mode": "std",
      "duration": "5",
      "sound": "off"
    }'

  curl "https://api.cometapi.com/kling/v1/videos/text2video/<task_id>" \
    -H "Authorization: Bearer $COMETAPI_KEY"
  ```

  ```python Python theme={null}
  import os
  import time

  import requests

  api_key = os.environ["COMETAPI_KEY"]
  headers = {
      "Authorization": f"Bearer {api_key}",
      "Content-Type": "application/json",
  }

  create_response = requests.post(
      "https://api.cometapi.com/kling/v1/videos/text2video",
      headers=headers,
      json={
          "prompt": "A small ceramic cup on a wooden table, steam rising in soft morning light",
          "model_name": "kling-v3",
          "mode": "std",
          "duration": "5",
          "sound": "off",
      },
      timeout=60,
  )
  create_response.raise_for_status()
  task_id = create_response.json()["data"]["task_id"]

  for _ in range(60):
      task_response = requests.get(
          f"https://api.cometapi.com/kling/v1/videos/text2video/{task_id}",
          headers={"Authorization": f"Bearer {api_key}"},
          timeout=30,
      )
      task_response.raise_for_status()
      task = task_response.json()["data"]
      if task["task_status"] in {"succeed", "failed"}:
          print(task)
          break
      time.sleep(5)
  else:
      raise TimeoutError("Kling task did not finish in time")
  ```

  ```javascript Node.js theme={null}
  const createResponse = await fetch(
    "https://api.cometapi.com/kling/v1/videos/text2video",
    {
      method: "POST",
      headers: {
        Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
        "Content-Type": "application/json",
      },
      body: JSON.stringify({
        prompt: "A small ceramic cup on a wooden table, steam rising in soft morning light",
        model_name: "kling-v3",
        mode: "std",
        duration: "5",
        sound: "off",
      }),
    },
  );

  if (!createResponse.ok) {
    throw new Error(await createResponse.text());
  }

  const created = await createResponse.json();
  const taskId = created.data.task_id;

  for (let attempt = 0; attempt < 60; attempt += 1) {
    const taskResponse = await fetch(
      `https://api.cometapi.com/kling/v1/videos/text2video/${taskId}`,
      { headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` } },
    );

    if (!taskResponse.ok) {
      throw new Error(await taskResponse.text());
    }

    const task = (await taskResponse.json()).data;
    if (["succeed", "failed"].includes(task.task_status)) {
      console.log(task);
      break;
    }
    await new Promise((resolve) => setTimeout(resolve, 5000));
  }
  ```
</CodeGroup>

## Flow explanation

Kling video generation is asynchronous. The create endpoint returns a `data.task_id`. Poll `GET /kling/v1/videos/text2video/<task_id>` until `data.task_status` reaches `succeed` or `failed`. When the task succeeds, copy the finished asset URL from `data.task_result` into your own storage if you need durable access.

Add `callback_url` when you want task status updates pushed to an HTTPS endpoint you control. Keep polling available for reconciliation and missed callback delivery.

## Common parameters

| Parameter      | Use                                                                             |
| -------------- | ------------------------------------------------------------------------------- |
| `prompt`       | Text prompt for the video task.                                                 |
| `model_name`   | Kling model ID. The reference example uses `kling-v3`.                          |
| `mode`         | Generation mode. Start with `std` before testing higher-cost modes.             |
| `duration`     | Output length. The reference page uses `5` for a first request.                 |
| `sound`        | Use `off` for a deterministic no-audio first request on supported model tracks. |
| `callback_url` | Optional HTTPS URL for task status callbacks.                                   |

## Troubleshooting / FAQ

<AccordionGroup>
  <Accordion title="The response shape is different from OpenAI video routes">
    Kling uses provider-specific JSON fields such as `data.task_id`, `data.task_status`, and `data.task_result`. Do not parse it like the OpenAI-compatible `/v1/videos` routes.
  </Accordion>

  <Accordion title="The task never reaches succeed">
    Use bounded polling and inspect `data.task_status_msg` when a task fails. Store the task ID for later diagnostics.
  </Accordion>

  <Accordion title="Should I use callback_url or polling">
    Use polling as the baseline. Add `callback_url` for push delivery, then reconcile final state with polling.
  </Accordion>
</AccordionGroup>

## Next steps

* Read the [Kling text-to-video API reference](/api/video/kling/text-to-video).
* Poll with [Get a Kling task](/api/video/kling/individual-queries).
* Configure callbacks with [Use Kling callback URLs](/api/video/kling/callback_url).
* Find Kling video models in [Models](/overview/models).
* Review [Use polling and webhooks for video generation](/guides/webhook-and-polling-for-video-generation).
* Estimate task cost with [Estimate request cost before calling a model](/guides/how-to-estimate-cost-before-calling-a-model).
