Skip to main content

Documentation 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.

The 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

use AiSdk\Generate;
use AiSdk\XAI;

XAI::create(['apiKey' => 'xai-...']);

$result = Generate::image()
    ->model(XAI::image('grok-imagine-image-quality'))
    ->prompt('A clean app icon for a PHP AI SDK')
    ->aspectRatio('1:1')
    ->run();

$result->output->save(__DIR__.'/icon.png');
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 IDStatusCapabilities
grok-imagine-image-qualitystableimage_generation, text_input, image_input
Pass this exact identifier to XAI::image(). Unlike the text models there is no wildcard pattern — use the full model ID string.

Builder options

The Generate::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.
->prompt('A minimalist illustration of a PHP elephant holding a glowing neural network')

->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:
ValueUse 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
->aspectRatio('16:9')

->count(int $n)

The number of images to generate in a single request. Maps to the n parameter. Defaults to 1.
->count(4)

The response object

->run() returns an ImageResponse object:
FieldTypeDescription
$result->output->base64stringThe base64-encoded image bytes returned by the API (b64_json format).
$result->output->save(string $path)voidDecodes the base64 data and writes it to $path on disk.
$result->usage->totalTokensintTotal tokens consumed by the request (prompt + generation).
$result = Generate::image()
    ->model(XAI::image('grok-imagine-image-quality'))
    ->prompt('A sunset over mountains')
    ->run();

// Save to disk
$result->output->save('/var/www/images/sunset.png');

// Or work with the raw base64 string
$imageData = base64_decode($result->output->base64);

echo $result->usage->totalTokens; // e.g. 14
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:
$result = Generate::image()
    ->model(XAI::image('grok-imagine-image-quality'))
    ->prompt('A futuristic skyline')
    ->aspectRatio('16:9')
    ->providerOptions('xai', ['raw' => ['resolution' => '2k']])
    ->run();
This adds "resolution": "2k" to the JSON body before the request is sent.

API endpoint

All image generation requests are sent to:
POST {baseUrl}/images/generations
The default base URL is https://api.x.ai/v1, giving a full endpoint of https://api.x.ai/v1/images/generations. A representative request body looks like this:
{
  "model": "grok-imagine-image-quality",
  "prompt": "A clean app icon for a PHP AI SDK",
  "n": 1,
  "response_format": "b64_json",
  "aspect_ratio": "1:1"
}
Note that the 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.

Build docs developers (and LLMs) love