const API_KEY = "your_api_key_here";
const BASE_URL = "https://api.sinancode.com/v1";
async function generateImage() {
// 创建任务
const createResponse = await fetch(`${BASE_URL}/generate/k/imagen4`, {
method: "POST",
headers: {
"Authorization": `Bearer ${API_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
prompt: "a serene mountain landscape",
aspect_ratio: "16:9",
num_images: 1
})
});
const { task_id } = await createResponse.json();
console.log(`任务创建成功,ID: ${task_id}`);
// 轮询任务状态
while (true) {
const statusResponse = await fetch(`${BASE_URL}/tasks/${task_id}`, {
headers: { "Authorization": `Bearer ${API_KEY}` }
});
const task = await statusResponse.json();
console.log(`任务状态: ${task.status}`);
if (task.status === "completed") {
console.log(`生成完成!图片地址: ${task.result[0]}`);
break;
} else if (task.status === "failed") {
console.log(`生成失败: ${task.error_msg}`);
break;
}
await new Promise(resolve => setTimeout(resolve, 2000)); // 等待 2 秒
}
}
generateImage();