> ## 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.

# Hướng dẫn nhanh Veo 3 API: Tạo video với CometAPI

> Tạo một tác vụ video Veo với CometAPI, thăm dò trạng thái tác vụ và lưu đầu ra video đã hoàn tất bằng curl, Python hoặc Node.js.

## Những gì bạn sẽ xây dựng

Bạn sẽ tạo một tác vụ video Veo bằng multipart form data, lưu task ID được trả về, thăm dò endpoint truy xuất và lưu URL asset cuối cùng hoặc tệp trong hệ thống của riêng bạn.

## Điều kiện tiên quyết

* Một khóa API CometAPI được lưu trong `COMETAPI_KEY`
* Python 3.10+ với `requests`, hoặc Node.js 18+
* Một trình chạy tác vụ phía máy chủ để thăm dò

## API key, base URL, xác thực

Tạo tác vụ Veo với:

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

Thăm dò trạng thái tác vụ Veo với:

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

Xác thực bằng Bearer token:

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

## Ví dụ mã

Sử dụng các tab bên dưới để xem các ví dụ có thể sao chép bằng cURL, Python và Node.js.

<CodeGroup>
  ```bash cURL theme={null}
  curl https://api.cometapi.com/v1/videos \
    -H "Authorization: Bearer $COMETAPI_KEY" \
    -F model=veo3.1-fast \
    -F "prompt=A paper kite floats above a field." \
    -F seconds=4 \
    -F size=1280x720

  curl "https://api.cometapi.com/v1/videos/<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}"}

  create_response = requests.post(
      "https://api.cometapi.com/v1/videos",
      headers=headers,
      files={
          "model": (None, "veo3.1-fast"),
          "prompt": (None, "A paper kite floats above a field."),
          "seconds": (None, "4"),
          "size": (None, "1280x720"),
      },
      timeout=60,
  )
  create_response.raise_for_status()
  task_id = create_response.json()["id"]

  for _ in range(60):
      retrieve_response = requests.get(
          f"https://api.cometapi.com/v1/videos/{task_id}",
          headers=headers,
          timeout=30,
      )
      retrieve_response.raise_for_status()
      task = retrieve_response.json()
      if task["status"] in {"completed", "failed", "error"}:
          print(task)
          break
      time.sleep(5)
  else:
      raise TimeoutError("Veo task did not finish in time")
  ```

  ```javascript Node.js theme={null}
  const form = new FormData();
  form.append("model", "veo3.1-fast");
  form.append("prompt", "A paper kite floats above a field.");
  form.append("seconds", "4");
  form.append("size", "1280x720");

  const createResponse = await fetch("https://api.cometapi.com/v1/videos", {
    method: "POST",
    headers: { Authorization: `Bearer ${process.env.COMETAPI_KEY}` },
    body: form,
  });

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

  const { id } = await createResponse.json();

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

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

    const task = await retrieveResponse.json();
    if (["completed", "failed", "error"].includes(task.status)) {
      console.log(task);
      break;
    }
    await new Promise((resolve) => setTimeout(resolve, 5000));
  }
  ```
</CodeGroup>

## Giải thích quy trình

Việc tạo video Veo là bất đồng bộ. Endpoint tạo chấp nhận multipart form data và trả về task ID ngay lập tức. Hãy thăm dò endpoint truy xuất cho đến khi tác vụ đạt trạng thái kết thúc, sau đó lưu lại URL video cuối cùng hoặc chi tiết tệp từ phản hồi đã hoàn tất.

Hãy dùng thời lượng ngắn và kích thước hữu ích nhỏ nhất cho các lần kiểm thử đầu tiên. Di chuyển các asset đã hoàn tất vào hệ thống lưu trữ của riêng bạn khi ứng dụng của bạn cần lưu giữ lâu dài.

## Các tham số phổ biến

| Tham số           | Cách dùng                                                                    |
| ----------------- | ---------------------------------------------------------------------------- |
| `model`           | model ID của Veo. Ví dụ trong tài liệu tham chiếu API sử dụng `veo3.1-fast`. |
| `prompt`          | Prompt văn bản cho tác vụ video.                                             |
| `seconds`         | Trường biểu mẫu thời lượng. Tài liệu tham chiếu ghi nhận `4`, `6` và `8`.    |
| `size`            | Kích thước chính xác `WxH`, chẳng hạn như `1280x720`.                        |
| `input_reference` | Tệp hình ảnh khung hình đầu tiên tùy chọn cho image-to-video.                |

## Khắc phục sự cố / FAQ

<AccordionGroup>
  <Accordion title="Yêu cầu thất bại do vấn đề content type">
    Gửi dữ liệu biểu mẫu multipart. Không gửi yêu cầu tạo Veo dưới dạng JSON.
  </Accordion>

  <Accordion title="Việc polling mất nhiều thời gian hơn dự kiến">
    Sử dụng vòng lặp polling có giới hạn, lưu task ID, và hiển thị trạng thái đang chờ trong ứng dụng của bạn thay vì chặn một yêu cầu web.
  </Accordion>

  <Accordion title="Tôi nên kiểm soát chi phí như thế nào">
    Bắt đầu với thời lượng ngắn, một task và kích thước nhỏ nhất vẫn chấp nhận được. Sử dụng quota tài khoản và ước tính chi phí trước khi mở rộng số lượng task.
  </Accordion>
</AccordionGroup>

## Các bước tiếp theo

* Đọc [Tài liệu tham chiếu API tạo video Veo 3](/api/video/veo3/create).
* Poll với [Truy xuất một video Veo 3](/api/video/veo3/retrieve).
* Tìm các model video Veo trong [Models](/vi/overview/models).
* Xem lại [Sử dụng polling và webhook cho việc tạo video](/vi/guides/webhook-and-polling-for-video-generation).
* Ước tính chi phí task với [Ước tính chi phí yêu cầu trước khi gọi một model](/vi/guides/how-to-estimate-cost-before-calling-a-model).
