gRPC API

Image

xai_api.Image

An API service for interaction with image generation models.

Methods of xai_api.Image

GenerateImage

unary

Create an image based on a text prompt and optionally another image.

Generate an image

import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

response = client.image.sample(
    prompt="A collage of London landmarks in a stenciled street-art style",
    model="grok-imagine-image-2.0",
)

print(response.url)

Edit an image

import os
import xai_sdk

client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))

response = client.image.sample(
    prompt="Render this as a pencil sketch with detailed shading",
    model="grok-imagine-image-2.0",
    image_url="https://docs.x.ai/assets/api-examples/images/style-realistic.png",
)

print(response.url)

Last updated: September 2, 2026