SPB Git

Repository Public

imagine.md 7.9 KB 216 lines markdown

1<!-- source: https://docs.x.ai/developers/model-capabilities/imagine (from https://docs.x.ai/llms-full.txt, retrieved 2026-09-19T05:28:55Z) -->2#### Model Capabilities34# Imagine Overview56The Imagine API lets you generate and edit images and videos with Grok Imagine models. Use it for image generation, image editing with up to 5 reference images, video generation from text or still images, video editing, and more.78## Pricing910Image generation uses flat per-image pricing regardless of prompt length. Each generated image incurs a fixed fee. Image edits are billed for both the input image and the generated output image. Video generation uses per-second pricing where both duration and resolution affect the total cost. For full pricing details, see the [pricing page](/developers/pricing#imagine-api-pricing).1112## Image Generation1314Generate new images from text prompts with Grok Imagine models. Configure output count (up to 10 images per request), aspect ratio, resolution, and response format.1516```python customLanguage="pythonXAI"17import xai_sdk1819client = xai_sdk.Client()2021response = client.image.sample(22    prompt="A collage of London landmarks in a stenciled street‑art style",23    model="grok-imagine-image-2.0",24)2526print(response.url)27```2829```bash30curl -X POST https://api.x.ai/v1/images/generations \31  -H "Content-Type: application/json" \32  -H "Authorization: Bearer $XAI_API_KEY" \33  -d '{34    "model": "grok-imagine-image-2.0",35    "prompt": "A collage of London landmarks in a stenciled street‑art style"36  }'37```3839```python customLanguage="pythonOpenAISDK"40from openai import OpenAI4142client = OpenAI(43    base_url="https://api.x.ai/v1",44    api_key="YOUR_API_KEY",45)4647response = client.images.generate(48    model="grok-imagine-image-2.0",49    prompt="A collage of London landmarks in a stenciled street‑art style",50)5152print(response.data[0].url)53```5455```javascript customLanguage="javascriptAISDK"56import { xai } from "@ai-sdk/xai";57import { generateImage } from "ai";5859const { image } = await generateImage({60    model: xai.image("grok-imagine-image-2.0"),61    prompt: "A collage of London landmarks in a stenciled street‑art style",62});6364console.log(image.base64);65```6667## Image Editing6869Edit a source image with natural language. Provide a public image URL or base64-encoded data URI, then describe the change you want Grok Imagine to apply. Multi-image editing supports up to 5 source images in a single request for combining subjects, transferring styles, and composing scenes.7071```python customLanguage="pythonXAI"72import base6473import xai_sdk7475client = xai_sdk.Client()7677# Load image from file and encode as base6478with open("photo.png", "rb") as f:79    image_data = base64.b64encode(f.read()).decode("utf-8")8081response = client.image.sample(82    prompt="Render this as a pencil sketch with detailed shading",83    model="grok-imagine-image-2.0",84    image_url=f"data:image/png;base64,{image_data}",85)8687print(response.url)88```8990```bash91# Using a public URL as the source image92curl -X POST https://api.x.ai/v1/images/edits \93  -H "Content-Type: application/json" \94  -H "Authorization: Bearer $XAI_API_KEY" \95  -d '{96    "model": "grok-imagine-image-2.0",97    "prompt": "Render this as a pencil sketch with detailed shading",98    "image": {99      "url": "https://docs.x.ai/assets/api-examples/images/style-realistic.png",100      "type": "image_url"101    }102  }'103```104105```javascript customLanguage="javascriptAISDK"106import { xai } from "@ai-sdk/xai";107import { generateImage } from "ai";108import fs from "fs";109110// Load image and encode as base64111const imageBuffer = fs.readFileSync("photo.png");112const base64Image = imageBuffer.toString("base64");113114const { image } = await generateImage({115    model: xai.image("grok-imagine-image-2.0"),116    prompt: {117        text: "Render this as a pencil sketch with detailed shading",118        images: [`data:image/png;base64,${base64Image}`],119    },120});121122console.log(image.base64);123```124125## Video Generation126127Animate a still image with a text prompt. The source image becomes the starting point for the generated video. Video requests are asynchronous: start a request, poll with the returned request ID, and use the completed video URL when ready. The xAI SDK and AI SDK handle polling for you.128129```python customLanguage="pythonXAI"130import os131import xai_sdk132133client = xai_sdk.Client(api_key=os.getenv("XAI_API_KEY"))134135response = client.video.generate(136    prompt="Make the water crash down and slowly pan out the camera",137    model="grok-imagine-video-1.5",138    image_url="https://docs.x.ai/assets/api-examples/video/waterfall-still.png",139    duration=12,140)141142print(response.url)143```144145```javascript customLanguage="javascriptAISDK"146import { xai } from "@ai-sdk/xai";147import { experimental_generateVideo as generateVideo } from "ai";148149const result = await generateVideo({150    model: xai.video("grok-imagine-video-1.5"),151    prompt: {152        image: "https://docs.x.ai/assets/api-examples/video/waterfall-still.png",153        text: "Make the water crash down and slowly pan out the camera",154    },155    duration: 12,156});157158const videoUrl = result.providerMetadata?.xai?.videoUrl;159console.log(videoUrl);160```161162```bash163# Start the video generation request164REQUEST_ID=$(curl -s -X POST https://api.x.ai/v1/videos/generations \165  -H "Content-Type: application/json" \166  -H "Authorization: Bearer $XAI_API_KEY" \167  -d '{168    "model": "grok-imagine-video-1.5",169    "prompt": "Make the water crash down and slowly pan out the camera",170    "image": {"url": "https://docs.x.ai/assets/api-examples/video/waterfall-still.png"},171    "duration": 12172  }' | jq -r '.request_id')173174# Poll until the video is ready175while true; do176  RESULT=$(curl -s https://api.x.ai/v1/videos/$REQUEST_ID \177    -H "Authorization: Bearer $XAI_API_KEY")178  STATUS=$(echo "$RESULT" | jq -r '.status')179  if [ "$STATUS" = "done" ]; then180    echo "$RESULT" | jq -r '.video.url'181    break182  elif [ "$STATUS" = "failed" ] || [ "$STATUS" = "expired" ]; then183    echo "Request $STATUS"; echo "$RESULT" | jq .184    break185  fi186  sleep 5187done188```189190## More Capabilities191192Beyond the top use cases above, the Imagine API supports several additional workflows:193194* **[Multi-Image Editing](/developers/model-capabilities/images/multi-image-editing)** — Combine up to 5 source images in a single edit for compositing subjects, transferring styles, and building scenes from multiple references.195* **[Video Generation](/developers/model-capabilities/video/generation)** — Generate videos from text prompts with configurable duration (up to 15s), aspect ratio, and resolution.196* **[Video Editing](/developers/model-capabilities/video/editing)** — Modify an existing video with a text prompt while preserving the rest of the scene.197* **[Reference-to-Video](/developers/model-capabilities/video/reference-to-video)** — Guide a generated video with one or more reference images that influence the output without forcing the first frame.198* **[Video Extension](/developers/model-capabilities/video/extension)** — Continue an existing video from its last frame, combining the original and extension into one clip.199* **[Files API Integration](/developers/model-capabilities/imagine/files)** — Reference stored files as Imagine inputs by ID, persist generated assets to the Files API, and optionally create a permanent shareable public URL — all in a single request.200201## Enterprise Compliance & Security202203The Imagine APIs are built for production workloads with strict security and compliance requirements. Generated media is subject to content policy review and is not used for training.204205* **SOC 2 Type II** — Audited controls for security, availability, and confidentiality206207* **HIPAA Eligible** — BAA available for healthcare applications handling PHI208209* **GDPR Compliant** — Data processing agreements and EU data residency options210211* **Data Residency** — Regional processing for compliance requirements212213* **High Availability** — Multi-region infrastructure with custom SLAs for enterprise workloads214215* **SSO & RBAC** — SAML SSO, role-based access, and audit logging216
Files main
Menu

Theme