Image Generations
POST
https://api.x.ai/v1/images/generations
Parameters
n
: Number of image(s) to generate (1-10, default to 1)response_format
: "url"
or "b64_json"
. If "url"
is specified, the response will return a url to the image(s) in data[index].url
; if "b64_json" is specified, the response will return the image(s) in base64 encoded format in data[index].b64_json
.Note: quality
,size
orstyle
are not supported by xAI API at the moment.
Generate image
https://api.x.ai/v1/images/generations
from the chat and image-understanding models that share https://api.x.ai/v1/chat/completions
. The endpoint is compatible with OpenAI SDK (but not with Anthropic SDK), so you can keep using the same base_url
of https://api.x.ai/v1
."model": "grok-2-image"
in the request body to use the model. The generated image will be in jpg
format.curl -X 'POST' https://api.x.ai/v1/images/generations \ -H 'accept: application/json' \ -H 'Authorization: Bearer <API_KEY>' \ -H 'Content-Type: application/json' \ -d '{ -d '{ "model": "grok-2-image", "prompt": "A cat in a tree" }'
Base 64 JSON Output
response_format
parameter to "b64_json"
.
b64_json
field instead of url
in the response image object.Generating multiple images
n
in your request body. For example, to generate four images:
Revised prompt
{
"data": [
{
"b64_json": "data:image/png;base64,...",
"revised_prompt": "..."
}
]
}
revised_prompt
to the user.
Input/Output | Example |
---|---|
prompt (in request body) | A cat in a tree |
revised_prompt (in response body) | 3D render of a gray cat with green eyes perched on a thick branch of a leafy tree, set in a suburban backyard during the day. The cat's fur is slightly ruffled by a gentle breeze, and it is looking directly at the viewer. The background features a sunny sky with a few clouds and other trees, creating a natural and serene environment. The scene is focused on the cat, with no distracting foreground elements, ensuring the cat remains the central subject of the image. |
Request Request Example
Shell
JavaScript
Java
Swift
curl --location --request POST 'https://api.x.ai/v1/images/generations' \
--header 'Authorization: Bearer ' \
--header 'Content-Type: application/json' \
--data-raw '{
-d {'
Response Response Example
{}
Request
Header Params
Authorization
string
required
Example:
Bearer {{XAI_API_KEY}}
Content-Type
string
required
Example:
application/json
Body Params text/plain
Responses
Modified at 2025-04-10 09:04:21