Aspeto
MAGI-2
MAGI-2 API · Reference to Video
Use existing images, video clips, or audio to guide a new scene.
Describe the scene in a prompt and give each reference a role. An image can guide the subject or visual style, a video can demonstrate motion or camera work, and audio can guide the sound.
1. Prepare
The examples use https://api.tokatlas.com as the API origin. To run them, use an environment that exposes /v1/generations and an API key with access to MAGI-2.
bash
export API_KEY='YOUR_API_KEY'
export IDEMPOTENCY_KEY="$(uuidgen)"Choose one tab per step. See language setup for run commands. The Python examples use the standard library. Keep the same idempotency key and request.json when retrying a submission, and generate a new key for each new task.
2. Submit the task
Include at least one reference URL across the three media fields. Replace the placeholders with publicly accessible files. Field names and supported settings depend on the MAGI-2 interface you use. Save this JSON as request.json.
json
{
"model": "magi-2",
"endpoint": "reference-to-video",
"input": {
"prompt": "Keep the green ceramic cup from the image. Follow the slow camera movement in the video and use the audio as a guide for cafe ambience.",
"reference_images": [
"https://example.com/green-cup.png"
],
"reference_video": [
"https://example.com/camera-move.mp4"
],
"reference_audio": [
"https://example.com/cafe-ambience.wav"
],
"aspect_ratio": "16:9",
"duration_seconds": 5,
"resolution": "1080p",
"generate_audio": true
}
}Input fields
| Field | Example formats | What it guides |
|---|---|---|
reference_images | JPEG, PNG, WebP | Subject appearance, setting, or visual style. |
reference_video | MP4, MOV | Subject movement or camera direction. |
reference_audio | MP3, WAV | Voice, music, or ambient sound. |
The examples send request.json to POST /v1/generations.
bash
curl --fail-with-body --silent --show-error --max-time 180 \
--request POST \
--url "https://api.tokatlas.com/v1/generations" \
--header "Authorization: Bearer $API_KEY" \
--header "Idempotency-Key: $IDEMPOTENCY_KEY" \
--header 'Content-Type: application/json' \
--data-binary @request.jsonpython
import os
from pathlib import Path
from urllib.request import Request, urlopen
url = "https://api.tokatlas.com/v1/generations"
request = Request(url, data=Path("request.json").read_bytes(), headers={
"Authorization": "Bearer " + os.environ["API_KEY"],
"Idempotency-Key": os.environ["IDEMPOTENCY_KEY"],
"Content-Type": "application/json",
}, method="POST")
with urlopen(request, timeout=180) as response:
print(response.read().decode("utf-8"))js
import { readFile } from "node:fs/promises";
const { API_KEY, IDEMPOTENCY_KEY } = process.env;
const response = await fetch("https://api.tokatlas.com/v1/generations", {
method: "POST",
headers: {
Authorization: "Bearer " + API_KEY,
"Idempotency-Key": IDEMPOTENCY_KEY,
"Content-Type": "application/json",
},
body: await readFile("request.json"),
signal: AbortSignal.timeout(180_000),
});
const body = await response.text();
if (!response.ok) throw new Error(`HTTP ${response.status}: ${body}`);
console.log(body);java
import java.net.URI;
import java.net.http.*;
import java.nio.file.Path;
import java.time.Duration;
public class Example {
public static void main(String[] args) throws Exception {
URI uri = URI.create("https://api.tokatlas.com/v1/generations");
HttpRequest request = HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(180))
.header("Authorization", "Bearer " + System.getenv("API_KEY"))
.header("Idempotency-Key", System.getenv("IDEMPOTENCY_KEY"))
.header("Content-Type", "application/json")
.POST(HttpRequest.BodyPublishers.ofFile(Path.of("request.json")))
.build();
var response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) {
throw new IllegalStateException("HTTP " + response.statusCode() + ": " + response.body());
}
System.out.println(response.body());
}
}A new task typically returns HTTP 202. Repeating an accepted request may return HTTP 200 with the original task. Save generation.id from the response.
json
{
"generation": {
"id": "task_example",
"status": "processing"
}
}3. Query the task
Replace task_example with the full ID from the submission response, then call GET /v1/generations/{task_id}.
bash
export TASK_ID='task_example'bash
curl --fail-with-body --silent --show-error --max-time 60 \
--request GET \
--url "https://api.tokatlas.com/v1/generations/$TASK_ID" \
--header "Authorization: Bearer $API_KEY"python
import os
from urllib.request import Request, urlopen
url = "https://api.tokatlas.com/v1/generations/" + os.environ["TASK_ID"]
request = Request(url, headers={"Authorization": "Bearer " + os.environ["API_KEY"]})
with urlopen(request, timeout=60) as response:
print(response.read().decode("utf-8"))js
const { API_KEY, TASK_ID } = process.env;
const url = `https://api.tokatlas.com/v1/generations/${TASK_ID}`;
const response = await fetch(url, {
headers: { Authorization: "Bearer " + API_KEY },
signal: AbortSignal.timeout(60_000),
});
const body = await response.text();
if (!response.ok) throw new Error(`HTTP ${response.status}: ${body}`);
console.log(body);java
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
public class Example {
public static void main(String[] args) throws Exception {
URI uri = URI.create("https://api.tokatlas.com/v1/generations/" + System.getenv("TASK_ID"));
HttpRequest request = HttpRequest.newBuilder(uri)
.timeout(Duration.ofSeconds(60))
.header("Authorization", "Bearer " + System.getenv("API_KEY"))
.GET()
.build();
var response = HttpClient.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofString());
if (response.statusCode() / 100 != 2) {
throw new IllegalStateException("HTTP " + response.statusCode() + ": " + response.body());
}
System.out.println(response.body());
}
}generation.status | Next step |
|---|---|
processing | Poll the same task again after a delay. Increase the delay between checks. |
succeeded | Read the video asset from generation.result.assets. |
failed, canceled, refunded, expired | Stop polling and inspect the task response. |
A successful response includes the following fields.
json
{
"generation": {
"id": "task_example",
"status": "succeeded",
"result": {
"assets": [
{
"type": "video",
"url": "https://example.com/generated-video.mp4"
}
]
}
}
}4. Download the video
Find the video asset in generation.result.assets and set VIDEO_URL to its url. The download examples save the file as video.mp4.
bash
export VIDEO_URL='https://example.com/generated-video.mp4'bash
curl --fail --silent --show-error --location --max-time 180 \
"$VIDEO_URL" --output video.mp4python
import os
from pathlib import Path
from shutil import copyfileobj
from urllib.request import urlopen
with urlopen(os.environ["VIDEO_URL"], timeout=180) as response:
with Path("video.mp4").open("wb") as output:
copyfileobj(response, output)js
import { createWriteStream } from "node:fs";
import { pipeline } from "node:stream/promises";
const response = await fetch(process.env.VIDEO_URL, {
signal: AbortSignal.timeout(180_000),
});
if (!response.ok) throw new Error(`HTTP ${response.status}`);
await pipeline(response.body, createWriteStream("video.mp4"));java
import java.io.InputStream;
import java.net.URI;
import java.net.http.*;
import java.nio.file.*;
import java.time.Duration;
public class Example {
public static void main(String[] args) throws Exception {
HttpRequest request = HttpRequest.newBuilder(URI.create(System.getenv("VIDEO_URL")))
.timeout(Duration.ofSeconds(180))
.GET()
.build();
var response = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NORMAL)
.build()
.send(request, HttpResponse.BodyHandlers.ofInputStream());
try (InputStream video = response.body()) {
if (response.statusCode() / 100 != 2) {
throw new IllegalStateException("HTTP " + response.statusCode());
}
Files.copy(video, Path.of("video.mp4"), StandardCopyOption.REPLACE_EXISTING);
}
}
}Troubleshooting
- Submission times out. Retry with the same request body and
IDEMPOTENCY_KEY. - HTTP 400. Check the generation mode, required fields, and asset URLs. HTTP 401. Check the API origin and key.
- Lookup fails or polling times out. Keep
TASK_IDand query the same task later.
See Text to Video for output settings. To use an image as the opening frame, see Image to Video.
