API / edit-an-image-with-imagine-api

API

Edit an image with the Imagine API

Edit an image with the Imagine API

POST https://api.x.ai/v1/images/edits takes a source image and a prompt, then returns a new still. Model id is grok-imagine-image-2.0. Get a key and credits at console.x.ai.

Minimal call

Body is JSON: prompt plus an image object with url and type: "image_url". The URL can be public or a data URI. You can also pass a file_id from the Files API.

curl -X POST https://api.x.ai/v1/images/edits \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
  "model": "grok-imagine-image-2.0",
  "prompt": "Render this as a pencil sketch with detailed shading",
  "image": {
    "url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png",
    "type": "image_url"
  }
}'

Python with the official SDK (data URI shown):

import os
from xai_sdk import Client

client = 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="data:image/png;base64,...",
)
print(response.url)

Chain edits by feeding each output URL into the next request. Multi-image editing (up to 3 sources) is a related path on the same docs page.

SDK note

OpenAI SDK images.edit() sends multipart/form-data. xAI wants application/json, so that helper is unsupported. Use the xAI SDK, Vercel AI SDK, or raw HTTP.

Pitfalls

  • Image edits bill for both the input image and the generated output.
  • Response URLs are temporary. Download them before they expire.
  • Do not paste the API key into a chat log or a public repo.
  • Console API credits are a separate bill from SuperGrok's weekly pool on grok.com.