存取完整的範例程式碼和 API 資源,以簡化您的 MiniMax H3 Max 整合流程。我們詳盡的文件提供逐步指引,協助您在專案中充分發揮 MiniMax H3 Max 的潛力。
cURL Code Example
set -euo pipefail
# 1. Create the task.
task=$(curl --fail-with-body --silent --show-error \
https://api.cometapi.com/v1/videos \
-H "Authorization: Bearer $COMETAPI_KEY" \
--form-string 'model=minimax-h3-max' \
--form-string 'prompt=A cinematic view of clouds moving over green mountains, locked camera, no text.' \
--form-string 'seconds=5' \
--form-string 'size=1344x768')
# Add --form-string 'images=https://example.com/reference.jpg' for image-to-video.
task_id=$(jq -r '.id' <<<"$task")
echo "Task: ${task_id}"
# 2. Poll until the task finishes.
while true; do
task=$(curl --fail-with-body --silent --show-error \
--retry 3 --retry-all-errors --retry-delay 2 \
"https://api.cometapi.com/v1/videos/${task_id}" \
-H "Authorization: Bearer $COMETAPI_KEY")
status=$(jq -r '.status' <<<"$task")
echo "Status: $status"
[[ "$status" == "failed" ]] && { echo "$task" >&2; exit 1; }
[[ "$status" == "completed" ]] && break
sleep 10
done
# 3. Download the completed video.
curl --fail-with-body --silent --show-error \
--retry 3 --retry-all-errors --retry-delay 2 \
"https://api.cometapi.com/v1/videos/${task_id}/content" \
-H "Authorization: Bearer $COMETAPI_KEY" \
--output "${task_id}.mp4"
echo "Saved: ${task_id}.mp4"
Python Code Example
import os
import time
from pathlib import Path
import requests
api_key = os.environ["COMETAPI_KEY"]
base_url = "https://api.cometapi.com"
headers = {"Authorization": f"Bearer {api_key}"}
def get(path: str, timeout: int) -> requests.Response:
for attempt in range(3):
try:
response = requests.get(
f"{base_url}{path}", headers=headers, timeout=timeout
)
response.raise_for_status()
return response
except requests.RequestException:
if attempt == 2:
raise
time.sleep(2)
raise RuntimeError("unreachable")
# 1. Create the task.
form = {
"model": (None, "minimax-h3-max"),
"prompt": (
None,
"A cinematic view of clouds moving over green mountains, "
"locked camera, no text.",
),
"seconds": (None, "5"),
"size": (None, "1344x768"),
}
if image_url := os.getenv("MINIMAX_H3_IMAGE_URL"):
form["images"] = (None, image_url)
response = requests.post(
f"{base_url}/v1/videos",
headers=headers,
files=form,
timeout=60,
)
response.raise_for_status()
task_id = response.json()["id"]
print(f"Task: {task_id}")
# 2. Poll until the task finishes.
while True:
response = get(f"/v1/videos/{task_id}", timeout=30)
task = response.json()
status = task["status"]
print(f"Status: {status}")
if status == "failed":
raise RuntimeError(task)
if status == "completed":
break
time.sleep(10)
# 3. Download the completed video.
response = get(f"/v1/videos/{task_id}/content", timeout=300)
output = Path(f"{task_id}.mp4")
output.write_bytes(response.content)
print(f"Saved: {output}")
JavaScript Code Example
import fs from "node:fs";
const apiKey = process.env.COMETAPI_KEY;
const baseUrl = "https://api.cometapi.com";
const headers = { Authorization: `Bearer ${apiKey}` };
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
async function get(path) {
for (let attempt = 0; attempt < 3; attempt += 1) {
try {
const result = await fetch(`${baseUrl}${path}`, { headers });
if (!result.ok) throw new Error(`${result.status} ${await result.text()}`);
return result;
} catch (error) {
if (attempt === 2) throw error;
await sleep(2000);
}
}
throw new Error("unreachable");
}
// 1. Create the task.
const form = new FormData();
form.append("model", "minimax-h3-max");
form.append(
"prompt",
"A cinematic view of clouds moving over green mountains, locked camera, no text.",
);
form.append("seconds", "5");
form.append("size", "1344x768");
if (process.env.MINIMAX_H3_IMAGE_URL) {
form.append("images", process.env.MINIMAX_H3_IMAGE_URL);
}
let response = await fetch(`${baseUrl}/v1/videos`, {
method: "POST",
headers,
body: form,
});
if (!response.ok) throw new Error(`${response.status} ${await response.text()}`);
const taskId = (await response.json()).id;
console.log(`Task: ${taskId}`);
// 2. Poll until the task finishes.
while (true) {
response = await get(`/v1/videos/${taskId}`);
const task = await response.json();
console.log(`Status: ${task.status}`);
if (task.status === "failed") throw new Error(JSON.stringify(task));
if (task.status === "completed") break;
await sleep(10000);
}
// 3. Download the completed video.
response = await get(`/v1/videos/${taskId}/content`);
const output = `${taskId}.mp4`;
fs.writeFileSync(output, Buffer.from(await response.arrayBuffer()));
console.log(`Saved: ${output}`);