Cheaper Gen.
xAITutorial

How to Use Grok Imagine Image 2.0 API

A step-by-step Grok Imagine Image 2.0 API tutorial with Python, Node.js and cURL: submit a text to image job, poll it or receive a webhook, and download the image. The example below costs $0.022.

Endpoints
2
Text to Image · Image Edit
Example request
$0.022
Text to Image
Languages
3
Python · Node.js · cURL
Failed runs
$0
refunded automatically
1

Get an API key

Create an account, add credits (from $10) and create a key in Dashboard → API keys. Store it as CHEAPERGEN_API_KEY on your server. Every request sends it as a bearer token.

How to get a Grok Imagine Image 2.0 API key
2

Choose a Grok Imagine Image 2.0 endpoint

Each task has its own endpoint. All of them take a JSON body and return a generation you can poll.

Grok Imagine Image 2.0 API endpoints, required inputs and prices
TaskEndpointRequiredFrom
Text to Imagexai/grok-imagine-image/text-to-imageprompt$0.022/image
Image Editxai/grok-imagine-image/editprompt, image_urls$0.022/image
3

Submit a text to image job

This program submits the job, polls every 3 seconds until it finishes and prints the image URL. Swap the prompt and settings for your own.

import os, time, requests

API = "https://cheapergen.com/api/v1"
headers = {"Authorization": f"Bearer {os.environ['CHEAPERGEN_API_KEY']}"}

# 1. Submit the job
job = requests.post(f"{API}/run/xai/grok-imagine-image/text-to-image", headers=headers, json={
    "prompt": "Fashion editorial: a model in a sculptural orange coat on a salt flat at noon, harsh shadows, medium format",
    "aspect_ratio": "1:1",
    "resolution": "1K",
    "quality": "medium"
}).json()

# 2. Poll until it finishes (or pass "webhook_url" and skip polling)
while job["status"] in ("queued", "running"):
    time.sleep(3)
    job = requests.get(f"{API}/generations/{job['id']}", headers=headers).json()

print(job["status"], job["output"]["images"][0]["url"] if job["status"] == "succeeded" else job["error"])
4

Set the parameters that matter

The settings for xai/grok-imagine-image/text-to-image, with what each one does to the price.

Grok Imagine Image 2.0 Text to Image parameters and their effect on price
ParameterType · defaultWhat it doesPrice effect
aspect_ratiostring (enum) · 1:1Output image shape.
1:1 · 3:4 · 4:3 · 9:16 · 16:9 · 2:3 · 3:2 · 1:2 · 2:1 · 9:19.5 · 19.5:9 · 9:20 · 20:9
Yes
resolutionstring (enum) · 1K1K ≈ 1 MP, 2K ≈ 4 MP.
1K · 2K
1K: $0.022/image
2K: $0.0528/image
qualitystring (enum) · mediumLow is faster and cheaper; medium has more detail. (Edits always run at medium.)
low · medium
Low: $0.022/image
Medium: $0.022/image

Other Grok Imagine Image 2.0 endpoints take the same shape with their own inputs:

POST /api/v1/run/xai/grok-imagine-image/edit

{
  "prompt": "Put the person from image 1 in the jacket from image 2 on a rainy Tokyo street at night",
  "image_urls": [
    "https://cdn.cheapergen.com/samples/portrait.jpg"
  ],
  "aspect_ratio": "1:1",
  "resolution": "1K"
}
5

Get the result

A job moves from queued to running to succeeded, failed or canceled. Poll GET /api/v1/generations/{id}, or pass webhook_url and we POST the result to you. Download the output and store it yourself.

200 — succeeded
{
  "id": "gen_8c2QmX1nq0k4",
  "object": "generation",
  "model": "xai/grok-imagine-image",
  "endpoint": "xai/grok-imagine-image/text-to-image",
  "mode": "text-to-image",
  "status": "succeeded",
  "input": {
    "prompt": "Fashion editorial: a model in a sculptural orange coat on a salt flat at noon, harsh shadows, medium format",
    "aspect_ratio": "1:1",
    "resolution": "1K",
    "quality": "medium"
  },
  "output": {
    "images": [
      {
        "url": "https://cdn.cheapergen.com/outputs/gen_8c2Q-0.png",
        "content_type": "image/png",
        "width": 2048,
        "height": 2048
      }
    ]
  },
  "error": null,
  "cost": {
    "amount_usd": 0.2255,
    "currency": "usd",
    "estimated": false,
    "official_usd": 0.264,
    "saved_usd": 0.0385
  },
  "source": "api",
  "created_at": "2026-10-15T09:21:04Z",
  "started_at": "2026-10-15T09:21:06Z",
  "completed_at": "2026-10-15T09:22:31Z"
}
Verify webhook signatures
6

Price a request before you run it

The quote endpoint is free and needs no key. It returns the price, the official price and your discount for exactly the input you send.

curl -X POST https://cheapergen.com/api/v1/quote/xai/grok-imagine-image/text-to-image \
  -H "Content-Type: application/json" \
  -d '{"prompt":"Fashion editorial: a model in a sculptural orange coat on a salt flat at noon, harsh shadows, medium format","aspect_ratio":"1:1","resolution":"1K","quality":"medium"}'
API errors and what to do
StatusCodeMeaning
400invalid_requestA parameter is missing or out of range — the message names it.
401unauthorizedMissing, malformed or revoked API key.
402insufficient_balanceNot enough credits for the quoted price; top up or enable auto top-up.
429rate_limitedToo many requests or generations in progress; back off and retry.
502upstream_errorThe model provider failed; the hold is refunded and you can retry.
503model_unavailableNo healthy route right now; retry later or pick another model.
Full Grok Imagine Image 2.0 API documentation

FAQ

Grok Imagine Image 2.0 API questions

No. The API is plain HTTPS with JSON, so the Python (requests) and Node.js (fetch) examples on this page are complete programs. Any language that can send an HTTP request works.

Yes. Add "webhook_url" to the request body and we POST the finished generation to it, signed with a CheaperGen-Signature header (HMAC-SHA256 of the timestamp and raw body). Polling GET /api/v1/generations/{id} works too.

Pass public URLs in "image_urls". Files without a public URL can be uploaded first with POST /api/v1/uploads, which returns a URL to use in the request.

Text to Image starts at $0.022/image; the example request on this page costs $0.022, 63% below the official price. Failed generations are refunded automatically.