生命周期
任务状态:| 状态 | 含义 |
|---|---|
pending | 任务已创建并进入队列。 |
processing | Worker 已开始执行或轮询上游。 |
completed | 输出、成本和 usage 已记录。 |
failed | 任务失败,响应里会有错误信息。 |
创建任务
curl -X POST https://api.eggapi.ai/v1/generate \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "nanobanana",
"prompt": "A dashboard hero image with glass UI panels",
"aspect_ratio": "16:9",
"num_outputs": 1
}'
{
"data": {
"id": "gen_abc123def456",
"status": "pending",
"model": "nanobanana",
"type": "image_generate",
"cost": "$0.00",
"created_at": "2026-05-08T10:30:00Z"
},
"error": null
}
轮询任务
curl https://api.eggapi.ai/v1/tasks/gen_abc123def456 \
-H "Authorization: Bearer YOUR_API_KEY"
{
"data": {
"id": "gen_abc123def456",
"status": "completed",
"model": "nanobanana",
"type": "image_generate",
"output": {
"url": "https://file.eggapi.ai/images/gen_abc123def456/0.png",
"urls": ["https://file.eggapi.ai/images/gen_abc123def456/0.png"]
},
"cost": "$0.0100",
"created_at": "2026-05-08T10:30:00Z",
"started_at": "2026-05-08T10:30:01Z",
"completed_at": "2026-05-08T10:30:10Z"
},
"error": null
}
列出任务
curl "https://api.eggapi.ai/v1/tasks?page=1&per_page=20" \
-H "Authorization: Bearer YOUR_API_KEY"
data.pagination 中返回。
Webhook
创建任务时传入webhook_url:
{
"model": "nanobanana",
"prompt": "A studio portrait with soft lighting",
"webhook_url": "https://example.com/eggapi/webhook"
}
| Header | 说明 |
|---|---|
X-EggAPI-Timestamp | 用于签名的 Unix 时间戳。 |
X-EggAPI-Signature | sha256= 加上使用你的 webhook secret 对 {timestamp}.{raw_body} 做 HMAC-SHA256 的结果。 |
{
"event": "task.completed",
"task_id": "gen_abc123def456",
"status": "completed",
"model": "nanobanana",
"output": {
"urls": ["https://file.eggapi.ai/images/gen_abc123def456/0.png"]
},
"cost": "0.010000",
"timestamp": 1778217000
}
{
"event": "task.failed",
"task_id": "gen_abc123def456",
"status": "failed",
"model": "nanobanana",
"error": "generation failed: upstream provider unavailable",
"cost": "0.000000",
"timestamp": 1778217000
}
轮询辅助函数
async function waitForTask(taskId, apiKey, timeoutMs = 300000) {
const start = Date.now();
while (Date.now() - start < timeoutMs) {
const response = await fetch(`https://api.eggapi.ai/v1/tasks/${taskId}`, {
headers: { "Authorization": `Bearer ${apiKey}` }
});
const { data, error } = await response.json();
if (error) throw new Error(error.message);
if (data.status === "completed") return data;
if (data.status === "failed") throw new Error(data.error || "Task failed");
await new Promise((resolve) => setTimeout(resolve, 3000));
}
throw new Error("Task timed out");
}