API / generate-a-still-with-imagine-api

API

Generate a still with the Imagine Image 2.0 API

Generate a still with the Imagine Image 2.0 API

Consumer Quality Mode on grok.com/imagine is the same Image 2.0 family. In the API the model id is grok-imagine-image-2.0. Get a key at console.x.ai. URLs in the response are temporary; download them.

Minimal call

import os
import xai_sdk

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

response = client.image.sample(
    prompt="A concert poster for a synthwave band, bold retro typography, sharp small print",
    model="grok-imagine-image-2.0",
)

print(response.url)

REST equivalent: POST https://api.x.ai/v1/images/generations with model and prompt.

Knobs that matter

  • n: 1–10 stills in one request. Same prompt, several takes. Use sample_batch(n=...) in the xAI Python SDK.
  • aspect_ratio: 1:1, 16:9, 9:16, 4:3, 3:4, 3:2, 2:3, 2:1, 1:2, plus the wide phone ratios. Omit it and the model picks (auto).
  • resolution: 1k (default) or 2k.
  • quality: low or medium on grok-imagine-image-2.0. Default medium.
  • response_format: url (default, temporary) or b64_json when you want bytes.

Pitfalls

  • Different prompts in parallel belong in separate requests (AsyncClient + asyncio.gather). Same prompt, several takes, belongs in n.
  • Check response.respect_moderation before you save the file.
  • Do not paste the API key into a chat log or a public repo.