Tạo ảnh AI

POSThttps://sangtao.ai/api/v2/images/generate

Tài liệu cho Tạo ảnh AI.

Tạo ảnh AI từ mô tả text. Trả về jobId — poll GET /api/v2/jobs/{jobId} để lấy kết quả.

Nội dung request

FieldTypeDescription
model*stringModel slug từ GET /api/v2/models?category=Image
prompt*stringMô tả hình ảnh cần tạo
numImagesintSố ảnh tạo ra (1-4). Mặc định: 1
sizestringKích thước ảnh. Phụ thuộc model, ví dụ "1024x1024"
refImageUrlsstring[]URL ảnh tham chiếu. Upload trước qua POST /api/v2/images/upload
tierstringTier tạo ảnh: "standard" (mặc định, 1x credit) hoặc "vip1" (chất lượng cao hơn, 5x credit). Ảnh hưởng routing và chất lượng
optionsobjectOptions theo model. Keys từ GET /api/v2/models/{slug} config. Ví dụ: aspectRatio, outputFormat, resolution, style
Tip: Dùng GET /api/v2/models?category=Image để xem danh sách model và config options.

cURL

bash
curl -X POST https://sangtao.ai/api/v2/images/generate \
  -H "X-Api-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "nano-banana-pro",
    "prompt": "A cozy Vietnamese coffee shop in Hanoi Old Quarter, morning light",
    "numImages": 2,
    "options": { "aspectRatio": "16:9" }
  }'

JSON Body

json
{
  "model": "nano-banana-pro",
  "prompt": "A cozy Vietnamese coffee shop in Hanoi Old Quarter, morning light",
  "numImages": 2,
  "options": {
    "aspectRatio": "16:9",
    "outputFormat": "jpeg",
    "resolution": "1K"
  }
}

Phản hồi

Trả về jobId. Poll endpoint job status để lấy kết quả:

json
{
  "success": true,
  "data": {
    "jobId": "img-abc123"
  }
}

Theo dõi tiến độ

Poll GET /api/v2/jobs/{jobId} mỗi 3 giây cho đến khi status là "complete" hoặc "error".

json
// GET /api/v2/jobs/{jobId}
{
  "success": true,
  "data": {
    "status": "complete",
    "resultImages": [
      "https://cdn.sangtao.ai/images/result1.jpg",
      "https://cdn.sangtao.ai/images/result2.jpg"
    ]
  }
}
Note: Kết quả ảnh nằm trong mảng resultImages. Mỗi URL là link trực tiếp đến ảnh đã tạo.

Mã lỗi

MãKhi nào
400Input không hợp lệ (story quá ngắn/dài, model/voice không tồn tại...)
401Chưa xác thực — thiếu hoặc sai API key / token
402Không đủ credit hoặc voice cao cấp cần nạp tiền
429Đã có 4 job đang chạy cùng lúc
503Hệ thống đang xử lý nhiều yêu cầu cùng lúc. Vui lòng thử lại sau 30 giây