Tổng quan
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://api.sangtao.ai/v2/images/generations \
-H "X-Api-Key: your-api-key-here" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-image-latest",
"prompt": "A vintage travel poster of Ha Long Bay at sunset",
"aspectRatio": "9:16",
"resolution": "2k"
}'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ạn | Mô tả |
|---|---|
| Khóa | Credit đượ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án | Khi hoàn thành, credit thừa được tự động hoàn trả |
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://api.sangtao.ai/v2/images/generations', {
method: 'POST',
headers: {
'X-Api-Key': 'your-api-key',
'Content-Type': 'application/json',
},
body: JSON.stringify({
model: 'gpt-image-latest',
prompt: 'A vintage travel poster of Ha Long Bay at sunset',
aspectRatio: '9:16',
resolution: '2K',
}),
})
const { data } = await res.json()
// 2. Poll until complete
const poll = setInterval(async () => {
const job = await fetch(`https://api.sangtao.ai/v2/images/generations/${data.jobId}`, {
headers: { 'X-Api-Key': 'your-api-key' },
}).then(r => r.json())
const status = job.data.status.toLowerCase()
if (status === 'complete') {
clearInterval(poll)
console.log('Image URL:', job.data.resultUrl)
} else if (status === 'error') {
clearInterval(poll)
console.error('Job failed:', job.data.error)
}
}, 5000) // Poll every 5 seconds