Tài liệu API
Tìm hiểu cách tích hợp Sora2 API để tạo các video tuyệt đẹp theo chương trình
💡 Sao chép và dán vào ChatGPT, Claude hoặc các trợ lý AI khác để thực hiện
2. Thực hiện yêu cầu đầu tiên
Sử dụng khóa API của bạn để xác thực và bắt đầu tạo video
// Make your first video generation request
async function generateVideo() {
const response = await fetch('https://sora2api.org/api/generate-video', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'A beautiful sunset over the ocean with gentle waves',
aspectRatio: '16:9',
duration: 10,
type: 'text2video'
})
});
const result = await response.json();
console.log('Task ID:', result.data.taskId);
console.log('Credits used:', result.data.creditsUsed);
return result.data.taskId; // Save this for status checking
}
generateVideo();3. Kiểm tra kết quả
Kiểm tra trạng thái của tác vụ tạo video và lấy URL video
// Complete example: Generate and poll for video
async function generateAndWaitForVideo() {
// Step 1: Start video generation
const generateResponse = await fetch('https://sora2api.org/api/generate-video', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'A beautiful sunset over the ocean with gentle waves',
aspectRatio: '16:9',
duration: 10,
type: 'text2video'
})
});
const generateResult = await generateResponse.json();
const taskId = generateResult.data.taskId;
console.log('Video generation started, Task ID:', taskId);
// Step 2: Poll for completion
return new Promise((resolve, reject) => {
const checkStatus = async () => {
const statusResponse = await fetch('https://sora2api.org/api/check-video-status', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({ taskId })
});
const statusResult = await statusResponse.json();
const { status, progress, videoUrl } = statusResult.data;
console.log(`Status: ${status}, Progress: ${progress}%`);
if (status === 'succeeded') {
console.log('Video ready! URL:', videoUrl);
resolve(videoUrl);
} else if (status === 'failed') {
reject(new Error('Video generation failed'));
} else {
// Still processing, check again in 5 seconds
setTimeout(checkStatus, 5000);
}
};
checkStatus();
});
}
// Usage
generateAndWaitForVideo()
.then(videoUrl => console.log('Final video URL:', videoUrl))
.catch(error => console.error('Error:', error));Bao gồm khóa API của bạn trong tiêu đề Authorization:
Authorization: Bearer YOUR_API_KEYVí dụ xác thực
curl -X POST https://sora2api.org/api/generate-video \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"prompt": "A cat playing", "aspectRatio": "16:9", "duration": 10, "type": "text2video"}'/api/generate-videoBắt đầu một tác vụ tạo video và trả về ID tác vụ
Tham số
promptrequiredMô tả văn bản hoặc lời nhắc hành động cho video (chuỗi, bắt buộc)
aspectRatiorequiredTỷ lệ khung hình video: "16:9" (ngang) hoặc "9:16" (dọc) (chuỗi, bắt buộc)
durationoptionalVideo duration in seconds: 10 or 15 (number, optional, default: 10). Cost depends on resolution: 480p 10s = 95 credits, 480p 15s = 143 credits, 720p 10s = 205 credits, 720p 15s = 308 credits
imageUrloptionalURL công khai của hình ảnh đã tải lên (chuỗi, tùy chọn, bắt buộc cho image2video)
Ví dụ yêu cầu
{
"prompt": "A beautiful sunset over the ocean",
"aspectRatio": "16:9",
"resolution": "480p",
"duration": 10, // optional, default 10, can be 10 or 15 seconds
"type": "text2video",
"imageUrl": "https://yourdomain.com/image.jpg" // optional, for image2video
}Ví dụ phản hồi
{
"code": 0,
"data": {
"success": true,
"taskId": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx",
"creditsUsed": 95,
"message": "Video generation started"
}
}/api/check-video-statusKiểm tra trạng thái và tiến trình của một tác vụ tạo video
Tham số
taskIdrequiredID tác vụ được trả về từ điểm cuối tạo video (chuỗi, bắt buộc)
Ví dụ yêu cầu
{
"taskId": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx"
}Ví dụ phản hồi
{
"code": 0,
"data": {
"status": "succeeded",
"progress": 100,
"videoUrl": "https://example.com/video.mp4",
"id": "xxxxxxxxxx-xxxx-xxxx-xxxxxxxxxx"
},
"msg": "success"
}Mã trạng thái
Tạo video từ mô tả văn bản:
async function generateTextToVideo() {
// Step 1: Generate video
const generateResponse = await fetch('https://sora2api.org/api/generate-video', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
prompt: 'A serene mountain landscape at sunrise',
aspectRatio: '16:9',
resolution: '480p', // optional: 480p or 720p, default 480p
duration: 10, // optional: 10 or 15 seconds
type: 'text2video'
})
});
const { data } = await generateResponse.json();
const taskId = data.taskId;
// Step 2: Poll for status
const pollStatus = async () => {
const statusResponse = await fetch('https://sora2api.org/api/check-video-status', {
method: 'POST',
headers: {
'Authorization': 'Bearer YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({ taskId })
});
const statusData = await statusResponse.json();
if (statusData.data.status === 'succeeded') {
console.log('Video URL:', statusData.data.videoUrl);
return statusData.data.videoUrl;
} else if (statusData.data.status === 'running') {
// Poll again after 3 seconds
setTimeout(pollStatus, 3000);
} else {
console.error('Generation failed');
}
};
pollStatus();
}Các mã lỗi thường gặp
401 UnauthorizedKhóa API không hợp lệ hoặc thiếu. Kiểm tra tiêu đề Authorization của bạn.
402 Payment RequiredKhông đủ tín dụng. Vui lòng mua thêm tín dụng để tiếp tục.
400 Bad RequestTham số yêu cầu không hợp lệ. Kiểm tra nội dung yêu cầu của bạn.
Chi phí mỗi video
95-308 Tín dụng
480p: 95 credits for 10s, 143 credits for 15s. 720p: 205 credits for 10s, 308 credits for 15s
Thời lượng video
10s / 15s
Chọn giữa 10 giây hoặc 15 giây
Kích thước ảnh tối đa
5MB
Để tạo hình ảnh thành video
Hiệu lực URL video
2 Giờ
Tải xuống trong khoảng thời gian này