When working with xAI’s image generation capabilities via the Grok API (grok-imagine-image-2.0 and related models), developers have powerful options to control how outputs are dimensioned and delivered.
Key Configuration Parameters
1. aspect_ratio
The aspect_ratio parameter dictates the geometric proportions of your generated canvas.
- Default Behavior: When omitted, the parameter defaults to
auto, allowing the model to intelligently select the optimal aspect ratio based on the descriptive context of your prompt. - Supported Options: The API supports a wide array of formats tailored for different use cases (such as square
1:1, widescreen16:9, vertical mobile views9:16,4:3,3:4,2:1, and more).
2. images & Batch Generation (n)
You can scale up production by requesting multiple images in a single API call.
- Using the
nparameter, you can generate anywhere from 1 to 10 images per request. - In environments utilizing OpenAI-compatible REST implementations,
ndefaults to 1 if left unspecified.
3. response_format (b64_json vs. url)
By default, the API returns temporary hosted URLs (url) pointing to the generated assets. However, depending on your application architecture, handling links that expire can introduce overhead.
By setting response_format to b64_json, the API bypasses temporary URLs entirely. Instead, it returns the raw image payload encoded directly in base64 format inside the response body (data[index].b64_json). This is especially useful if you need to:
- Embed images directly into document streams, databases, or frontend components without executing a secondary download step.
- Maintain strict payload self-containment without relying on external file hosting lifecycles.
Example Request Payload
Below is an example of how a standard generation call looks when configuring custom aspect ratios and requesting base64-encoded JSON output:
{
"model": "grok-imagine-image-2.0",
"prompt": "A cyberpunk street market glowing in neon rain, cinematic lighting",
"aspect_ratio": "16:9",
"n": 1,
"response_format": "b64_json"
}
Leave a Reply