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

# Tạo video xAI

> Sử dụng CometAPI POST /grok/v1/videos/generations để tạo tác vụ image-to-video Grok từ một ảnh đầu vào và prompt, sau đó thăm dò kết quả qua request_id.

Sử dụng endpoint này để khởi tạo một tác vụ image-to-video Grok từ một ảnh nguồn và một prompt. Endpoint sẽ trả về `request_id` ngay lập tức, vì vậy hãy coi đây là bước đầu tiên trong một quy trình async.

## Bắt đầu với một yêu cầu nhỏ

* Sử dụng `model: grok-imagine-video-1.5`
* Đặt `image.url` thành một URL JPEG, PNG hoặc WebP công khai
* Với yêu cầu đầu tiên, giữ `duration` ở `1` và `resolution` ở `720p`
* Giữ `prompt` rõ ràng để hướng chuyển động của animation được xác định cụ thể
* Nếu bạn gửi base64, hãy giữ nguyên định dạng trong tài liệu và ví dụ OpenAPI là `data:image/png;base64,<BASE64_IMAGE_DATA>`

## Duration và resolution

| Setting        | Supported values                             | Default                | Boundary behavior                                                                                                      |
| -------------- | -------------------------------------------- | ---------------------- | ---------------------------------------------------------------------------------------------------------------------- |
| `duration`     | số nguyên `1`-`15`                           | `8`                    | Sử dụng clip ngắn hơn cho lần kiểm thử đầu tiên; clip dài hơn tốn chi phí hơn và mất nhiều thời gian hơn để hoàn tất.  |
| `resolution`   | `480p`, `720p`, `1080p`                      | `720p` trong các ví dụ | Yêu cầu độ phân giải cao hơn tốn chi phí hơn và mất nhiều thời gian hơn để hoàn tất.                                   |
| `aspect_ratio` | các nhãn tỷ lệ như `1:1`, `16:9` hoặc `9:16` | `16:9` trong các ví dụ | Endpoint này sử dụng `resolution` cùng với `aspect_ratio`; không gửi trường `size` riêng để kiểm soát chính xác `WxH`. |

| Request combination                      | Intended output `WxH` |
| ---------------------------------------- | --------------------- |
| `resolution: 480p`, `aspect_ratio: 1:1`  | `480x480`             |
| `resolution: 720p`, `aspect_ratio: 16:9` | `1280x720`            |
| `resolution: 720p`, `aspect_ratio: 9:16` | `720x1280`            |

## Luồng tác vụ

<Steps>
  <Step title="Tạo tác vụ">
    Gửi prompt và ảnh nguồn, sau đó lưu `request_id` được trả về.
  </Step>

  <Step title="Thăm dò để hoàn tất">
    Gọi [Lấy kết quả video xAI](./get-video-generation-results) cho đến khi trạng thái provider lồng nhau trở thành `done`.
  </Step>

  <Step title="Lưu trữ đầu ra">
    Sao chép `video.url` cuối cùng vào hệ thống lưu trữ của riêng bạn nếu bạn cần dùng nó sau thời gian cung cấp tạm thời của provider.
  </Step>
</Steps>


## OpenAPI

````yaml api/openapi/video/xai/post-video-generation.openapi.json POST /grok/v1/videos/generations
openapi: 3.1.0
info:
  title: Video Generation API
  version: 1.0.0
  description: >-
    Create an asynchronous xAI Grok image-to-video job and poll it later with
    GET /grok/v1/videos/{request_id}.
servers:
  - url: https://api.cometapi.com
security:
  - bearerAuth: []
paths:
  /grok/v1/videos/generations:
    post:
      summary: Create an xAI video generation job
      description: >-
        Start a Grok image-to-video job from a prompt and one source image. Save
        the returned request_id and poll the query endpoint until the provider
        status becomes done.
      operationId: video_generation
      requestBody:
        required: true
        content:
          application/json:
            schema:
              type: object
              required:
                - prompt
              properties:
                prompt:
                  type: string
                  description: >-
                    Prompt describing how the source image should animate.
                    CometAPI requires this field for this route.
                  example: >-
                    Animate the still image with a slow camera move and natural
                    motion.
                aspect_ratio:
                  type: string
                  description: >-
                    Output aspect ratio. Combine with resolution to choose the
                    target frame; for example 480p with 1:1 targets 480x480,
                    720p with 16:9 targets 1280x720, and 720p with 9:16 targets
                    720x1280. This endpoint does not expose an exact size field.
                  enum:
                    - '1:1'
                    - '16:9'
                    - '9:16'
                    - '4:3'
                    - '3:4'
                    - '3:2'
                    - '2:3'
                duration:
                  type: integer
                  description: >-
                    Output duration in seconds. Use an integer from 1 through
                    15. If omitted, CometAPI treats the request as an 8-second
                    generation for billing and default handling.
                  default: 8
                image:
                  type: object
                  required:
                    - url
                  properties:
                    url:
                      type: string
                      description: >-
                        Public image URL or data URI such as
                        data:image/png;base64,<BASE64_IMAGE_DATA>.
                  description: >-
                    Source image for image-to-video. Use a public JPEG, PNG, or
                    WebP URL, or a data URI such as
                    data:image/png;base64,<BASE64_IMAGE_DATA>.
                model:
                  type: string
                  description: >-
                    xAI video model id. The default example uses Grok Imagine
                    1.5 for image-to-video.
                  default: grok-imagine-video-1.5
                  example: grok-imagine-video-1.5
                output:
                  type: object
                  properties:
                    upload_url:
                      type: string
                      description: >-
                        Optional signed URL for uploading the finished result to
                        your own storage.
                resolution:
                  type: string
                  description: >-
                    Output resolution. Use 720p for a small image-to-video test,
                    or 1080p when you need a higher resolution tier.
                  enum:
                    - 480p
                    - 720p
                    - 1080p
                  default: 720p
                size:
                  type: string
                  description: Deprecated size field. Prefer aspect_ratio and resolution.
                user:
                  type: string
                  description: Optional end-user identifier.
              default:
                model: grok-imagine-video-1.5
                prompt: >-
                  Animate the still image with a slow camera move and natural
                  motion.
                image:
                  url: https://your-image-host/source.jpg
                duration: 1
                aspect_ratio: '16:9'
                resolution: 720p
            examples:
              Image URL:
                summary: Image URL
                value:
                  model: grok-imagine-video-1.5
                  prompt: >-
                    Animate the still image with a slow camera move and natural
                    motion.
                  image:
                    url: https://your-image-host/source.jpg
                  duration: 1
                  aspect_ratio: '16:9'
                  resolution: 720p
              Data URI:
                summary: Data URI
                value:
                  model: grok-imagine-video-1.5
                  prompt: Animate the clouds drifting across the sky.
                  image:
                    url: data:image/png;base64,<BASE64_IMAGE_DATA>
                  duration: 1
                  aspect_ratio: '16:9'
                  resolution: 720p
      responses:
        '200':
          description: Request accepted.
          content:
            application/json:
              schema:
                type: object
                required:
                  - request_id
                properties:
                  request_id:
                    type: string
                    description: Deferred request id used for polling.
                example:
                  request_id: <request_id>
              example:
                request_id: <request_id>
      x-codeSamples:
        - lang: Shell
          label: Image URL
          source: |
            curl https://api.cometapi.com/grok/v1/videos/generations \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              -d '{
                "model": "grok-imagine-video-1.5",
                "prompt": "Animate the still image with a slow camera move and natural motion.",
                "image": {"url": "https://your-image-host/source.jpg"},
                "duration": 1,
                "aspect_ratio": "16:9",
                "resolution": "720p"
              }'
        - lang: Shell
          label: Data URI
          source: |
            IMAGE_B64=$(base64 < source.png | tr -d '\n')

            curl https://api.cometapi.com/grok/v1/videos/generations \
              -H "Authorization: Bearer $COMETAPI_KEY" \
              -H "Content-Type: application/json" \
              --data-binary @- <<EOF
            {
              "model": "grok-imagine-video-1.5",
              "prompt": "Animate the clouds drifting across the sky.",
              "image": {"url": "data:image/png;base64,${IMAGE_B64}"},
              "duration": 1,
              "aspect_ratio": "16:9",
              "resolution": "720p"
            }
            EOF
        - lang: Python
          label: Image URL
          source: |
            import os
            import requests

            response = requests.post(
                "https://api.cometapi.com/grok/v1/videos/generations",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                    "model": "grok-imagine-video-1.5",
                    "prompt": "Animate the still image with a slow camera move and natural motion.",
                    "image": {"url": "https://your-image-host/source.jpg"},
                    "duration": 1,
                    "aspect_ratio": "16:9",
                    "resolution": "720p",
                },
            )

            task = response.json()
            print(task["request_id"])  # poll GET /grok/v1/videos/{request_id}
        - lang: Python
          label: Data URI
          source: |
            import base64
            import os
            import requests

            with open("source.png", "rb") as f:
                image_b64 = base64.b64encode(f.read()).decode()

            response = requests.post(
                "https://api.cometapi.com/grok/v1/videos/generations",
                headers={"Authorization": "Bearer " + os.environ["COMETAPI_KEY"]},
                json={
                    "model": "grok-imagine-video-1.5",
                    "prompt": "Animate the clouds drifting across the sky.",
                    "image": {"url": "data:image/png;base64," + image_b64},
                    "duration": 1,
                    "aspect_ratio": "16:9",
                    "resolution": "720p",
                },
            )

            task = response.json()
            print(task["request_id"])
        - lang: JavaScript
          label: Image URL
          source: >
            const response = await
            fetch("https://api.cometapi.com/grok/v1/videos/generations", {
                method: "POST",
                headers: {
                    Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                    model: "grok-imagine-video-1.5",
                    prompt: "Animate the still image with a slow camera move and natural motion.",
                    image: { url: "https://your-image-host/source.jpg" },
                    duration: 1,
                    aspect_ratio: "16:9",
                    resolution: "720p",
                }),
            });


            const task = await response.json();

            console.log(task.request_id); // poll GET
            /grok/v1/videos/{request_id}
        - lang: JavaScript
          label: Data URI
          source: >
            import { readFile } from "node:fs/promises";


            const imageB64 = (await readFile("source.png")).toString("base64");


            const response = await
            fetch("https://api.cometapi.com/grok/v1/videos/generations", {
                method: "POST",
                headers: {
                    Authorization: `Bearer ${process.env.COMETAPI_KEY}`,
                    "Content-Type": "application/json",
                },
                body: JSON.stringify({
                    model: "grok-imagine-video-1.5",
                    prompt: "Animate the clouds drifting across the sky.",
                    image: { url: `data:image/png;base64,${imageB64}` },
                    duration: 1,
                    aspect_ratio: "16:9",
                    resolution: "720p",
                }),
            });


            const task = await response.json();

            console.log(task.request_id);
components:
  securitySchemes:
    bearerAuth:
      type: http
      scheme: bearer
      description: Bearer token authentication. Use your CometAPI key.

````