提交图片生成或编辑任务,获得 task_id 后查询状态与图片链接,无需保持生成请求的长连接。
| 方法 | 路径 | 用途 |
|---|---|---|
| POST | /v1/images/async/generations | 提交图片生成任务 |
| POST | /v1/images/async/edits | 提交图片编辑任务 |
| GET | /v1/images/async/{task_id} | 查询任务状态与图片链接 |
所有接口需要 Authorization: Bearer <你的 API Key>,只能查询当前用户的任务。
{
"model": "gpt-image-2",
"prompt": "一只戴墨镜的柴犬",
"response_format": "url"
}
请求 POST /v1/images/async/generations,Content-Type: application/json。
提交成功:
{"task_id":"task_example"}
JSON 请求 POST /v1/images/async/edits:
{
"model": "gpt-image-2",
"prompt": "把背景换成海边",
"image_urls": ["https://example.com/input.png"],
"response_format": "url"
}
输入支持 image_urls 数组,或 image_url、image 单个 URL;遮罩使用 mask_url。JSON 图片字段只接受可公网访问的 http(s) URL,不接受 Base64。请保证输入链接在排队及执行期间有效。
multipart/form-data 可使用 model、prompt、response_format=url 和 image 文件字段;多图重复 image 或 image[]。mask 文件暂不支持,请使用 JSON mask_url。文件上传无法接受时请改用 image_urls;单文件超限返回 413。客户端应使用接口实际返回的错误说明。
GET /v1/images/async/task_example。查询时无需传 response_format。
{
"task_id": "task_example",
"status": "succeeded",
"result": {
"images": ["https://example.com/result.png"]
}
}
提交时校验余额,不扣生成费用;执行后按模型正常计费。建议每 2~5 秒查询一次。
新任务不会保留图片正文供日后补传。保存失败时不保证能再次获取图片,也不会自动重新生成;已经完成生成的任务可能已计费,请先核对消费记录,避免重复提交。仅历史任务仍有有效补传副本时,才返回 storage.retry_until;补传接口不会重新调用生成。
| HTTP 状态 | 含义 |
|---|---|
| 400 | 不支持的 response_format(包括 b64_json)、参数错误或缺少输入图 |
| 401 | API Key 无效 |
| 403 | 权限或余额不足 |
| 404 | 任务不存在或不属于当前用户 |
| 413 | 上传文件过大 |
| 429 | 排队任务过多,请等待已有任务完成 |
| 503 | 服务暂时不可用,按具体错误说明处理 |
无论何种终态,请先核对结果与消费信息,再决定是否重新提交。
# 1) 提交生图任务,秒回 task_id
curl https://api.openai-hub.net/v1/images/async/generations \
-H "Authorization: Bearer $APIKEY" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-2",
"prompt": "一只戴宇航头盔的柴犬",
"n": 1,
"size": "1024x1024"
}'
# 返回:{"task_id":"task_xxx"}
# 2) 用 task_id 轮询结果(status 变为 succeeded/failed/storage_failed 即结束)
curl https://api.openai-hub.net/v1/images/async/task_xxx \
-H "Authorization: Bearer $APIKEY"{
"task_id": "string"
}