Curl Code Example
# Create a video with sora-2
# Step 1: Submit the video generation request
echo "Submitting video generation request..."
response=$(curl -s https://api.cometapi.com/v1/videos \
-H "Authorization: Bearer $COMETAPI_KEY" \
-F "model=sora-2" \
-F "prompt=A calico cat playing a piano on stage")
echo "Response: $response"
# Extract video_id from response (handle JSON with spaces like "id": "xxx")
video_id=$(echo "$response" | tr -d '\n' | sed 's/.*"id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/')
echo "Video ID: $video_id"
# Step 2: Poll for progress until 100%
echo ""
echo "Checking video generation progress..."
while true; do
status_response=$(curl -s "https://api.cometapi.com/v1/videos/$video_id" \
-H "Authorization: Bearer $COMETAPI_KEY")
# Parse progress from "progress": "0%" format
progress=$(echo "$status_response" | grep -o '"progress":"[^"]*"' | head -1 | sed 's/"progress":"//;s/"$//')
# Parse status from the outer level
status=$(echo "$status_response" | grep -o '"status":"[^"]*"' | head -1 | sed 's/"status":"//;s/"$//')
echo "Progress: $progress, Status: $status"
if [ "$progress" = "100%" ]; then
echo "Video generation completed!"
break
fi
if [ "$status" = "FAILURE" ] || [ "$status" = "failed" ]; then
echo "Video generation failed!"
echo "$status_response"
exit 1
fi
sleep 10
done
# Step 3: Download the video to output directory
echo ""
echo "Downloading video to ./output/$video_id.mp4..."
mkdir -p ./output
curl -s "https://api.cometapi.com/v1/videos/$video_id/content" \
-H "Authorization: Bearer $COMETAPI_KEY" \
-o "./output/$video_id.mp4"
if [ -f "./output/$video_id.mp4" ]; then
echo "Video saved to ./output/$video_id.mp4"
ls -la "./output/$video_id.mp4"
else
echo "Failed to download video"
exit 1
fi
Python Code Example
# Create a video with sora-2 using raw HTTP requests
import os
import time
import requests
api_key = os.environ.get("COMETAPI_KEY")
base_url = "https://api.cometapi.com/v1"
headers = {"Authorization": f"Bearer {api_key}"}
# Step 1: Submit the video generation request
print("Submitting video generation request...")
response = requests.post(
f"{base_url}/videos",
headers=headers,
files={
"model": (None, "sora-2"),
"prompt": (None, "A calico cat playing a piano on stage"),
},
)
result = response.json()
print(f"Response: {result}")
video_id = result.get("id")
print(f"Video ID: {video_id}")
# Step 2: Poll for progress until 100%
print("\nChecking video generation progress...")
while True:
try:
status_response = requests.get(f"{base_url}/videos/{video_id}", headers=headers)
status_result = status_response.json()
# Parse progress and status from response
data = status_result.get("data", {})
if data is None:
data = {}
progress = data.get("progress", "0%")
status = data.get("status", "unknown")
print(f"Progress: {progress}, Status: {status}")
if status in ["FAILURE", "failed"]:
print("Video generation failed!")
print(status_result)
exit(1)
if progress == "100%":
print("Video generation completed!")
break
except Exception as e:
print(f"Temporary error: {e}, retrying...")
time.sleep(10)
# Step 3: Download the video to output directory
print(f"\nDownloading video to ./output/{video_id}.mp4...")
os.makedirs("./output", exist_ok=True)
video_response = requests.get(f"{base_url}/videos/{video_id}/content", headers=headers)
output_path = f"./output/{video_id}.mp4"
with open(output_path, "wb") as f:
f.write(video_response.content)
if os.path.exists(output_path):
file_size = os.path.getsize(output_path)
print(f"Video saved to {output_path}")
print(f"File size: {file_size} bytes")
else:
print("Failed to download video")
exit(1)
JavaScript Code Example
// Create a video with sora-2 using raw HTTP requests
import fs from "fs";
import path from "path";
const apiKey = process.env.COMETAPI_KEY;
const baseUrl = "https://api.cometapi.com/v1";
async function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
async function main() {
// Step 1: Submit the video generation request
console.log("Submitting video generation request...");
const formData = new FormData();
formData.append("model", "sora-2");
formData.append("prompt", "A calico cat playing a piano on stage");
const submitResponse = await fetch(`${baseUrl}/videos`, {
method: "POST",
headers: {
Authorization: `Bearer ${apiKey}`,
},
body: formData,
});
const result = await submitResponse.json();
console.log("Response:", JSON.stringify(result, null, 2));
const videoId = result.id;
console.log("Video ID:", videoId);
// Step 2: Poll for progress until 100%
console.log("\nChecking video generation progress...");
while (true) {
try {
const statusResponse = await fetch(`${baseUrl}/videos/${videoId}`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
const text = await statusResponse.text();
if (text.startsWith("<")) {
console.log("Temporary server error, retrying...");
await sleep(10000);
continue;
}
const statusResult = JSON.parse(text);
// Parse progress and status from response
const data = statusResult.data || {};
const progress = data.progress || "0%";
const status = data.status || "unknown";
console.log(`Progress: ${progress}, Status: ${status}`);
if (status === "FAILURE" || status === "failed") {
console.log("Video generation failed!");
console.log(JSON.stringify(statusResult, null, 2));
process.exit(1);
}
if (progress === "100%") {
console.log("Video generation completed!");
break;
}
} catch (e) {
console.log(`Temporary error: ${e.message}, retrying...`);
}
await sleep(10000);
}
// Step 3: Download the video to output directory
console.log(`\nDownloading video to ./output/${videoId}.mp4...`);
const outputDir = "./output";
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
const videoResponse = await fetch(`${baseUrl}/videos/${videoId}/content`, {
headers: {
Authorization: `Bearer ${apiKey}`,
},
});
const outputPath = path.join(outputDir, `${videoId}.mp4`);
const videoBuffer = Buffer.from(await videoResponse.arrayBuffer());
fs.writeFileSync(outputPath, videoBuffer);
if (fs.existsSync(outputPath)) {
const stats = fs.statSync(outputPath);
console.log(`Video saved to ${outputPath}`);
console.log(`File size: ${stats.size} bytes`);
} else {
console.log("Failed to download video");
process.exit(1);
}
}
main().catch(console.error);