Grok Videos Generation API Integration Guide

This article introduces how to integrate the Grok Videos Generation API, which can generate Grok Imagine (xAI) videos using a text prompt, an input image, and optional reference images.

Application Process

To use the Grok Videos Generation API, first visit the Ace Data Cloud Console to obtain your API Token for later use.

If you have not yet logged in or registered, you will be automatically redirected to the login page to register and log in. Once completed, you will automatically return to the current page.

The API Key for a general application can be used for services enabled for the account; the Key for a dedicated application can be used within that application's service scope. Please check the Key's permissions, validity period, and limits, and confirm that the application balance is sufficient to pay for this request. Available trial credits are subject to what is shown in your account. If the balance is insufficient, click the “Recharge” button.

📘 Full documentation: Grok Videos Generation API →

Model Overview

You can select one of the following four publicly available models using model. Their capabilities and pricing methods are as follows:

  • grok-imagine-video-1.5-fast:reverse (default): Supports text-to-video (passing only prompt) and image-to-video (passing image_url), with durations of 6–30 seconds. Pricing is tiered by duration, and this is the least expensive option.
  • grok-imagine-video:reverse: Supports text-to-video and image-to-video, with durations of 1–15 seconds. Charged per second of output.
  • grok-imagine-video:official: Supports text-to-video and image-to-video, with durations of 1–15 seconds. Charged per second of output, with higher image quality.
  • grok-imagine-video-1.5:official: Only supports image-to-video. You must provide image_url. Supports durations of 1–15 seconds and resolutions up to 1080p. Charged per second of output.

Basic Usage

First, let's look at the basic usage. By providing parameters such as the prompt prompt and model model, you can generate the corresponding video.

Here, we set the Request Headers, including:

  • accept: The desired response format. Set it to application/json here, meaning JSON format.
  • authorization: The key used to call the API. After obtaining it, you can select it directly from the dropdown.

We also set the Request Body, including:

  • prompt: A text prompt describing the video content you want to generate. Required for text-to-video; optional when image_url is provided.
  • model: The model used to generate the video. Options are grok-imagine-video-1.5-fast:reverse (default), grok-imagine-video:reverse, grok-imagine-video:official, or grok-imagine-video-1.5:official.
  • image_url: The input image URL for image-to-video. Required when model is grok-imagine-video-1.5:official.
  • reference_image_urls: An optional array of reference image URLs used to guide the style or content of the video.
  • aspect_ratio: The aspect ratio of the generated video. Options are 1:1 / 16:9 / 9:16 / 4:3 / 3:4 / 3:2 / 2:3.
  • resolution: The output resolution. Options are 480p (default), 720p, or 1080p.
  • duration: The duration of the generated video in seconds. The range is 6–30 for grok-imagine-video-1.5-fast:reverse and 1–15 for the other models, with a default of 6. A duration of 6 or 10 seconds is recommended, as these two standard durations are relatively stable.
  • callback_url: The asynchronous callback URL. When set, the API immediately returns a task_id and POSTs the result to this URL when the task is complete.
  • async: Optional. When set to true, the API immediately returns a task_id without requiring a callback_url. You can then poll the corresponding task query endpoint to retrieve the result.

Click the “Try” button to test it. The result will look similar to the following:

{
  "success": true,
  "task_id": "b8976e18-32dc-4718-9ed8-1ea090fcb6ea",
  "trace_id": "fb751e1e-4705-49ea-9fd4-5024b7865ea2",
  "data": [
    {
      "id": "grok-imagine-video-1.5-fast:reverse:41eb9a5f-3b2d-4d1e-9f5a-6c2f1a0b9e77",
      "video_url": "https://cdn.acedata.cloud/c8cbf53aa0.mp4",
      "state": "succeeded"
    }
  ]
}

The response contains several fields, described below:

  • success: Whether this video generation request was successful.
  • task_id: The ID of this video generation task.
  • trace_id: The trace ID of this request, used for troubleshooting.
  • data: The list of generated video results.
    • id: The unique identifier of the generated video.
    • video_url: The URL of the generated video.
    • state: The status of the video generation task. Options are pending / succeeded / failed.

We only need to use the video_url link in data from the result to retrieve the generated video.

The corresponding CURL code is as follows:

curl -X POST 'https://api.acedata.cloud/grok/videos' \
-H 'authorization: Bearer ${bearer_token}' \
-H 'accept: application/json' \
-H 'content-type: application/json' \
-d '{
  "prompt": "A cinematic shot of a kitten chasing a butterfly in a sunlit garden",
  "model": "grok-imagine-video-1.5-fast:reverse",
  "resolution": "480p",
  "duration": 6
}'

The corresponding Python code is as follows:

import requests

url = "https://api.acedata.cloud/grok/videos"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "prompt": "A cinematic shot of a kitten chasing a butterfly in a sunlit garden",
    "model": "grok-imagine-video-1.5-fast:reverse",
    "resolution": "480p",
    "duration": 6
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

Image-to-Video

If you want to generate a video based on an input image, you can provide image_url. This field is required when using grok-imagine-video-1.5:official:

{
  "prompt": "The character slowly turns around and smiles at the camera",
  "model": "grok-imagine-video-1.5:official",
  "image_url": "https://cdn.acedata.cloud/5hmkdg.jpg",
  "resolution": "720p",
  "duration": 6
}

Reference Image Guidance

If you want to use one or more reference images to guide the style or content of the generated video, you can provide an array of image URLs in reference_image_urls:

{
  "prompt": "A character dancing in the same art style",
  "model": "grok-imagine-video-1.5-fast:reverse",
  "reference_image_urls": [
    "https://cdn.acedata.cloud/vunnjf.png"
  ]
}

Asynchronous Callback

Video generation requires some processing time. If you do not want to maintain a long-running connection while waiting, you can provide callback_url. The API will then immediately return a task_id and POST the final result to that URL when the task is complete:

{
  "prompt": "A cinematic shot of a kitten chasing a butterfly in a sunlit garden",
  "model": "grok-imagine-video-1.5-fast:reverse",
  "duration": 6,
  "callback_url": "https://your-domain.com/callback/grok"
}

The immediately returned result is as follows:

{
  "task_id": "b8976e18-32dc-4718-9ed8-1ea090fcb6ea"
}

Query Task Results

If you use an asynchronous callback or want to proactively query the task status, you can use the Grok Tasks API (POST https://api.acedata.cloud/grok/tasks) to query the latest status and result of the task based on its task_id.

Billing Information

The billing method for this service is determined by the model:

  • grok-imagine-video-1.5-fast:reverse: Billed in tiers based on duration, regardless of resolution—6–10 seconds, 11–20 seconds, and 21–30 seconds correspond to different pricing tiers.
  • grok-imagine-video:reverse: Billed by the number of output seconds, total price = unit price × duration.
  • grok-imagine-video:official and grok-imagine-video-1.5:official: Billed by the number of output seconds, with a higher unit price for higher resolutions; official models are billed even if content moderation fails.

Refer to the pricing page for specific unit prices. Failed requests are not billed and do not consume the free quota.

Error Handling

When a problem occurs with a request, the API returns the corresponding error code and description. Common examples are as follows:

  • 400: Invalid request parameters, for example, a missing prompt for text-to-video generation, a missing image_url for grok-imagine-video-1.5:official, or a duration outside the permitted range (6–30 for grok-imagine-video-1.5-fast:reverse, 1–15 for other models).
  • 401: Authentication failed; the token is invalid or does not match the API.
  • 403: Insufficient balance, or the prompt was rejected by content moderation.
  • 429: Requests are too frequent; please try again later.
  • 500: Video generation failed or a service error occurred.