Xác thực

Tài liệu cho Xác thực.

Xác thực bằng API Key

Cách xác thực được khuyến nghị. Vào Cài đặt → API Key để tạo key. Gửi kèm trong mọi request qua header X-Api-Key:

cURL
curl -X POST https://sangtao.ai/api/v2/render/video \
  -H "X-Api-Key: your-api-key-here" \
  -H "Content-Type: application/json" \
  -d '{ ... }'
Important: API key là bí mật. Không được để lộ trong code client-side (trình duyệt, app di động). Chỉ sử dụng ở server-side.

Hệ thống Credit

Mỗi job tiêu tốn credit. Kiểm tra số dư trước khi gửi:

JSON
GET /api/v2/account/balance

// Response
{
  "freeCredit": 100,      // Credit từ gói hàng tháng
  "purchasedCredit": 50,  // Credit đã mua
  "lockedCredit": 10,     // Đang giữ cho job đang chạy
  "availableCredit": 140  // free + purchased - locked
}
Giai đoạnMô tả
KhóaCredit được ước tính và khóa khi bạn gửi job
Xử lýJob chạy. Chi phí thực tế có thể thấp hơn ước tính
Quyết toánKhi hoàn thành, credit thừa được tự động hoàn trả
Note: Thứ tự trừ: free credit trước, purchased credit sau.

Theo dõi tiến độ Job

Tất cả endpoint tạo nội dung trả về jobId. Poll endpoint job status để theo dõi tiến độ:

pending→processing→complete|error
JavaScript
// 1. Submit job
const res = await fetch('https://sangtao.ai/api/v2/voiceover-videos/create', {
  method: 'POST',
  headers: {
    'X-Api-Key': 'your-api-key',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({ story: 'Hello world', voiceId: 'hoai-my' }),
})
const { data } = await res.json()

// 2. Poll until complete
const poll = setInterval(async () => {
  const job = await fetch(`https://sangtao.ai/api/v2/jobs/${data.jobId}`, {
    headers: { 'X-Api-Key': 'your-api-key' },
  }).then(r => r.json())

  if (job.data.status === 'complete') {
    clearInterval(poll)
    console.log('Video URL:', job.data.result)
  } else if (job.data.status === 'error') {
    clearInterval(poll)
    console.error('Job failed:', job.data.errorMessage)
  }
}, 4000) // Poll every 4 seconds
Tip: Trong lúc processing, field step cho biết tiến độ hiện tại (VD: "generating_images_2_of_5"). Dùng nó để hiển thị indicator chi tiết.