图像
获取图像状态
获取异步图像生成任务的状态和结果
路径参数
idstringpath必填
初始图像生成请求返回的任务 ID。
创建响应中有 poll_url 时,直接使用这个地址查询。它可能指向 /v1/tasks/{id},也可能是图片专用状态地址。
ldtask_... 图片任务也可以通过 /v1/images/{id} 查询;这个兼容地址与 /v1/images/generations/{id} 返回相同格式。
已过期或不可用的公开任务可能返回 404 async_task_not_found。无法确认当前 API 密钥对任务的归属权限时,返回 403 task_not_owned。
响应
结果、错误、时间戳和模型字段仅在任务提供时返回。
idstring
TokenLab 任务 ID。
poll_urlstring
创建响应返回的状态查询地址。
createdinteger
创建时的 Unix 时间戳。
task_idstring
任务标识符。
statusstring
任务状态:pending、processing、completed 或 failed。
dataarray
生成的图像数组(当 status 为 completed 时填充)。
每个对象包含:
url(string): 生成图像的 URLrevised_prompt(string): 所选模型返回的可选提示词改写结果
errorstring | object
错误信息(仅在 status 为 failed 时存在)。
请求
curl "https://api.tokenlab.sh/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
-H "Authorization: Bearer sk-your-api-key"响应
{
"created": 1706000000,
"id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"task_id": "ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"poll_url": "/v1/tasks/ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
"status": "pending",
"data": []
}查询间隔
状态为 pending 或 processing 时,每 3–5 秒查询一次;变为 completed 或 failed 后停止。
import requests
import time
def poll_image_task(task_id, api_key, max_wait=300, interval=3):
"""Poll for image generation result with timeout."""
url = f"https://api.tokenlab.sh/v1/tasks/{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, timeout=30)
response.raise_for_status()
data = response.json()
if data["status"] == "completed":
return data["data"][0]["url"]
elif data["status"] == "failed":
raise Exception(data.get("error", "Generation failed"))
time.sleep(interval)
raise TimeoutError(f"Task {task_id} did not complete within {max_wait}s")
# Usage
image_url = poll_image_task("ldtask_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "sk-your-api-key")
print(f"Generated image: {image_url}")授权
BearerAuth AuthorizationBearer <token>
API Key 身份验证。在 Dashboard > API > API Keys 中创建或管理 API Key。
位置: header
路径参数
id*string
由 id / task_id 返回或嵌入在 poll_url 中的异步任务 ID
响应
application/json
application/json
application/json
application/json