How to Use MAGI-2 API
A step-by-step MAGI-2 API tutorial with Node.js, Python and cURL: submit a text to video job, poll it or receive a webhook, and download the video. The example below costs $0.40.
- Endpoints
- 3
- Text to Video · Image to Video · Reference to Video
- Example request
- $0.40
- Text to Video
- Languages
- 3
- Node.js · Python · cURL
- Failed runs
- $0
- refunded automatically
Get an API key
Create an account, add credits and create a key in Dashboard → API keys. Store it as CHEAPERGEN_API_KEY on your server. Every request sends it as a bearer token.
Choose a MAGI-2 endpoint
Each task has its own endpoint. All of them take a JSON body and return a generation you can poll.
| Task | Endpoint | Required | From |
|---|---|---|---|
| Text to Video | sand-ai/magi-2/text-to-video | prompt | $0.08/s |
| Image to Video | sand-ai/magi-2/image-to-video | prompt, image_url | $0.08/s |
| Reference to Video | sand-ai/magi-2/reference-to-video | prompt | $0.08/s |
Submit a text to video job
This program submits the job, polls every 3 seconds until it finishes and prints the video URL. Swap the prompt and settings for your own.
const API = "https://cheapergen.com/api/v1";
const headers = {
Authorization: `Bearer ${process.env.CHEAPERGEN_API_KEY}`,
"Content-Type": "application/json",
};
// 1. Submit the job
let job = await fetch(`${API}/run/sand-ai/magi-2/text-to-video`, {
method: "POST",
headers,
body: JSON.stringify({
"prompt": "A paper boat drifts across a pond as rain taps against the surface.",
"aspect_ratio": "16:9",
"resolution": "1080p",
"duration": 5
}),
}).then((r) => r.json());
// 2. Poll until it finishes (or pass webhook_url and skip polling)
while (job.status === "queued" || job.status === "running") {
await new Promise((r) => setTimeout(r, 3000));
job = await fetch(`${API}/generations/${job.id}`, { headers }).then((r) => r.json());
}
console.log(job.status === "succeeded" ? job.output.video.url : job.error);Set the parameters that matter
The settings for sand-ai/magi-2/text-to-video, with what each one does to the price.
| Parameter | Type · default | What it does | Price effect |
|---|---|---|---|
| aspect_ratio | string (enum) · 16:9 | Output frame shape. 16:9 · 9:16 · 1:1 · 4:3 · 3:4 · 21:9 | None |
| resolution | string (enum) · 1080p | The 1080p tier. Pixel dimensions depend on the output aspect ratio. 1080p | None |
| duration | integer · 5 | Length of the generated clip in seconds (4–30s). You pay per second. | Billed per second |
| generate_audio | boolean · true | Generate synchronized sound, ambience and dialogue. No extra cost. | None |
Other MAGI-2 endpoints take the same shape with their own inputs:
POST /api/v1/run/sand-ai/magi-2/image-to-video
{
"prompt": "The camera pulls back as the boat begins to drift.",
"image_url": "https://cdn.cheapergen.com/samples/portrait.jpg",
"resolution": "1080p",
"duration": 5
}POST /api/v1/run/sand-ai/magi-2/reference-to-video
{
"prompt": "Place the paper boat from @Image1 on the pond from @Image2.",
"reference_image_urls": [
"https://cdn.cheapergen.com/samples/reference-1.jpg",
"https://cdn.cheapergen.com/samples/reference-2.jpg"
],
"aspect_ratio": "16:9",
"resolution": "1080p",
"duration": 5
}Get the result
A job moves from queued to running to succeeded, failed or canceled. Poll GET /api/v1/generations/{id}, or pass webhook_url and we POST the result to you. Download the output and store it yourself.
{
"id": "gen_8c2QmX1nq0k4",
"object": "generation",
"model": "sand-ai/magi-2",
"endpoint": "sand-ai/magi-2/text-to-video",
"mode": "text-to-video",
"status": "succeeded",
"input": {
"prompt": "A paper boat drifts across a pond as rain taps against the surface.",
"aspect_ratio": "16:9",
"resolution": "1080p",
"duration": 5
},
"output": {
"video": {
"url": "https://cdn.cheapergen.com/outputs/gen_8c2Q.mp4",
"content_type": "video/mp4"
}
},
"error": null,
"cost": {
"amount_usd": 0.2255,
"currency": "usd",
"estimated": false,
"official_usd": 0.264,
"list_price_source": "official",
"list_price_label": "Official",
"saved_usd": 0.0385
},
"source": "api",
"created_at": "2026-10-15T09:21:04Z",
"started_at": "2026-10-15T09:21:06Z",
"completed_at": "2026-10-15T09:22:31Z"
}Price a request before you run it
The quote endpoint is free and needs no key. It returns the price, the official price and your discount for exactly the input you send.
curl -X POST https://cheapergen.com/api/v1/quote/sand-ai/magi-2/text-to-video \
-H "Content-Type: application/json" \
-d '{"prompt":"A paper boat drifts across a pond as rain taps against the surface.","aspect_ratio":"16:9","resolution":"1080p","duration":5}'| Status | Code | Meaning |
|---|---|---|
| 400 | invalid_request | A parameter is missing or out of range — the message names it. |
| 401 | unauthorized | Missing, malformed or revoked API key. |
| 402 | insufficient_balance | Not enough credits for the quoted price; top up or enable auto top-up. |
| 429 | rate_limited | Too many requests or generations in progress; back off and retry. |
| 502 | upstream_error | The model failed; the hold is refunded and you can retry. |
| 503 | model_unavailable | No healthy route right now; retry later or pick another model. |
FAQ