curl "https://api.lemondata.cc/v1/images/generations/img_abc123def456" \ -H "Authorization: Bearer sk-your-api-key"
{ "created": 1706000000, "task_id": "img_abc123def456", "status": "pending", "data": [ { "url": "", "revised_prompt": "a beautiful sunset over mountains" } ] }
取得異步圖像生成任務的狀態和結果
pending
processing
completed
failed
status
url
revised_prompt
import requests import time def poll_image_task(task_id, api_key, max_wait=300, interval=3): """帶超時的圖像生成結果輪詢。""" url = f"https://api.lemondata.cc/v1/images/generations/{task_id}" headers = {"Authorization": f"Bearer {api_key}"} start_time = time.time() while time.time() - start_time < max_wait: response = requests.get(url, headers=headers) data = response.json() if data["status"] == "completed": return data["data"][0]["url"] elif data["status"] == "failed": raise Exception(data.get("error", "生成失敗")) time.sleep(interval) raise TimeoutError(f"任務 {task_id} 未在 {max_wait} 秒內完成") # 使用範例 image_url = poll_image_task("img_abc123def456", "sk-your-api-key") print(f"生成的圖像: {image_url}")
從初始圖像生成請求回傳的任務 ID。
Response 200
建立時間的 Unix 時間戳。
任務識別碼。
任務狀態: pending 、 processing 、 completed 或 failed 。
生成的圖像陣列(當 status 為 completed 時填充)。 每個物件包含: url (string): 生成圖像的 URL revised_prompt (string): 用於生成的提示詞
錯誤訊息(僅當 status 為 failed 時存在)。