# MAGI-2 API - Agent Integration Guide

API Platform: MAGI-2 API (https://magi2api.net)
Model Served: Sand.ai MAGI-2 (`model: "magi-2"`)
API Base URL: https://api.beatapi.io
API Docs: https://docs.beatapi.io/api-reference/video-api/magi-2
OpenAPI 3.1 Schema: https://magi2api.net/openapi.json
Support: support@magi2api.net

## Identity & Authentication

- MAGI-2 API is an independent API platform providing REST API access to Sand.ai's MAGI-2 audio-video model.
- Pass Bearer token on every request: `Authorization: Bearer $MAGI2_API_KEY`.
- Never hardcode `MAGI2_API_KEY` inside source repositories or client bundles.
- Read `MAGI2_API_KEY` from server environment variables; manage keys at https://magi2api.net/settings/apikeys and balance at https://magi2api.net/settings/credits.

## Core Endpoints

1. `POST https://api.beatapi.io/v1/videos/tasks`
   - Returns HTTP `201 Created` with `data.id` (task ID) and `poll_after_seconds` (default `8`).
   - Send `Idempotency-Key` (max 255 chars, 24h dedupe window) so network retries never double-bill.
2. `GET https://api.beatapi.io/v1/tasks/{task_id}`
   - Returns `data.status` (`queued`, `processing`, `succeeded`, `failed`) and watermark-free `data.output.url` on completion.
3. `POST https://api.beatapi.io/v1/files`
   - Uploads local images/audio (max 50 MB) or video (max 100 MB) to public HTTPS URLs usable in task inputs.
4. `GET https://api.beatapi.io/v1/usage?period=7d`
   - Queries credit consumption and task counts (`24h`, `7d`, `30d`, `all`).
5. `POST https://api.beatapi.io/v1/webhooks`
   - Registers webhook notifications as an alternative or complement to polling.

## Strict Schema Rules for `model: "magi-2"`

- `model`: Exact literal `"magi-2"`.
- `prompt`: Required string, 1-5000 characters. Put spoken lines inside quotes for tighter bilingual lip-sync.
- `duration`: Optional integer from `4` to `30` inclusive (default `5`).
- `resolution`: Strictly `"1080p"` at `25 fps`. Never send `"480p"` or `"720p"` (returns HTTP 422).
- `aspect_ratio`: Enum `16:9` (default), `9:16`, `1:1`, `4:3`, `3:4`, `21:9`.
- `generate_audio`: Boolean (default `true`). Generates native synchronized bilingual dialogue, foley, and music.
- `seed`: Optional integer `0-2147483647`.

## Mutual-Exclusion & Input Mode Matrix

- **Text-to-Video**: Omit `images` and omit all `reference_*` arrays.
- **First-Frame I2V**: Supply `images: [firstFrameHttpsUrl]`. Omit `aspect_ratio` and omit `reference_*`.
- **First-Last Keyframes**: Supply `images: [startHttpsUrl, endHttpsUrl]` (matched aspect ratio). Omit `aspect_ratio` and omit `reference_*`.
- **Multimodal References**: Supply up to 10 `reference_images`, 5 `reference_videos` (2-30s each, 30s combined), and 5 `reference_audios` (2-30s each, 30s combined). Reference them in `prompt` as `@image1..@image10`, `@video1..@video5`, `@audio1..@audio5`. Omit `images`.

## Billing & Media Lifecycle Rules

- Flat rate on MAGI-2 API: `$0.088/second` of output 1080p video (`4s=$0.352`, `5s=$0.440`, `8s=$0.704`, `15s=$1.320`, `30s=$2.640`).
- Reference images, reference videos, and reference audio add `$0` extra fee.
- Tasks terminating in `failed` automatically refund `100%` of deducted credits.
- Download and persist watermark-free MP4 assets to durable storage (R2/S3) within **72 hours** of task completion.
