Async tasks & status¶
Submit a job, get a task ID right away, and check its status when it suits you. This avoids long-lived HTTP connections and works well for 4K images, queues and batch processing.
1. Submit a task¶
The request body is the same as for synchronous generation. The response returns immediately:
{
"id": "task_CXdAjyIiOqK75tQUnSdNdExipZYe0Qy2",
"object": "image.generation.task",
"model": "gpt-image-2",
"status": "queued",
"progress": 0,
"created_at": 1796107800
}
The price is reserved when the task is accepted. If the task fails, it is refunded automatically. See Billing & refunds.
2. Check the task¶
{
"id": "task_CXdAjyIiOqK75tQUnSdNdExipZYe0Qy2",
"object": "image.generation.task",
"model": "gpt-image-2",
"status": "completed",
"progress": 100,
"created_at": 1796107800,
"completed_at": 1796107868,
"data": [
{ "url": "https://api.yexapi.com/v1/images/files/01M8R5W41Q93GQT3FJ8RAWPYZV/1796107836_b98922a5.png" }
]
}
Status values¶
status |
Meaning | Final? |
|---|---|---|
queued |
Accepted and waiting to start | no |
in_progress |
Generating; progress is 0–100 |
no |
completed |
Done; image links are in data[].url |
yes |
failed |
Generation failed; see error. The charge is refunded. |
yes |
A failed task looks like this:
{
"id": "task_ciPK5oiw6LycJlTQeOECq3DnArbTSZAH",
"object": "image.generation.task",
"model": "gpt-image-2",
"status": "failed",
"progress": 100,
"created_at": 1796107903,
"completed_at": 1796107928,
"error": {
"code": "image_generation_failed",
"message": "image generation failed: 输入参数无效. 请检查请求参数后重试."
}
}
Note
error.message is a human-readable reason and its wording may change
(some reasons are currently returned in Chinese). Use status for
program logic.
Polling guidance¶
- Wait about 5 seconds before the first check, then poll every 5–10 seconds. Most 1K/2K images finish within 30–60 seconds; 4K can take longer.
- Tasks can only be read with a key from the account that created them.
Unknown IDs return
404. - Image links stay available for 30 days after completion.
Full examples¶
# Submit
curl https://api.yexapi.com/v1/images/generations/async \
-H "Authorization: Bearer $YEXAPI_API_KEY" \
-H "Content-Type: application/json" \
-d '{"model": "gpt-image-2", "prompt": "A tiny robot watering a bonsai tree", "size": "1024x1024"}'
# Check (repeat until status is completed or failed)
curl https://api.yexapi.com/v1/images/generations/task_QHTA3GtjsX1unGqsXTsckBeT5OILs5db \
-H "Authorization: Bearer $YEXAPI_API_KEY"
# pip install requests
import os
import time
import requests
API_KEY = os.environ["YEXAPI_API_KEY"]
BASE_URL = "https://api.yexapi.com/v1"
headers = {"Authorization": f"Bearer {API_KEY}"}
# 1. Submit the task; the response comes back immediately.
task = requests.post(
f"{BASE_URL}/images/generations/async",
headers=headers,
json={
"model": "gpt-image-2",
"prompt": "An isometric illustration of a tiny greenhouse",
"size": "3:2",
"resolution": "2k",
},
timeout=30,
).json()
print("submitted:", task["id"], task["status"])
# 2. Poll until the task reaches a final state.
while task["status"] in ("queued", "in_progress"):
time.sleep(5)
task = requests.get(
f"{BASE_URL}/images/generations/{task['id']}",
headers=headers,
timeout=30,
).json()
if task["status"] == "completed":
print(task["data"][0]["url"])
else:
print("failed:", task["error"]["message"])
// Node.js 18+ (built-in fetch). Save as async.mjs
const API_KEY = process.env.YEXAPI_API_KEY;
const BASE_URL = "https://api.yexapi.com/v1";
const headers = {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "application/json",
};
// 1. Submit the task; the response comes back immediately.
let task = await fetch(`${BASE_URL}/images/generations/async`, {
method: "POST",
headers,
body: JSON.stringify({
model: "gpt-image-2",
prompt: "An isometric illustration of a tiny greenhouse",
size: "3:2",
resolution: "2k",
}),
}).then((r) => r.json());
console.log("submitted:", task.id, task.status);
// 2. Poll until the task reaches a final state.
while (task.status === "queued" || task.status === "in_progress") {
await new Promise((r) => setTimeout(r, 5000));
task = await fetch(`${BASE_URL}/images/generations/${task.id}`, { headers }).then((r) => r.json());
}
if (task.status === "completed") console.log(task.data[0].url);
else console.log("failed:", task.error.message);
The official OpenAI SDKs have no method for these two endpoints, so the examples use plain HTTP. Your existing SDK client can still be used for synchronous calls.