📚 Tài liệu REST API

Tách nền ảnh bằng AI ngay trong ứng dụng của bạn. Xác thực bằng API key, không cần SDK — mọi ngôn ngữ có HTTP client đều dùng được.

Swagger UI tương tác: /docs · OpenAPI JSON: /openapi.json

Bắt đầu nhanh

  1. Đăng ký tài khoản tại Dashboard — hệ thống cấp ngay 1 API key (gói Free, 50 credits).
  2. Copy API key (dạng rbg_...) — key chỉ hiển thị một lần, hãy lưu ngay.
  3. Gửi ảnh tới endpoint POST /v1/remove-background với header X-API-Key.
Base URL:
  • Production: http://removebg.charmevietnam.com//v1 (thay bằng domain thật khi deploy)
  • Môi trường dev: http://localhost:8000/v1

Xác thực

Mọi request tới /v1/* đều yêu cầu header X-API-Key:

curl "http://localhost:8000/v1/account/credits" \
  -H "X-API-Key: rbg_your_api_key_here"
import requests

headers = {"X-API-Key": "rbg_your_api_key_here"}
response = requests.get("http://localhost:8000/v1/account/credits", headers=headers)
print(response.json())

Key được lưu trong DB dạng SHA256 hash — nếu làm mất, hãy thu hồi key cũ và tạo key mới trong Dashboard.

Gói dịch vụ & giới hạn

GóiRequests / thángRequests / phút
Free50 credits5
Pro5.000 credits60
EnterpriseUnlimitedUnlimited

Mỗi lần tách nền thành công trừ 1 credit. Khi hết credit, API trả về mã 402.

POST /v1/remove-background

Tách nền từ file ảnh upload. Body dạng multipart/form-data.

Tham số

Tham sốBắt buộcMặc địnhMô tả
image✓—File ảnh: JPG, PNG, WEBP — tối đa 10MB
return_format—pngpng | jpg | base64
size—autoauto (gốc) | full (gốc) | preview (~0.25MP)
bg_color—trong suốtMàu nền thay thế, vd #ffffff, red, rgb(59,130,246)
model—autohigh (ISNet, viền sạch) | ultra (BiRefNet, sắc nét nhất) | portrait (ảnh người) | anime | fast | tên model rembg vd isnet-general-use
alpha_matting—theo servertrue = viền mềm hơn với tóc, lông (xử lý chậm hơn)

Ví dụ

curl -X POST "http://localhost:8000/v1/remove-background" \
  -H "X-API-Key: rbg_your_api_key_here" \
  -F "image=@photo.jpg" \
  -F "return_format=png" \
  -F "size=auto" \
  -F "model=high" \
  -F "alpha_matting=true"
import requests

url = "http://localhost:8000/v1/remove-background"
headers = {"X-API-Key": "rbg_your_api_key_here"}
files = {"image": open("photo.jpg", "rb")}
data = {"return_format": "png", "size": "auto"}

response = requests.post(url, headers=headers, files=files, data=data)
result = response.json()
print(result["data"]["image_url"])
const form = new FormData();
form.append("image", fileInput.files[0]);
form.append("return_format", "png");

const res = await fetch("http://localhost:8000/v1/remove-background", {
  method: "POST",
  headers: { "X-API-Key": "rbg_your_api_key_here" },
  body: form,
});
const result = await res.json();
console.log(result.data.image_url);
<?php
$ch = curl_init('http://localhost:8000/v1/remove-background');
curl_setopt_array($ch, [
    CURLOPT_POST           => true,
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER     => ['X-API-Key: rbg_your_api_key_here'],
    CURLOPT_POSTFIELDS     => [
        'image'         => new CURLFile('photo.jpg'),
        'return_format' => 'png',
    ],
]);
echo curl_exec($ch);

Response 200

{
  "success": true,
  "data": {
    "image_url": "http://localhost:8000/results/9f2c7a4e....png",
    "base64": null,
    "credits_used": 1,
    "credits_remaining": 99,
    "processing_time": "1.2s",
    "image_size": "1920x1080"
  },
  "message": "Background removed successfully"
}

Với return_format=base64, trường base64 chứa dữ liệu ảnh mã hóa base64 (kèm image_url như thường).

POST /v1/remove-background-url

Tách nền từ URL ảnh công khai. Body dạng JSON.

curl -X POST "http://localhost:8000/v1/remove-background-url" \
  -H "X-API-Key: rbg_your_api_key_here" \
  -H "Content-Type: application/json" \
  -d '{"image_url": "https://example.com/photo.jpg", "size": "auto", "model": "high", "alpha_matting": true}'
import requests

url = "http://localhost:8000/v1/remove-background-url"
headers = {
    "X-API-Key": "rbg_your_api_key_here",
    "Content-Type": "application/json",
}
payload = {"image_url": "https://example.com/photo.jpg"}

response = requests.post(url, headers=headers, json=payload)
print(response.json())

Response 200

{
  "success": true,
  "data": {
    "image_url": "http://localhost:8000/results/3ab19d0e....png",
    "base64": null,
    "credits_used": 1,
    "credits_remaining": 98,
    "processing_time": "1.4s",
    "image_size": "1600x1200"
  },
  "message": "Background removed successfully"
}

Server chặn URL trỏ tới địa chỉ nội bộ/private (SSRF) và ảnh lớn hơn 10MB.

GET /v1/account/credits

Kiểm tra số credits còn lại của API key.

curl "http://localhost:8000/v1/account/credits" \
  -H "X-API-Key: rbg_your_api_key_here"

Response 200

{
  "success": true,
  "data": {
    "total_credits": 100,
    "used_credits": 25,
    "remaining_credits": 75,
    "plan": "free"
  },
  "message": "OK"
}

GET /v1/account/usage

Lịch sử sử dụng API của key, hỗ trợ phân trang và lọc theo ngày.

Query paramMặc địnhMô tả
page1Số trang
limit20Số dòng mỗi trang (1–100)
date_from—YYYY-MM-DD
date_to—YYYY-MM-DD
curl "http://localhost:8000/v1/account/usage?page=1&limit=20&date_from=2026-01-01" \
  -H "X-API-Key: rbg_your_api_key_here"

Response 200

{
  "success": true,
  "data": {
    "usage": [
      {
        "id": "req_9f2c7a4e5b1d4c3a8e7f2b1d0a9c8e7f",
        "endpoint": "/v1/remove-background",
        "timestamp": "2026-01-15T10:30:00Z",
        "credits_used": 1,
        "image_size": "1920x1080",
        "status": "success",
        "processing_time": 1.21
      }
    ],
    "total": 25,
    "page": 1,
    "limit": 20
  },
  "message": "OK"
}

Mã lỗi

MãÝ nghĩaNguyên nhân thường gặp
400Bad requestSai định dạng ảnh, URL không tải được, tham số không hợp lệ
401UnauthorizedThiếu header X-API-Key, key sai hoặc đã thu hồi
402Out of creditsKey đã hết credits — nâng cấp gói hoặc tạo key mới
413Payload too largeẢnh vượt giới hạn 10MB
422Validation errorThiếu tham số bắt buộc, sai kiểu dữ liệu
429Rate limit exceededVượt số request/phút của gói — xem header Retry-After
500Server errorLỗi xử lý phía server — thử lại sau

Dạng lỗi chung

{ "success": false, "error": "Invalid API key" }

Lưu ý quan trọng