
这篇 MAGI-2 API 指南带你从 API 密钥一路走到生成好的 MP4。每个请求都在 https://api.poyo.ai 上使用模型 ID magi-2,同一个请求体可以在 cURL、Python 和 Node.js 中通用。你可以先在 MAGI-2 Playground 中测试提示词;字段说明见 MAGI-2 API 文档。
第一次接触这个模型?先看 MAGI-2 API 概览。
MAGI-2 API 调用流程
- 带上
model和input调用POST /api/generate/submit。响应包含data.task_id和status: "not_started"。 - 轮询
GET /api/generate/status/{task_id},直到status为finished或failed;或者传入callback_url,等待 POST 回调。 - 从
data.files[]中file_type为video的条目读取 MP4。
在 poyo.ai/dashboard/api-key 创建密钥,并导出为 POYO_API_KEY。密钥只保存在服务端。
用 cURL 文生视频
curl https://api.poyo.ai/api/generate/submit \
-H "Authorization: Bearer $POYO_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "magi-2",
"input": {
"prompt": "On a ferry deck at sunset, two old friends lean on the railing. The woman says: \"I never thought we would make this trip.\" The man laughs and replies: \"Neither did I.\" Seagulls call overhead and the engine hums below.",
"duration": 8,
"aspect_ratio": "16:9"
}
}'
然后用返回的 ID 查询任务:
curl https://api.poyo.ai/api/generate/status/YOUR_TASK_ID \
-H "Authorization: Bearer $POYO_API_KEY"
resolution 默认为 1080p,generate_audio 默认为 true,所以这个请求会返回带声音的 1920×1080 MP4。在 PoYo 2026 年 11 月 28 日上线当天的测试中,这样一个 8 秒请求扣费 128 积分($0.64),约 3.5 分钟内完成(轮询)。
带超时上限的 Python 轮询
这个循环同时检查 HTTP 状态码和响应体中的 code 字段,因为部分校验错误会以 HTTP 200 加 code: 400 的形式返回。它在 20 分钟后停止;如果发生这种情况,请稍后继续轮询同一个 task_id,不要重新提交,否则会启动第二个计费任务。
import os
import time
import requests
BASE = "https://api.poyo.ai"
session = requests.Session()
session.headers["Authorization"] = "Bearer " + os.environ["POYO_API_KEY"]
def checked(resp):
resp.raise_for_status()
body = resp.json()
if body.get("code") != 200:
raise RuntimeError(f"PoYo error {body.get('code')}: {body.get('error')}")
return body["data"]
task = checked(session.post(f"{BASE}/api/generate/submit", json={
"model": "magi-2",
"input": {
"prompt": "A rainy railway crossing at night. The barrier lowers, bells ring, "
"and a freight train rushes past. Rain hisses on the road. No dialogue.",
"duration": 6,
"aspect_ratio": "16:9",
},
}, timeout=60))
task_id = task["task_id"]
print("task", task_id)
deadline = time.monotonic() + 20 * 60
while time.monotonic() < deadline:
data = checked(session.get(f"{BASE}/api/generate/status/{task_id}", timeout=60))
if data["status"] == "failed":
raise RuntimeError(data.get("error_message") or "generation failed")
if data["status"] == "finished":
video = next(f["file_url"] for f in data["files"] if f.get("file_type") == "video")
print(video, "credits:", data.get("credits_amount"))
break
time.sleep(10)
else:
raise TimeoutError(f"still running, poll again later: {task_id}")
用 fetch 的 Node.js
Node 18 及以上版本内置 fetch,不需要 SDK。
const BASE = "https://api.poyo.ai";
const headers = {
Authorization: `Bearer ${process.env.POYO_API_KEY}`,
"Content-Type": "application/json",
};
async function call(path, init = {}) {
const res = await fetch(BASE + path, { headers, ...init });
const body = await res.json();
if (!res.ok || body.code !== 200) {
throw new Error(`PoYo error ${body.code}: ${JSON.stringify(body.error)}`);
}
return body.data;
}
const task = await call("/api/generate/submit", {
method: "POST",
body: JSON.stringify({
model: "magi-2",
input: {
prompt: "A cyclist rides along a coastal road at golden hour, waves breaking below, wind rushing past.",
image_urls: ["https://example.com/cyclist-first-frame.jpg"],
duration: 5,
},
}),
});
const deadline = Date.now() + 20 * 60 * 1000;
while (Date.now() < deadline) {
const data = await call(`/api/generate/status/${task.task_id}`);
if (data.status === "failed") throw new Error(data.error_message || "failed");
if (data.status === "finished") {
console.log(data.files.find((f) => f.file_type === "video").file_url);
break;
}
await new Promise((r) => setTimeout(r, 10_000));
}
保存为 magi2.mjs,这样顶层 await 才能生效。
MAGI-2 API 各输入模式的请求体
首帧,或首尾帧
发送一张图片设定开头帧,或发送两张图片分别设定开头帧和结尾帧。视频沿用首帧的画面比例,因此不要传 aspect_ratio,或传 auto。
{
"model": "magi-2",
"input": {
"prompt": "The cyclist from the first frame rides toward the lighthouse and stops at the end of the road. Wind and distant waves.",
"image_urls": [
"https://example.com/first-frame.jpg",
"https://example.com/last-frame.jpg"
],
"duration": 6,
"aspect_ratio": "auto"
}
}
在 PoYo 上线当天的测试中,一个 5 秒的首帧请求(image_urls 中一张图片)扣费 80 积分($0.40),约 3 分钟内完成(轮询)。
参考图片和参考音频
参考素材可以把角色、地点或一段音乐带进新场景。在提示词中说明每个参考素材的用途。
{
"model": "magi-2",
"input": {
"prompt": "The elderly street musician from the reference image walks along a sunlit seaside boardwalk, playing the saxophone melody from the reference audio. Seagulls glide overhead. He stops, turns to the camera and says: \"Morning light suits this tune.\"",
"reference_image_urls": [
"https://storage.poyo.ai/models/magi-2/examples/reference-character-6586977013a0.jpg"
],
"reference_audio_urls": [
"https://storage.poyo.ai/models/magi-2/examples/reference-audio-5bf2add370f4.mp3"
],
"duration": 8,
"aspect_ratio": "16:9"
}
}
参考视频
用 reference_video_urls 提供动作、运镜或运动中的角色。它可以和图片、音频参考在同一个任务中组合使用。
{
"model": "magi-2",
"input": {
"prompt": "Follow the camera move from the reference video: a slow dolly through a night market. Vendors call out, oil sizzles in woks, a scooter passes.",
"reference_video_urls": ["https://example.com/dolly-reference.mp4"],
"duration": 10,
"aspect_ratio": "9:16"
}
}
也可以只发送参考音频,例如用一段音乐片段来设定片段的节奏。
值得在客户端执行的校验规则
| 规则 | 原因 |
|---|---|
prompt 1–15,000 个字符 | 更长的提示词会被拒绝 |
duration 为 4–30 的整数 | 不支持小数秒 |
image_urls 和 reference_* 不能出现在同一个任务中 | 每个任务只用一种模式 |
image_urls 1–2 张 JPG、PNG 或 WEBP | 第三张图片会被拒绝 |
| 最多 10 张参考图片、5 段视频、5 段音频 | 按任务计 |
| 每段参考视频或音频 2–30 秒,每种类型合计 30 秒 | 上传前先测量 |
使用 image_urls 时 aspect_ratio 为 auto 或留空 | 由帧决定比例 |
seed 0–2147483647 | 相同输入搭配同一 seed 可得到相似结果 |
| 不要传 schema 以外的字段 | 多余的键会导致校验失败 |
所有媒体 URL 必须公开且可直接下载。如果使用签名 URL,有效期要足够覆盖任务排队等待的时间。
需要处理的错误
code | 含义 | 处理方式 |
|---|---|---|
| 400 | 参数无效 | 修正请求体;不要原样重试 |
| 401 | 密钥缺失或无效 | 检查 Authorization 请求头 |
| 402 | 积分不足 | 充值后重新提交 |
| 429 | 触发限流 | 退避后重试提交 |
| 500 / 502 / 503 | 服务端或上游错误 | 退避重试 |
任务也可能以 failed 结束并带有 error_message;PoYo 会自动为失败任务退款。
用回调代替轮询
在 model 旁边添加顶层字段 callback_url(HTTPS、公网可访问)。任务完成或失败时,PoYo 会 POST 任务数据,结构与状态响应相同,但没有 code/data 外层包装,并通过 X-Webhook-Timestamp 和 X-Webhook-Signature(HMAC-SHA256)签名。投递失败最多重试 5 次;轮询仍可作为兜底。详情见视频 Webhook 文档。
为 API 请求编写提示词
prompt 字段承载一切:场景、动作、镜头和声音。以下几条来自文档和 PoYo 上线测试的规则,能让第一次请求的结果更可预期:
- 台词加引号。 文档说明,引号中的台词会由角色在生成的音频中说出。在台词前写明说话人。
- 明确描述声音。 海鸥、引擎嗡鸣、雨打柏油路:列出应该听到的声音;如果只想要音效和环境声,写上"no dialogue"(无对白)。
- 按需使用长度。 提示词最长可达 15,000 个字符,足以写一份带时间段的镜头脚本。Sand AI 表示 MAGI-2 支持在提示词中控制时间线和切镜。
- 时长与内容匹配。 两句简短对白放得进 8 秒。30 秒的镜头脚本需要
duration: 30。 - 迭代时固定
seed。 相同输入搭配同一 seed 会得到相似结果,这样你可以一次只改提示词中的一个细节。
上线生产前的检查清单
在把 MAGI-2 API 调用从脚本迁移到产品之前:
- 轮询前先保存
task_id。 如果 worker 重启,继续轮询已保存的 ID,而不是重新提交。 - 先在自己这边校验。 请求离开服务器之前检查时长、数量以及帧/参考模式规则,让用户看到清晰的错误,而不是一个失败的任务。
- 把媒体放在 PoYo 能拉取的地方。 使用直接返回文件的公开 HTTPS URL,而不是预览页面。
- 把结果复制到自己的存储。 任务完成后从
file_url下载 MP4,并从你自己的存储或 CDN 提供服务。 - 记录
credits_amount。 与你的估算(duration × 16)对比,及时发现与预期不符的请求。 - 规模化时优先使用回调。 大量并行任务时,签名回调的运行成本远低于成千上万次状态轮询;轮询保留作兜底。
第一笔 MAGI-2 API 账单如何计算
credits = duration × 16
USD = credits × 0.005
只计算输出秒数。参考素材、画面比例和 generate_audio 都不影响价格,所以 5 秒片段为 80 积分($0.40),30 秒片段为 480 积分($2.40)。已完成任务上的 credits_amount 显示实际扣费。更多示例以及与 PoYo 其他视频模型的对比,请看 MAGI-2 API 价格;音频提示词写法请看 MAGI-2 API 有声视频。
常见问题
如何用 Python 调用 MAGI-2 API?
用 requests 把 JSON 请求体 POST 到 https://api.poyo.ai/api/generate/submit,然后按上面的循环轮询 /api/generate/status/{task_id}。
MAGI-2 API 有官方 Node.js SDK 吗?
不需要。如上所示,直接用 fetch 加 Bearer 请求头即可。
可以在一个 MAGI-2 API 请求中同时发送首帧和参考图片吗?
不可以。帧用 image_urls,参考素材用 reference_* 字段,每个任务只用一种模式。
为什么我的 MAGI-2 API 请求返回 HTTP 200 却没有任务?
检查响应体中的 code。校验错误可能以 HTTP 200 加 code: 400 的形式返回。
MAGI-2 API 任务应该轮询多久?
在 PoYo 上线当天的测试中,5–8 秒的片段约在 3–3.5 分钟内完成(轮询)。大约每 10 秒轮询一次,如果超时请保留 task_id。