API / edit-a-video-with-imagine-api

API

Edit a video with the Imagine API

Edit a video with the Imagine API

POST https://api.x.ai/v1/videos/edits takes a source clip and a prompt, then returns a new clip of the same length. Model id in the docs is grok-imagine-video. Get a key at console.x.ai.

Minimal call

Source video is a public URL, a data URI, or a Files API file_id. MP4 with H.264, H.265, or AV1.

curl -s -X POST https://api.x.ai/v1/videos/edits \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
    "model": "grok-imagine-video",
    "prompt": "Give the woman a silver necklace",
    "video": { "url": "'"$VIDEO_URL"'" }
  }'

The response is a request_id. Poll GET https://api.x.ai/v1/videos/{request_id} until status is done, then read video.url. Failed and expired states stop the loop.

Python with the official SDK (it polls for you):

import os
import xai_sdk

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

response = client.video.generate(
    prompt="Give the woman a silver necklace",
    model="grok-imagine-video",
    video_url=os.getenv("VIDEO_URL"),
)
print(response.url)

Vercel AI SDK: providerOptions.xai.mode = "edit-video" and videoUrl on xai.video("grok-imagine-video"). duration, aspectRatio, and resolution on that call are ignored. Output keeps the source duration and aspect ratio, capped at 720p.

Branch several edits

Fire concurrent requests against the same source when you want variants (necklace, red outfit, hat). Chain by feeding video.url from one finished edit into the next.

Pitfalls

  • /v1/videos/edits keeps the original length. /v1/videos/extensions adds seconds from the last frame.
  • Output URLs on vidgen.x.ai expire. Download them, or persist with storage_options.
  • A 1080p source comes back at 720p.
  • Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.