Generate text-to-video with the Imagine API
Generate text-to-video with the Imagine API
POST https://api.x.ai/v1/videos/generations with a prompt and model grok-imagine-video-1.5 is text-to-video. The call returns a request_id. Poll GET /v1/videos/{request_id} until status is done, expired, or failed. Get a key at console.x.ai.
On 1.5, text-to-video runs text-to-image then image-to-video internally. The intermediate frame is not returned.
Start and poll
REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $XAI_API_KEY" \
-d '{
"model": "grok-imagine-video-1.5",
"prompt": "A glowing crystal-powered rocket launching from the red dunes of Mars",
"duration": 10,
"aspect_ratio": "16:9",
"resolution": "720p"
}' | jq -r '.request_id')
while true; do
RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \
-H "Authorization: Bearer $XAI_API_KEY")
STATUS=$(echo "$RESULT" | jq -r '.status')
if [ "$STATUS" = "done" ]; then
echo "$RESULT" | jq -r '.video.url'
break
elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then
echo "Request $STATUS"; echo "$RESULT" | jq .
break
fi
sleep 5
done
duration is 1–15 seconds. aspect_ratio accepts 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3 (default 16:9). resolution is 480p (default), 720p, or 1080p. 1080p is available on 1.5 for text-to-video. Audio is on by default; set generate_audio: false for a silent clip.
SDK
client.video.generate(...) polls for you:
import os
import xai_sdk
client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))
response = client.video.generate(
prompt="A glowing crystal-powered rocket launching from the red dunes of Mars",
model="grok-imagine-video-1.5",
duration=10,
aspect_ratio="16:9",
resolution="720p",
)
print(response.url)
Result URLs are temporary. Download the file if you need to keep it.
Pitfalls
- Stop the poll on
done,expired, orfailed. Apendingbody has no usablevideo.url. - Output URLs expire. Save the bytes.
- Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.