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.
🔗 Related Links
| Resource | URL |
|---|---|
| API base URL | https://xiaomai.win |
| Console / model marketplace | xiaomai.win |
| Create a key | Create an API Key |
| Video API | Video 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_urlin the official OpenAI SDK - Several ways to send images: file upload (multipart), image URLs, base64 data URLs
- Media hosting: upload local images to
/v1/uploadsand 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
| Purpose | Method and path | Body format |
|---|---|---|
| Image generation (text-to-image) | POST /v1/images/generations | JSON |
| Image editing (image-to-image) | POST /v1/images/edits | multipart/form-data or JSON |
| Async image generation (submit) | POST /v1/images/async/generations | JSON |
| Async image editing (submit) | POST /v1/images/async/edits | JSON only (images as URLs) |
| Async task (result) | GET /v1/images/async/{task_id} | — |
| Upload media (get a URL) | POST /v1/uploads | multipart/form-data or JSON |
Every endpoint uses the same key:
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 (
ntoo 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
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
}'$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 300Response Format
Images are in the data array. Each one is either b64_json (base64 image data) or url (an image link):
{
"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
| Field | Required | Description |
|---|---|---|
model | Yes | Model ID, as listed in the console model marketplace |
prompt | Yes | Prompt text, must not be empty |
n | No | Number of images, default 1; the limit depends on the model |
size | No | Image 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
| Model | size format | Notes |
|---|---|---|
| GPT Image models | 1024x1024, 1536x1024, 1024x1536, auto, etc. | Standard OpenAI width x height |
wan2.7-image | 1K, 2K, or widthxheight (such as 1280x1280) | Total pixels ≤ 2048×2048, aspect ratio at most 8:1 |
wan2.7-image-pro | 1K, 2K, 4K, or widthxheight | 4K only for pure text-to-image without sequential mode |
qwen-image-3.0 / -pro | widthxheight 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:
| Format | Best for | Notes |
|---|---|---|
| multipart file upload | Local images | Send the files directly; simplest |
| JSON with URLs | Images that already have public URLs | Turn local images into URLs with /v1/uploads first |
Format 1: multipart File Upload
Upload local images with multipart/form-data:
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 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"
}| Field | Description |
|---|---|
image | Image file to edit; repeat image[] for several |
mask | Optional mask PNG; transparent areas mark what to change |
model / prompt / n / size | Same 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:
| Model | images format |
|---|---|
| GPT Image models | Array of objects: [{"image_url": "https://..."}] (official OpenAI JSON format); image_url can also be data:image/png;base64,... |
| Wan / Qwen | Array of strings: ["https://..."]; data:image/png;base64,... also works |
GPT Image example:
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"
}'$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 300Wan / Qwen example:
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
}'$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
| Field | Models | Description |
|---|---|---|
negative_prompt | Wan / Qwen | Negative prompt: what should not appear |
prompt_extend | Wan / Qwen | Smart prompt rewriting, true / false |
watermark | Wan / Qwen | Whether to add a watermark |
seed | Wan / Qwen | Random seed; fix it for more stable results |
enable_sequential | Wan 2.7 | Sequential mode: one coherent set of images, n up to 12 |
stream: trueis not supported; the result returns once all images are generatedresponse_formataccepts onlyurlorb64_json
📤 Media Upload /v1/uploads
Want the JSON format but your images are local? Upload them to Xiaomai API first to get HTTPS URLs:
# 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 7+
Invoke-RestMethod -Uri "https://xiaomai.win/v1/uploads" -Method Post `
-Headers @{ Authorization = "Bearer sk-xxxx" } `
-Form @{ file = Get-Item ".\photo.png" }Example response:
{
"id": "xxxxx",
"url": "https://media.xiaomai.win/uploads/xxxxx.png",
"mime": "image/png",
"size": 245760,
"expires_at": 1795259200
}| Item | Rule |
|---|---|
| Size | Up to 100 MB per file; larger files return 413 |
| Image types | png / jpg / gif / webp / bmp (video and audio are supported too, see the Video API) |
| Retention | Deleted automatically after 72 hours |
| Cost | Uploads are free |
| Delete | DELETE /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 onmedia.xiaomai.win stream: trueis 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:
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
}'$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:
{
"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:
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"
}'$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:
{
"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
| Item | Rule |
|---|---|
| Body format | Content-Type: application/json only; multipart file uploads are not supported and return 400 |
| Body size | At most 1 MB; larger bodies return 400 |
| Input images | In images (or image), as http(s) URLs; at least 1 |
| Mask | In mask (a URL, or {"image_url": "..."}), also an http(s) URL |
| Not accepted | data: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
curl https://xiaomai.win/v1/images/async/imgtask_xxxxx -H "Authorization: Bearer sk-xxxx"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):
{
"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:
{
"id": "imgtask_xxxxx",
"object": "image.generation.task",
"status": "failed",
"error": {
"code": "xxxxx",
"message": "failure reason"
}
}| Item | Description |
|---|---|
| Image links | Results of every model are re-hosted on media.xiaomai.win for 72 hours; download and save them before then |
| Task retention | A finished task is kept for 72 hours (see expires_at); after that, queries return 404 |
| Billing | Same as the sync endpoint: charged only on success; a failed task is not charged |
| Parameter errors | stream, 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 |
| Access | You 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.
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)
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 withrequests.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,
ntoo 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.