# API 文档

> 一次请求生成一支视频：用 API 密钥调用 `POST /api/v1/videos`，拿到视频 id，然后轮询 `GET /api/v1/videos/{id}`，或者传入 `webhook_url` 等我们回调。所有接口都返回 JSON，价格以积分计，失败的视频自动退还积分。

## 1. 认证

在 [设置 → API 密钥](https://magi2ai.dev/settings/apikeys) 创建密钥（以 `sk_` 开头），放进请求头：

```http
Authorization: Bearer sk_…
```

密钥只显示一次；撤销后立即失效。不要把密钥放进前端代码或公开仓库。

## 2. 生成视频

`POST /api/v1/videos`

| 字段 | 类型 | 说明 |
| --- | --- | --- |
| `model` | string | 必填。见下方模型表，例如 `seedance-2-fast` |
| `prompt` | string | 必填，最多 2000 个字符 |
| `image_url` | string | 可选，首帧图片的 https 地址。有它就是图生视频；Hailuo 2.3 必填 |
| `resolution` | string | 可选，默认用模型的默认分辨率 |
| `seconds` | number | 可选，视频时长 |
| `aspect_ratio` | string | 可选，仅文生视频，例如 `16:9`、`9:16` |
| `webhook_url` | string | 可选，视频结束时我们会 POST 到这个 https 地址 |

加上 `Idempotency-Key` 请求头可以安全重试：同一个键在同一账户下只会创建一支视频。

```bash
curl https://magi2ai.dev/api/v1/videos \
  -H "Authorization: Bearer $MAGI2_API_KEY" \
  -H "Content-Type: application/json" \
  -H "Idempotency-Key: 7f3c1a52-launch-teaser" \
  -d '{
    "model": "seedance-2-fast",
    "prompt": "A lighthouse at dusk, waves rolling in, slow push-in",
    "resolution": "480p",
    "seconds": 5,
    "aspect_ratio": "16:9",
    "webhook_url": "https://example.com/hooks/magi2"
  }'
```

返回 `201`：

```json
{
  "data": {
    "id": "3f9c2b7e-5a1d-4c8e-9b0f-2d6e8a41c7d3",
    "status": "pending",
    "model": "seedance-2-fast",
    "modelName": "Seedance 2.0 Fast",
    "mode": "text",
    "prompt": "A lighthouse at dusk, waves rolling in, slow push-in",
    "resolution": "480p",
    "seconds": 5,
    "aspect": "16:9",
    "credits": 95,
    "outputs": [],
    "license": "commercial",
    "createdAt": "2026-11-25T09:12:44.000Z"
  }
}
```

图生视频：

```bash
curl https://magi2ai.dev/api/v1/videos \
  -H "Authorization: Bearer $MAGI2_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "hailuo-2-3",
    "prompt": "The rider leans into the turn, camera tracks alongside",
    "image_url": "https://example.com/first-frame.jpg",
    "resolution": "768p",
    "seconds": 6
  }'
```

## 3. 获取结果

`GET /api/v1/videos/{id}` 返回同样的对象。`status` 依次为 `pending`、`processing`，最后是 `success`、`failed` 或 `canceled`。成功时 `outputs[0]` 是 MP4 地址。建议每 10 秒左右轮询一次，或使用 [webhook](https://magi2ai.dev/webhooks)。

```bash
curl https://magi2ai.dev/api/v1/videos/3f9c2b7e-5a1d-4c8e-9b0f-2d6e8a41c7d3 \
  -H "Authorization: Bearer $MAGI2_API_KEY"
```

`GET /api/v1/videos?limit=20&page=1` 按时间倒序列出你的视频。

## 4. 模型与价格

`GET /api/v1/models` 不需要密钥，返回每个模型的选项和价格。当前模型：

| 模型 (`model`) | 输入 | 分辨率 | 时长（秒） | 声音 | 起价 |
| --- | --- | --- | --- | --- | --- |
| Seedance 2.0 Fast (`seedance-2-fast`) | 文字或图片 | 480p / 720p | 5 / 10 | 有 | 15 积分 |
| Kling 4.0 Flash (`kling-4-flash`) | 文字或图片 | 720p | 5 / 10 | 无 | 20 积分 |
| Wan 2.7 (`wan-2-7`) | 文字或图片 | 720p / 1080p | 5 / 10 | 无 | 15 积分 |
| Hailuo 2.3 (`hailuo-2-3`) | 仅图片 | 768p / 1080p | 6 / 10 | 无 | 11 积分 |
| MAGI-2 (`magi-2`) | 即将上线 | | | | 暂不可用 |

每种组合的积分：

| 模型 | 分辨率 | 时长 | 积分 |
| --- | --- | --- | --- |
| Seedance 2.0 Fast | 480p | 5 秒 | 15 |
| Seedance 2.0 Fast | 480p | 10 秒 | 30 |
| Seedance 2.0 Fast | 720p | 5 秒 | 25 |
| Seedance 2.0 Fast | 720p | 10 秒 | 50 |
| Kling 4.0 Flash | 720p | 5 秒 | 20 |
| Kling 4.0 Flash | 720p | 10 秒 | 40 |
| Wan 2.7 | 720p | 5 秒 | 15 |
| Wan 2.7 | 720p | 10 秒 | 30 |
| Wan 2.7 | 1080p | 5 秒 | 25 |
| Wan 2.7 | 1080p | 10 秒 | 50 |
| Hailuo 2.3 | 768p | 6 秒 | 11 |
| Hailuo 2.3 | 768p | 10 秒 | 18 |
| Hailuo 2.3 | 1080p | 6 秒 | 16 |

积分在视频开始时扣除；生成失败会自动退回。被内容筛查拦截的提示词不扣费。购买积分见 [价格](https://magi2ai.dev/pricing)。

## 5. 账户余额

`GET /api/v1/account` 返回 `{ "data": { "credits": 1234 } }`。

## 6. 错误

出错时返回 `{ "error": { "message": "…" } }`：

| 状态码 | 含义 |
| --- | --- |
| `400` | 请求体不是 JSON，或选项组合无效（例如该模型不支持的分辨率或时长，或请求了尚未上线的 `magi-2`） |
| `401` | 缺少密钥，或密钥无效、已撤销 |
| `402` | 积分不足 |
| `422` | 提示词或图片未通过内容筛查（不扣费） |
| `429` | 请求太快，请稍后再试 |
| `5xx` | 我们这边出错，可以带上同一个 `Idempotency-Key` 重试 |

## 7. 代码示例

JavaScript（Node 18+）：

```js
const res = await fetch('https://magi2ai.dev/api/v1/videos', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.MAGI2_API_KEY}`,
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    model: 'wan-2-7',
    prompt: 'Morning fog lifting off an alpine lake, drone glide',
    resolution: '720p',
    seconds: 5,
    aspect_ratio: '16:9',
  }),
});
const { data, error } = await res.json();
if (!res.ok) throw new Error(error.message);
console.log(data.id, data.status);
```

Python：

```python
import os, time, requests

API = "https://magi2ai.dev/api/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['MAGI2_API_KEY']}"}

video = requests.post(f"{API}/videos", headers=HEADERS, json={
    "model": "kling-4-flash",
    "prompt": "A red fox trotting through a birch forest in snow",
    "resolution": "720p",
    "seconds": 5,
    "aspect_ratio": "16:9",
}).json()["data"]

while video["status"] in ("pending", "processing"):
    time.sleep(10)
    video = requests.get(f"{API}/videos/{video['id']}", headers=HEADERS).json()["data"]

print(video["status"], video["outputs"])
```

## 8. 在 AI 助手里使用（MCP）

同样的功能也可以通过 MCP 服务器 `https://magi2ai.dev/mcp` 使用，支持 Claude、ChatGPT、Cursor 等客户端，用 OAuth 登录或同一个 API 密钥。设置步骤见 [MCP 服务器](https://magi2ai.dev/mcp-server)。

## FAQ

### 生成一支视频需要多久？

通常几十秒到几分钟，取决于模型、分辨率和时长。用 webhook 就不必一直轮询。

### 视频失败会扣积分吗？

积分在开始时扣除，失败后自动退回。被内容筛查拦截的请求不扣费。

### 能用其他网站上的图片做首帧吗？

可以，传入公开的 https 图片地址即可，我们会先把它复制到自己的存储里再生成。

### 有速率限制吗？

每个账户每秒最多创建一支视频，同时进行的视频数量也有上限（付费账户更高）。超过时返回 429。


---

Source: https://magi2ai.dev/zh/docs · Site index for AI agents: https://magi2ai.dev/llms.txt · Full content: https://magi2ai.dev/llms-full.txt
