Skip to content

Image Generation and Editing API Guide ​

Call GPT Image, Wan, and Qwen image models with one API key, all through the OpenAI Images format.

📋 Overview

Xiaomai API offers image generation (text-to-image) and image editing (image-to-image) endpoints. The standard endpoints are synchronous: the response arrives once the images are ready. Generation and editing also each have an asynchronous endpoint: you get a task ID right away and poll for the result, so a dropped connection loses nothing.

⚠️ Supported Environment

Anything that can send HTTP requests works: Windows (PowerShell), macOS / Linux (curl), Python, Node.js, and so on.

🚨 Common Pitfall: Use an image-video Group Key

Image models are only available in the image-video group. A key from another group (such as Claude-Code-api or General) gets a "no access" or "model not found" error. Create a dedicated key in the image-video group first.

ResourceURL
API base URLhttps://xiaomai.win
Console / model marketplacexiaomai.win
Create a keyCreate an API Key
Video APIVideo Generation API Guide

✨ Features ​

  • Two endpoints for every image model: generate with /v1/images/generations, edit with /v1/images/edits
  • OpenAI SDK compatible: just change base_url in the official OpenAI SDK
  • Several ways to send images: file upload (multipart), image URLs, base64 data URLs
  • Media hosting: upload local images to /v1/uploads and get HTTPS URLs back
  • Sync or async: the sync endpoint returns the result in one request; the async endpoint returns a task ID to poll, for unstable networks or slow generations

🛠️ Endpoints ​

PurposeMethod and pathBody format
Image generation (text-to-image)POST /v1/images/generationsJSON
Image editing (image-to-image)POST /v1/images/editsmultipart/form-data or JSON
Async image generation (submit)POST /v1/images/async/generationsJSON
Async image editing (submit)POST /v1/images/async/editsJSON only (images as URLs)
Async task (result)GET /v1/images/async/{task_id}—
Upload media (get a URL)POST /v1/uploadsmultipart/form-data or JSON

Every endpoint uses the same key:

http
Authorization: Bearer sk-xxxx

⚠️ Use a Long Timeout

Images are generated synchronously, so one request can take from tens of seconds to a few minutes. Set the client timeout to 300 seconds, or the client may disconnect before the image is ready. If your connection drops often, use the async endpoints instead.

🎨 Choosing a Model ​

The available image models change over time, so this page lists no models or prices; check the console model marketplace for the current model IDs, billing methods, and live prices. The actual charge of each request is in the usage logs. All image models are in the image-video group.

  • GPT Image models (such as gpt-image-2): generate and edit; edits take multipart file uploads or JSON URLs
  • Wan / Qwen models (such as wan2.7-image, qwen-image-3.0): generate and edit; the number of images and reference images is capped per model
  • Parameters outside the model's limits (n too large, too many reference images, unsupported size) return 400 with the reason, and nothing is charged

💡 A Larger n Costs More

Most models bill per output image, so n: 4 costs four images. Without n, one image is generated.

⚙️ Image Generation /v1/images/generations ​

Request Example ​

bash
curl https://xiaomai.win/v1/images/generations \
  -H "Authorization: Bearer sk-xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "An orange cat sitting on a windowsill watching the rain, watercolor style",
    "size": "1024x1024",
    "n": 1
  }'
powershell
$headers = @{ Authorization = "Bearer sk-xxxx" }
$body = @{
  model  = "gpt-image-2"
  prompt = "An orange cat sitting on a windowsill watching the rain, watercolor style"
  size   = "1024x1024"
  n      = 1
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/generations" -Method Post `
  -Headers $headers -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) -TimeoutSec 300

Response Format ​

Images are in the data array. Each one is either b64_json (base64 image data) or url (an image link):

json
{
  "created": 1795000000,
  "data": [
    { "b64_json": "iVBORw0KGgo..." }
  ]
}
  • GPT Image models: usually return b64_json; decode it and save it as a file
  • Wan / Qwen: usually return url. The link expires, so download and save the image promptly

Common Fields ​

FieldRequiredDescription
modelYesModel ID, as listed in the console model marketplace
promptYesPrompt text, must not be empty
nNoNumber of images, default 1; the limit depends on the model
sizeNoImage size; see "Image Sizes" below

GPT Image models also accept the official OpenAI fields such as quality, background, and output_format. Extra Wan / Qwen fields are listed under "Wan / Qwen Notes" below.

Image Sizes ​

Modelsize formatNotes
GPT Image models1024x1024, 1536x1024, 1024x1536, auto, etc.Standard OpenAI width x height
wan2.7-image1K, 2K, or widthxheight (such as 1280x1280)Total pixels ≤ 2048×2048, aspect ratio at most 8:1
wan2.7-image-pro1K, 2K, 4K, or widthxheight4K only for pure text-to-image without sequential mode
qwen-image-3.0 / -prowidthxheight only (such as 1328x1328)Total pixels between 512×512 and 2048×2048, aspect ratio at most 8:1; 1K / 2K are not accepted

The x in widthxheight can also be * (such as 1280*1280).

✏️ Image Editing /v1/images/edits ​

The edit endpoint accepts two formats, for every model. Pick the one that fits your images:

FormatBest forNotes
multipart file uploadLocal imagesSend the files directly; simplest
JSON with URLsImages that already have public URLsTurn local images into URLs with /v1/uploads first

Format 1: multipart File Upload ​

Upload local images with multipart/form-data:

bash
curl https://xiaomai.win/v1/images/edits \
  -H "Authorization: Bearer sk-xxxx" \
  -F "model=gpt-image-2" \
  -F "prompt=Replace the background with a beach sunset, keep the person unchanged" \
  -F "[email protected]" \
  -F "size=1024x1024"

# Several reference images: use the field name image[]
curl https://xiaomai.win/v1/images/edits \
  -H "Authorization: Bearer sk-xxxx" \
  -F "model=gpt-image-2" \
  -F "prompt=Place the person from the first image into the scene of the second" \
  -F "image[][email protected]" \
  -F "image[][email protected]"
powershell
# PowerShell 7+
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/edits" -Method Post `
  -Headers @{ Authorization = "Bearer sk-xxxx" } -TimeoutSec 300 `
  -Form @{
    model  = "gpt-image-2"
    prompt = "Replace the background with a beach sunset, keep the person unchanged"
    image  = Get-Item ".\photo.png"
    size   = "1024x1024"
  }
FieldDescription
imageImage file to edit; repeat image[] for several
maskOptional mask PNG; transparent areas mark what to change
model / prompt / n / sizeSame as image generation

For Wan / Qwen multipart uploads, use the field name image (or image[0], image[1], …), at most 10 MB per image.

Format 2: JSON with Image URLs ​

The images format differs by model:

Modelimages format
GPT Image modelsArray of objects: [{"image_url": "https://..."}] (official OpenAI JSON format); image_url can also be data:image/png;base64,...
Wan / QwenArray of strings: ["https://..."]; data:image/png;base64,... also works

GPT Image example:

bash
curl https://xiaomai.win/v1/images/edits \
  -H "Authorization: Bearer sk-xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "gpt-image-2",
    "prompt": "Replace the background with a beach sunset, keep the person unchanged",
    "images": [
      { "image_url": "https://media.xiaomai.win/uploads/photo.png" }
    ],
    "size": "1024x1024"
  }'
powershell
$headers = @{ Authorization = "Bearer sk-xxxx" }
$body = @{
  model  = "gpt-image-2"
  prompt = "Replace the background with a beach sunset, keep the person unchanged"
  images = @(@{ image_url = "https://media.xiaomai.win/uploads/photo.png" })
  size   = "1024x1024"
} | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/edits" -Method Post `
  -Headers $headers -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) -TimeoutSec 300

Wan / Qwen example:

bash
curl https://xiaomai.win/v1/images/edits \
  -H "Authorization: Bearer sk-xxxx" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "wan2.7-image",
    "prompt": "The model in the first image wears the coat from the second image, street style",
    "images": [
      "https://media.xiaomai.win/uploads/model.jpg",
      "https://media.xiaomai.win/uploads/coat.jpg"
    ],
    "size": "2K",
    "n": 1
  }'
powershell
$headers = @{ Authorization = "Bearer sk-xxxx" }
$body = @{
  model  = "wan2.7-image"
  prompt = "The model in the first image wears the coat from the second image, street style"
  images = @(
    "https://media.xiaomai.win/uploads/model.jpg",
    "https://media.xiaomai.win/uploads/coat.jpg"
  )
  size   = "2K"
  n      = 1
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/edits" -Method Post `
  -Headers $headers -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body)) -TimeoutSec 300
  • The edit endpoint needs at least 1 image, otherwise it returns 400

Wan / Qwen Notes ​

FieldModelsDescription
negative_promptWan / QwenNegative prompt: what should not appear
prompt_extendWan / QwenSmart prompt rewriting, true / false
watermarkWan / QwenWhether to add a watermark
seedWan / QwenRandom seed; fix it for more stable results
enable_sequentialWan 2.7Sequential mode: one coherent set of images, n up to 12
  • stream: true is not supported; the result returns once all images are generated
  • response_format accepts only url or b64_json

📤 Media Upload /v1/uploads ​

Want the JSON format but your images are local? Upload them to Xiaomai API first to get HTTPS URLs:

bash
# Option 1: send the file (field name: file / image)
curl https://xiaomai.win/v1/uploads \
  -H "Authorization: Bearer sk-xxxx" \
  -F "[email protected]"

# Option 2: send a data URL
curl https://xiaomai.win/v1/uploads \
  -H "Authorization: Bearer sk-xxxx" \
  -H "Content-Type: application/json" \
  -d '{"data_url": "data:image/png;base64,iVBORw0KGgo..."}'
powershell
# PowerShell 7+
Invoke-RestMethod -Uri "https://xiaomai.win/v1/uploads" -Method Post `
  -Headers @{ Authorization = "Bearer sk-xxxx" } `
  -Form @{ file = Get-Item ".\photo.png" }

Example response:

json
{
  "id": "xxxxx",
  "url": "https://media.xiaomai.win/uploads/xxxxx.png",
  "mime": "image/png",
  "size": 245760,
  "expires_at": 1795259200
}
ItemRule
SizeUp to 100 MB per file; larger files return 413
Image typespng / jpg / gif / webp / bmp (video and audio are supported too, see the Video API)
RetentionDeleted automatically after 72 hours
CostUploads are free
DeleteDELETE /v1/uploads/{id}; you can only delete your own files

⚠️ Use the Returned url As Is

The url ends with the real file extension. Copy it exactly into your request; do not build it yourself or drop the extension.

⏳ Async Generation and Editing ​

The sync endpoint keeps the connection open until the image is ready, so a network hiccup can cut it off. The async endpoint splits this into two steps: submit and get a task ID at once, then check the result every few seconds. Losing the connection while the image is generated does no harm.

💡 Differences from the Sync Endpoint

  • Nearly the same request body: model, prompt, size, n, and so on are written as for the sync endpoints; every image model works
  • Async editing takes JSON with image URLs only; multipart file uploads are not supported. See "Async Editing Rules" below
  • Results always come back as url, re-hosted on media.xiaomai.win
  • stream: true is not supported and returns 400

Step 1: Submit a Task ​

Generation task: the body is the same as for image generation; the path is /v1/images/async/generations:

bash
curl https://xiaomai.win/v1/images/async/generations   -H "Authorization: Bearer sk-xxxx"   -H "Content-Type: application/json"   -d '{
    "model": "gpt-image-2",
    "prompt": "An orange cat sitting on a windowsill watching the rain, watercolor style",
    "size": "1024x1024",
    "n": 1
  }'
powershell
$headers = @{ Authorization = "Bearer sk-xxxx" }
$body = @{
  model  = "gpt-image-2"
  prompt = "An orange cat sitting on a windowsill watching the rain, watercolor style"
  size   = "1024x1024"
  n      = 1
} | ConvertTo-Json
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/async/generations" -Method Post `
  -Headers $headers -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

The response only means the task was accepted:

json
{
  "id": "imgtask_xxxxx",
  "object": "image.generation.task",
  "model": "gpt-image-2",
  "status": "queued",
  "created_at": 1795000000
}

Edit task: use the path /v1/images/async/edits and pass the images as URLs in images:

bash
curl https://xiaomai.win/v1/images/async/edits   -H "Authorization: Bearer sk-xxxx"   -H "Content-Type: application/json"   -d '{
    "model": "gpt-image-2",
    "prompt": "Replace the background with a beach sunset, keep the person unchanged",
    "images": [
      { "image_url": "https://media.xiaomai.win/uploads/photo.png" }
    ],
    "size": "1024x1024"
  }'
powershell
$headers = @{ Authorization = "Bearer sk-xxxx" }
$body = @{
  model  = "gpt-image-2"
  prompt = "Replace the background with a beach sunset, keep the person unchanged"
  images = @(@{ image_url = "https://media.xiaomai.win/uploads/photo.png" })
  size   = "1024x1024"
} | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/async/edits" -Method Post `
  -Headers $headers -ContentType "application/json; charset=utf-8" `
  -Body ([System.Text.Encoding]::UTF8.GetBytes($body))

For Wan / Qwen, write images as an array of strings, as with the sync endpoint:

json
{
  "model": "wan2.7-image",
  "prompt": "The model in the first image wears the coat from the second image, street style",
  "images": [
    "https://media.xiaomai.win/uploads/model.jpg",
    "https://media.xiaomai.win/uploads/coat.jpg"
  ],
  "size": "2K",
  "n": 1
}

Edit task IDs start with imgedit_ and their object is image.edit.task; generation task IDs start with imgtask_ and their object is image.generation.task.

Async Editing Rules ​

ItemRule
Body formatContent-Type: application/json only; multipart file uploads are not supported and return 400
Body sizeAt most 1 MB; larger bodies return 400
Input imagesIn images (or image), as http(s) URLs; at least 1
MaskIn mask (a URL, or {"image_url": "..."}), also an http(s) URL
Not accepteddata:image/...;base64 and file_id return 400

💡 Local Images

Upload them with media upload /v1/uploads first, then put the returned https://media.xiaomai.win/uploads/... URLs in images.

Step 2: Get the Result ​

bash
curl https://xiaomai.win/v1/images/async/imgtask_xxxxx   -H "Authorization: Bearer sk-xxxx"
powershell
Invoke-RestMethod -Uri "https://xiaomai.win/v1/images/async/imgtask_xxxxx" `
  -Headers @{ Authorization = "Bearer sk-xxxx" }

Both generation and edit tasks are queried here. Status flow: queued → in_progress → completed or failed. Results usually arrive within tens of seconds; polling about every 5 seconds is enough.

On success, data matches the sync endpoint, except that images always come back as url (a generation task here):

json
{
  "id": "imgtask_xxxxx",
  "object": "image.generation.task",
  "model": "gpt-image-2",
  "status": "completed",
  "created_at": 1795000000,
  "started_at": 1795000000,
  "completed_at": 1795000023,
  "expires_at": 1795259223,
  "data": [
    { "url": "https://media.xiaomai.win/uploads/xxxxx.png" }
  ],
  "usage": { "input_tokens": 10, "output_tokens": 200, "total_tokens": 210 }
}

A failed task carries the reason:

json
{
  "id": "imgtask_xxxxx",
  "object": "image.generation.task",
  "status": "failed",
  "error": {
    "code": "xxxxx",
    "message": "failure reason"
  }
}
ItemDescription
Image linksResults of every model are re-hosted on media.xiaomai.win for 72 hours; download and save them before then
Task retentionA finished task is kept for 72 hours (see expires_at); after that, queries return 404
BillingSame as the sync endpoint: charged only on success; a failed task is not charged
Parameter errorsstream, a non-JSON body, an unavailable model, or edit images that are not URLs fail at submit; other problems (such as an empty prompt or n out of range) show up as a failed task, also not charged
AccessYou can only query tasks of your own account; unknown or foreign task IDs return 404

Full Python Example ​

This is a generation task. For an edit task, change the path to /v1/images/async/edits and add images URLs to body; polling is identical.

python
import time
import requests

BASE = "https://xiaomai.win"
H = {"Authorization": "Bearer sk-xxxx"}  # a key in the image-video group

body = {
    "model": "gpt-image-2",
    "prompt": "An orange cat sitting on a windowsill watching the rain, watercolor style",
    "size": "1024x1024",
}
r = requests.post(BASE + "/v1/images/async/generations", headers=H, json=body, timeout=60)
r.raise_for_status()
task_id = r.json()["id"]
print("task:", task_id)

deadline = time.time() + 30 * 60  # wait up to 30 minutes
while time.time() < deadline:
    time.sleep(5)
    s = requests.get(f"{BASE}/v1/images/async/{task_id}", headers=H, timeout=60).json()
    print(s["status"])
    if s["status"] == "completed":
        for i, img in enumerate(s["data"]):
            with open(f"result_{i}.png", "wb") as f:
                f.write(requests.get(img["url"], timeout=120).content)
        print("saved")
        break
    if s["status"] == "failed":  # not charged
        raise SystemExit(s.get("error", {}).get("message"))
else:
    raise SystemExit("Not finished after 30 minutes; contact support with the task ID")

🐍 Python Example (OpenAI SDK) ​

python
import base64
from openai import OpenAI

client = OpenAI(
    base_url="https://xiaomai.win/v1",  # the OpenAI SDK needs /v1
    api_key="sk-xxxx",  # a key in the image-video group
    timeout=300,
)

# Image generation
result = client.images.generate(
    model="gpt-image-2",
    prompt="An orange cat sitting on a windowsill watching the rain, watercolor style",
    size="1024x1024",
)
with open("cat.png", "wb") as f:
    f.write(base64.b64decode(result.data[0].b64_json))

# Image editing
with open("cat.png", "rb") as image:
    edited = client.images.edit(
        model="gpt-image-2",
        prompt="Put a small red hat on the cat",
        image=image,
    )
with open("cat_hat.png", "wb") as f:
    f.write(base64.b64decode(edited.data[0].b64_json))

⚠️ Notes

  • When the response holds a url (Wan / Qwen), download it with requests.get(url) and save the file. The link expires, so do not keep only the link
  • Parameters outside the model's limits (size out of range, n too large, too many reference images) return 400 with the reason, and nothing is charged
  • Successful requests are charged on the actual result, even if you dislike the image

FAQ ​

"No access to group" or "model not found"? ​

The key is not in the image-video group. Create a new key in that group, or change the existing key's group to image-video. See Update Token Settings.

400 "n must be ..."? ​

n is outside the model's range. The error states the range; lower n accordingly.

400 "size is outside the model's pixel and aspect-ratio limits"? ​

The size is too large, too small, or too narrow. Adjust it per "Image Sizes" above; Qwen models need the widthxheight format.

400 "image is required"? ​

The edit endpoint was called without an image. In multipart, upload a file in the image field; in JSON, list URLs in images (GPT Image uses [{"image_url": "..."}]).

No response for a long time / client timeout? ​

Image generation takes time; set the client timeout to 300 seconds, or switch to the async endpoints if the connection drops often. After a timeout, do not resubmit right away. Check the usage logs for a charge first, and contact support with the time if anything looks wrong.

More Questions ​

See the FAQ or contact support.

Friendly, practical, and reliable AI coding support