API / extend-an-imagine-video-api

API

Extend an Imagine video with the API

Extend an Imagine video with the API

Video extension continues an existing clip from its last frame. You send the source video, a prompt for what happens next, and a duration for the new segment only. The API returns one file: original plus extension.

Extend with the Python SDK

  1. Create an API key in console.x.ai.
  2. Install xai-sdk and set XAI_API_KEY.
  3. Call client.video.extend with the source URL, the next-shot prompt, model id, and duration for the added seconds.
import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

response = client.video.extend(
    prompt="The shot pans over the shoulder. Calm, controlled scene.",
    model="grok-imagine-video",
    video_url="<VIDEO_URL>",
    duration=10,
)

print(response.url)

duration is the length of the new tail. A 10s source plus duration=5 yields about 15s total.

REST shape

POST https://api.x.ai/v1/videos/extensions with model, prompt, duration, and video: { "url": "..." }. Poll GET /v1/videos/{request_id} until status is done, then read video.url. Source video can also be a data URI or a Files API file_id.

Pitfalls

  • Edit and extend are different endpoints. Edits use /v1/videos/edits and keep the original length; extensions add time from the last frame.
  • Output URLs are temporary. Download or persist through the Files API if you need a lasting copy.
  • Model ids in the docs sometimes read grok-imagine-video and sometimes grok-imagine-video-1.5. Use the id your console lists for extension, and fail closed on failed_precondition if the team lacks the capability.