TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/phpaisdk/xai/llms.txt
Use this file to discover all available pages before exploring further.
aisdk/xai package supports image generation through xAI’s POST /images/generations endpoint. You describe what you want with a text prompt, choose an aspect ratio, and the model returns a base64-encoded image you can save directly to disk or process further in your application.
Basic image generation
XAI::image(string $modelId) returns an ImageModelInterface backed by XAIImageModel. The builder collects your options and ->run() submits a single POST /images/generations request.
Available image model
The xAI model catalog currently registers one image generation model:| Model ID | Status | Capabilities |
|---|---|---|
grok-imagine-image-quality | stable | image_generation, text_input, image_input |
XAI::image(). Unlike the text models there is no wildcard pattern — use the full model ID string.
Builder options
TheGenerate::image() builder exposes the following fluent methods for xAI requests:
->prompt(string $text)
The text description of the image you want to generate. Be specific — include style, subject, lighting, and composition details for best results.
->aspectRatio(string $ratio)
The desired aspect ratio of the output image. This maps directly to the aspect_ratio parameter in the xAI API request. Common values:
| Value | Use case |
|---|---|
'1:1' | Square — app icons, avatars, social posts |
'16:9' | Landscape widescreen — banners, hero images |
'4:3' | Standard landscape — blog headers, thumbnails |
'9:16' | Portrait — mobile screens, story formats |
->count(int $n)
The number of images to generate in a single request. Maps to the n parameter. Defaults to 1.
The response object
->run() returns an ImageResponse object:
| Field | Type | Description |
|---|---|---|
$result->output->base64 | string | The base64-encoded image bytes returned by the API (b64_json format). |
$result->output->save(string $path) | void | Decodes the base64 data and writes it to $path on disk. |
$result->usage->totalTokens | int | Total tokens consumed by the request (prompt + generation). |
The xAI image API always returns images in
b64_json format. The SDK sets "response_format": "b64_json" automatically — you do not need to specify this yourself. URL-based responses are not supported by this provider.Provider-specific options
Pass extra parameters directly to the xAI API using->providerOptions(). The raw key is merged into the request body as-is, letting you access xAI-specific fields not covered by the portable builder API.
For example, to request a higher-resolution output:
"resolution": "2k" to the JSON body before the request is sent.
API endpoint
All image generation requests are sent to:https://api.x.ai/v1, giving a full endpoint of https://api.x.ai/v1/images/generations.
A representative request body looks like this:
size parameter is not sent — xAI uses aspect_ratio instead. The seed parameter is also omitted. Both of these SDK-level parameters are intentionally suppressed by XAIImageModel so the request shape matches what the xAI API expects.