The xAI image generation endpoint (/v1/images/generations) powered by grok-imagine-image-2.0 gives developers predictable controls over framing, generation quality, resolution, and payload delivery. Tuning aspect_ratio, quality, and response_format (b64_json vs. temporary URLs) directly shapes latency, storage handling, and billing.
Endpoint and Core Request Parameters
Send a POST request to [https://api.x.ai/v1/images/generations](https://api.x.ai/v1/images/generations) with a model and prompt, plus optional controls for batch size (n from 1 to 10), resolution (1k default or 2k), framing (aspect_ratio), quality (quality), and payload encoding (response_format).
curl -X POST https://api.x.ai/v1/images/generations
-H "Authorization: Bearer $XAI_API_KEY"
-H "Content-Type: application/json"
-d '{
"model": "grok-imagine-image-2.0",
"prompt": "Two ceramic espresso cups on a pale oak counter in morning light",
"aspect_ratio": "16:9",
"quality": "medium",
"resolution": "2k",
"response_format": "b64_json",
"n": 1
}'
Controlling Framing with aspect_ratio
Omit aspect_ratio—or pass "auto"—to let the model pick the framing that best fits the prompt. Passing an explicit ratio locks the output proportions for fixed UI slots, mobile feeds, or widescreen layouts.
| Ratio | Typical Use Case |
|---|---|
1:1 |
Social media posts, square thumbnails |
16:9 / 9:16 |
Widescreen video/headers, vertical mobile stories |
4:3 / 3:4 |
Slide presentations, portraits |
19.5:9 / 9:19.5 |
Modern iPhone-style displays |
20:9 / 9:20 |
Modern Android-style displays |
21:9 |
Cinematic widescreen |
5:2 |
Wide banners and headers |
auto |
Model selects the best ratio from the prompt (default when omitted) |
When using the conversational image_generation tool rather than /v1/images/generations, the model chooses the ratio automatically unless you state the desired ratio inside the prompt text.
Tuning quality and resolution
The quality parameter accepts low, medium, or auto on grok-imagine-image-2.0.
- Default
autobehavior: When omitted or set toauto, the service chooses the quality per request—currently usinglowfor image generation andmediumfor image editing. - Explicit pinning & billing: Images are billed at the quality level they are served at. Pass
medium(paired with"resolution": "2k"when needed) when fine textures, small objects, or crisp details must remain clearly recognizable. Useloworautofor faster drafts and exploratory batch runs.
Receiving Payloads with b64_json
By default, response_format is "url", returning temporary hosted image URLs that should be downloaded or processed promptly. Setting "response_format": "b64_json" returns base64-encoded image data directly in data[0].b64_json.
Choose b64_json when:
- You want to persist or process image bytes immediately without a second HTTP download step or expiration risk.
- You are embedding or forwarding image payloads directly in an application pipeline.
Keep two operational caveats in mind:
- Deferred requests: Asynchronous
deferred: truerequests supportresponse_format: "url"only (retained for 24 hours), so omitb64_jsonwhen using deferred mode. - Files API alternative: For durable storage or reusable file IDs across requests, you can also persist outputs through the xAI Files API.
Leave a Reply