MAGI-2 API Integration Guide
From API key to a saved MP4 in five steps, plus the production details: media inputs, error handling, retries and credit control. Every example works as-is once you add your key.
# Submit a text-to-video task
$ curl -X POST 'https://magi2.studio/v1/generate' -H "Authorization: Bearer $MAGI2_API_KEY" -H 'Content-Type: application/json' -d '{"prompt": "A street musician plays violin under a bridge at night", "duration": 8}'
{"code":200,"message":"success","data":{"task_id":"n89a1b2c3d4e5f60718293a4b5c6d7e8f90magi2","status":"IN_PROGRESS","consumed_credits":144}}
# Poll the status until it reaches SUCCESS
$ curl 'https://magi2.studio/v1/status?task_id=n89a1b2c3d4e5f60718293a4b5c6d7e8f90magi2' -H "Authorization: Bearer $MAGI2_API_KEY"
{"code":200,"message":"success","data":{"status":"IN_PROGRESS","response":[]}}
{"code":200,"message":"success","data":{"status":"SUCCESS","response":["https://cdn.example.com/videos/your-video.mp4"]}}
# Download the MP4
$ curl -L -o magi2-video.mp4 'https://cdn.example.com/videos/your-video.mp4'
✓ magi2-video.mp41. Create an API key
Create a key in the dashboard and keep it in an environment variable or secret manager on your server. → Open API keys
Use one key per app or environment. Each key keeps its own task history, so the API call logs stay easy to read, and you can revoke one key without breaking the others.
2. Pick a mode and write the request
There is no mode switch. The API reads which inputs you sent:
- text-to-video — send only a prompt.
- image-to-video — send first_frame_url (and optionally last_frame_url). The clip keeps the first frame's aspect ratio.
- reference-to-video — send a prompt plus image_urls, video_urls and/or audio_urls to keep a character, product, place, motion or voice consistent.
MAGI-2 responds best to prompts that read like a shot description: framing, subject, action, camera movement, light, then the sound you want to hear.
3. Submit the task
POST the JSON to /v1/generate. The call returns in seconds with a task_id; credits are charged at this point.
A successful response:
Important: every successful submission is billed. If a request times out on your side, check /v1/tasks for a new task before sending it again, so you do not pay twice.
4. Poll the status
Call /v1/status with the task_id until status is SUCCESS or FAILED.
- Wait 15–30 seconds between calls. Most clips finish within a few minutes; long clips take longer.
- Stop polling after about 30 minutes and check the task later instead of creating a new one.
- FAILED tasks are refunded automatically; error_message explains why.
5. Save the video
When status is SUCCESS, response[0] is the MP4 URL. Download it and store it in your own storage or CDN; treat our URL as a delivery link, not permanent hosting.
Complete example
Create, wait with back-off, and download, with the error cases handled.
Preparing images, videos and audio
- Every input is a public http(s) URL that downloads the file directly. Links behind a login, expiring too soon, or pointing to an HTML page will fail.
- Use common formats: JPG, PNG or WebP images, MP4 video, MP3 or WAV audio. Upload them to your own bucket or CDN first.
- Reference mode takes up to 10 images, 5 videos and 5 audio clips. Frames and references cannot be combined in one request.
- For image-to-video, crop the first frame to the shape you want: the output follows its aspect ratio.
Handling errors
Read the HTTP status and the message field. Retry only when the problem is temporary.
| HTTP | What to do |
|---|---|
| 400 | Fix the request using the message (range, URL, or a frames/references mix). Do not retry unchanged. Nothing was charged. |
| 401 | Check the Authorization header and that the key was not revoked. |
| 402 | Not enough credits. Top up, then retry. |
| 404 | The task_id is wrong or was created with another key. |
| 5xx / timeout | Temporary. For /v1/status, back off and keep polling. For /v1/generate, look in /v1/tasks first; resubmit only if no task was created. |
Controlling spend
- Cost is predictable: duration × 18 credits in every mode (10 seconds = 180 credits). Audio and reference inputs do not change the price.
- consumed_credits on each task is the amount charged; it drops to 0 when a failed task is refunded.
- The API and the browser studio share one balance. Test prompts at short durations before running long clips.
Debug with API call logs
The dashboard lists every task per API key with the exact request, status, credits, error and video, and refreshes running tasks automatically. → Open API call logs
Production checklist
- The API key lives only on the server, in an environment variable or secret manager.
- You check code === 200 before reading data.
- You store task_id right after submitting, so a crash never leads to a paid duplicate.
- Polling waits 15–30 seconds and backs off on network or 5xx errors.
- Finished videos are copied to your own storage.
- Your UI shows error_message for failed tasks and tells users the credits were refunded.
→ Using an AI agent? Connect it through the MCP server
MAGI-2 is a video model published by Sand.ai. Magi2 Studio is an independent service that runs it through a third-party API provider; it is not operated by, affiliated with or endorsed by Sand.ai.