mcpField
G
grok-imagine-api

grok-imagine-api

github.com/thatjuanCreative & media148 installsUpdated 3w ago
Repository

About grok-imagine-api

A skill for using xAI’s Grok Imagine image API to generate and edit images through REST or supported SDKs. It covers editing, variations, output formats, and request handling.

Key features

  1. 01Text-to-image generation
  2. 02Single- and multi-image editing
  3. 03Multi-turn refinement and batch variations
  4. 04Aspect-ratio and resolution controls
  5. 05URL and base64 output

Use cases

  1. 01Generate images from text prompts
  2. 02Edit one or more source images
  3. 03Create multiple prompt variations
  4. 04Save generated images as URLs or base64

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/thatjuan/agent-skills --skill grok-imagine-api

After installing, type/grok-imagine-apiin Claude Code to confirm it loaded.

Bundled filesREADME.mdreferences/image-api.md

Details

From the project README

Grok Imagine API

Grok Imagine is xAI's image generation and editing API. It supports text-to-image, JSON-based image edits, multi-image edits, multi-turn refinement, batch variations, aspect-ratio control, 1k/2k resolution, URL output, and base64 output.

Use official xAI docs for exact current model availability and pricing when that matters. Do not recommend the deprecated grok-imagine-image-pro; migrate existing uses to grok-imagine-image-quality or the current documented Grok Imagine image model.

Quick start

Raw REST:

curl -X POST https://api.x.ai/v1/images/generations \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $XAI_API_KEY" \
  -d '{
    "model": "grok-imagine-image",
    "prompt": "A collage of London landmarks in a stenciled street-art style"
  }'

Python xAI SDK:

import xai_sdk

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

OpenAI-compatible generation:

from openai import OpenAI

client = OpenAI(base_url="https://api.x.ai/v1", api_key="YOUR_API_KEY")
response = client.images.generate(
    model="grok-imagine-image",
    prompt="A collage of London landmarks in a stenciled street-art style",
)
print(response.data[0].url)

Integration choices

PathBest forNotes
xai_sdkPython, first-party featuresUse client.image.sample() and sample_batch().
RESTAny language, full controlSend JSON to /images/generations or /images/edits.
OpenAI SDKDrop-in image generationPoint base_url / baseURL at https://api.x.ai/v1.
Vercel AI SDKTypeScript apps already using AI SDKUse xai.image("grok-imagine-image") with generateImage().

The OpenAI SDK's images.edit() is not supported for Grok Imagine edits because it sends multipart/form-data; xAI image edits require application/json.

Core workflow

  1. Choose generation vs editing:
    • New image: POST /v1/images/generations, client.image.sample(), or images.generate().
    • Edit image: POST /v1/images/edits, xAI SDK sample(image_url=...), or Vercel AI SDK provider options.
  2. Set model, prompt, and optional n, aspect_ratio, resolution, or output format.
  3. For image edits, pass a public image URL or a base64 data URI. Up to 5 input images are supported.
  4. Treat generated URLs as temporary. Download them promptly or request base64 output when embedding/saving directly.
  5. Check moderation metadata when using xai_sdk; do not assume every request produces a usable image.

Common patterns

  • Same prompt, multiple variants: use sample_batch(..., n=...) or images.generate(..., n=...).
  • Different prompts in parallel: use xai_sdk.AsyncClient() with asyncio.gather().
  • Single-image edit: output aspect ratio follows the input image.
  • Multi-image edit: output aspect ratio follows the first input unless aspect_ratio is set.
  • Base64 output: xAI SDK uses image_format="base64"; OpenAI-compatible SDKs use response_format="b64_json".
  • Resolution values: 1k or 2k.
  • Maximum images per request: 10.

Reference

See references/image-api.md for endpoint shapes, parameter names by SDK, editing payloads, aspect ratios, response handling, and troubleshooting.

Comments

Markdown supported