Darstellung
MAGI-2
MAGI-2 API · Image to Video
Animate an opening image and optionally guide the clip toward a specified final frame.
Choose a first frame with the composition you want at the start of the shot. Use the prompt to describe what changes over time, such as a subject turning or the camera moving closer. If you supply a last frame, describe a transition that can fit within the requested duration.
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
Replace the placeholder with a publicly accessible JPEG, PNG, or WebP image URL. The example uses the first frame to establish the output aspect ratio. Field names and supported settings depend on the MAGI-2 interface you use. Save this JSON as request.json.
json
{
"model": "magi-2",
"endpoint": "image-to-video",
"input": {
"first_frame_image": "https://example.com/greenhouse.png",
"prompt": "Slowly move between the leaves toward the greenhouse window. Leaves sway slightly while light rain taps against the glass.",
"duration_seconds": 5,
"resolution": "1080p",
"generate_audio": true
}
}Input fields
| Field | Purpose |
|---|---|
first_frame_image | Required opening-frame image URL. |
last_frame_image | Optional closing-frame image URL. |
prompt | Optional direction for movement, camera, and sound. |
duration_seconds, resolution, generate_audio | Output settings described in Text to Video. |
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.
If the image should guide appearance or style while allowing a different opening composition, use Reference to Video.
