给你的应用加上 AI 图像生成
通过 Orux AI API 提交图像生成任务、查询状态并读取生成结果。
ai 图像生成 api
当前可用模型
查看完整目录你将实现什么
把图像提示词作为异步任务提交,查询任务状态,并读取生成结果。具体输入字段取决于所选模型。
1. 选择模型并创建 API Key
先浏览图像模型目录,打开模型详情页查看请求结构,再到Keys创建 API Key。
2. 提交任务
const headers = {
Authorization: "Bearer " + process.env.ORUX_API_KEY,
"Content-Type": "application/json",
}
const response = await fetch("https://orux.top/api/v1/tasks", {
method: "POST",
headers,
body: JSON.stringify({
model: "YOUR_IMAGE_MODEL_ALIAS",
input: { prompt: "雪地里的红狐,电影感光线" },
}),
})
if (!response.ok) throw new Error(await response.text())
const { task_id } = await response.json()Swipe horizontally to view the full content
请按模型详情页的说明替换模型别名和输入字段。
3. 轮询任务结果
let task
do {
await new Promise((resolve) => setTimeout(resolve, 5000))
task = await fetch("https://orux.top/api/v1/tasks/" + task_id, { headers }).then((r) => r.json())
} while (task.status === "QUEUED" || task.status === "RUNNING")
if (task.status !== "SUCCESS") throw new Error(task.error?.message ?? task.status)
console.log(task.result)Swipe horizontally to view the full content
失败或过期的任务不会扣费。提交前请查看当前模型价格和模型详情页里的可选设置。
常见 API 响应
- HTTP 401——检查 API Key 是否存在、有效,并以 Bearer token 方式发送。
- HTTP 402——账户余额不足,请查看余额页面。
- HTTP 429——降低请求频率,并使用带随机抖动的指数退避重试。