API การลบพื้นหลัง RemBG – เอกสารสำหรับนักพัฒนาและคู่มือการบูรณาการ






OpenAPI · สนามเด็กเล่นสด

สำรวจ API เต็มรูปแบบ—เรียกใช้คำขอจริงในเบราว์เซอร์ของคุณ

เรียกดูปลายทาง พารามิเตอร์ และการตอบกลับทั้งหมด ลองใช้คำขอด้วยคีย์ API ของคุณ จากนั้นรับข้อมูลจำเพาะดิบที่ /api/openapi ได้ตลอดเวลา

เปิดการอ้างอิงเชิงโต้ตอบ

การติดตั้ง rembg.js

latest npm versionGitHub starsnpm downloadslicenseGitHub issues

npm i @remove-background-ai/rembg.js
                        

API Reference for rembg.js

@remove-background-ai/rembg.js is a zero-config Node.js wrapper for the free Rembg API, enabling background removal with simple, customizable parameters.

Parameters for RemBG.js
ParameterTypeRequiredDefaultDescription
apiKeystring
Required

Your Rembg API key
inputImagestring | Buffer | { base64: string }
Required

Image file, buffer, or base64 payload
onDownloadProgress(event) => void
Optional

Hook for upload progress events
onUploadProgress(event) => void
Optional

Hook for download progress events
options.formatwebp (default) | png
Optional
webp
Specifies the output image format. Either "webp" (default) or "png"
options.returnBase64boolean
Optional
false
Return Base64 string instead of file
options.returnMaskboolean
Optional
false
Return only the alpha mask
options.wnumber
Optional

Target width (maintains aspect ratio)
options.hnumber
Optional

Target height (maintains aspect ratio)
options.exact_resizeboolean
Optional
false
Force exact width × height (may distort)
options.anglenumber
Optional
0
Rotation angle in degrees
options.expandboolean
Optional
true
Add padding so rotated images aren’t cropped
options.bg_colorstring
Optional
-
Optional solid background color in hex (e.g. #FFFFFFFF) or named color (e.g. "red", "blue")
  • การตั้งค่าสภาพแวดล้อมของคุณ: ตรวจสอบให้แน่ใจว่าคุณมีไฟล์ .env ในรูทโปรเจ็กต์ที่มีคีย์ API ของคุณ

  • การนำเข้าโมดูลที่จำเป็น: เริ่มต้นด้วยการนำเข้าฟังก์ชัน rembg จาก @remove-พื้นหลัง-ai/rembg.js และโมดูล dotenv เพื่อจัดการตัวแปรสภาพแวดล้อม

  • การกำหนดค่าการเรียกกลับความคืบหน้า: ไลบรารีเสนอการโทรกลับ onDownloadProgress และ onUploadProgress เพื่อติดตามความคืบหน้าของการทำงานของไฟล์ ในตัวอย่างที่ให้มา เรากำลังบันทึกเหตุการณ์เหล่านี้ไปยังคอนโซลโดยตรง

ตอนนี้เรามาดูตัวอย่างการใช้งานโดยละเอียดกันดีกว่า:


// script.mjs file

import  { rembg } from '@remove-background-ai/rembg.js';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();

// API_KEY will be loaded from the .env file
const API_KEY = process.env.API_KEY;

// log upload and download progress
const onDownloadProgress = console.log;
const onUploadProgress = console.log;
rembg({
    apiKey: API_KEY,
    inputImage: './input.png', //inputImage can be one of these: string | Buffer | { base64: string };
    onDownloadProgress,
    onUploadProgress
}).then(({ outputImagePath, cleanup }) => {
    console.log('✅🎉 background removed and saved under path=', outputImagePath);
    // if called, it will cleanup (remove from disk) your removed background image
    // cleanup();
});
					
						

โปรดจำไว้ว่า คุณสามารถเรียกใช้ฟังก์ชันล้างข้อมูลได้หากคุณต้องการลบอิมเมจที่ประมวลผลออกจากดิสก์ของคุณหลังจากลบพื้นหลังแล้ว

กำลังแสดงแถบความคืบหน้า

เมื่อผสานรวมบริการลบพื้นหลัง การให้ข้อเสนอแนะแก่ผู้ใช้เกี่ยวกับความคืบหน้าของคำขออัปโหลดหรือดาวน์โหลดมักจะเป็นประโยชน์ เพื่ออำนวยความสะดวกในเรื่องนี้ คุณสามารถกำหนดการเรียกกลับ onDownloadProgress และ onUploadProgress ของคุณเองได้ การเรียกกลับทั้งสองนี้ยอมรับ AxiosProgressEvent เป็นพารามิเตอร์เหตุการณ์ เมื่อคำขอดำเนินต่อไป การเรียกกลับเหล่านี้จะถูกเรียกใช้หลายครั้ง ช่วยให้คุณสามารถแสดงแถบความคืบหน้าและปรับความยาวตามความคืบหน้าได้


 (base) root@DESKTOP-C0Q8KK7:~/rembg.js-test# node index.mjs
 {
   loaded: 65687,
   total: 68474,
   progress: 0.9592984198381868, <---- @95% progress 
   bytes: 65687,
   rate: undefined,
   estimated: undefined,
   upload: true  <---- upload progress
 }
 {
   loaded: 68474,
   total: 68474,
   progress: 1, <---- @100% progress 
   bytes: 2787,
   rate: undefined,
   estimated: undefined,
   upload: true <---- upload progress 
 }
 {
   loaded: 1002,
   total: 68824,
   progress: 0.014558874811112402, <---- @1% progress 
   bytes: 1002,
   rate: undefined,
   estimated: undefined,
   download: true <---- download progress 
 }
 {
   loaded: 68824,
   total: 68824,
   progress: 1, <---- @100% progress 
   bytes: 67822,
   rate: undefined,
   estimated: undefined,
   download: true <---- download progress
 }
 ✅🎉 background removed and saved under path=/tmp/rembg--3339-DBqqeJ2eOs4D-.png
					

การใช้สมาชิกและเครดิต

ส่งคืนป้ายกำกับแผนของคุณ ยอดเครดิตที่รวมและชำระล่วงหน้า และการใช้งาน คุณสามารถค้นหาตามเดือนตามปฏิทิน UTC (ดั้งเดิม) ตามช่วงเวลาที่เรียกเก็บเงินที่จัดแนว Stripe (สำหรับการตรวจสอบผ่านการต่ออายุ) หรือแสดงรายการช่วงเวลาที่เรียกเก็บเงินที่ทราบ ตรวจสอบสิทธิ์ด้วยคีย์ API ของคุณเท่านั้น

สำหรับสคีมา ตัวอย่าง และคอนโซลลองใช้งาน โปรดดูจุดสิ้นสุดเดียวกันในการอ้างอิงแบบเต็ม: เปิดเอกสาร API

ปลายทาง
GET https://www.rembg.com/api/membership-usage
การตรวจสอบสิทธิ์

ส่งคีย์ API ของคุณ: header x-api-key: YOUR_API_KEY_HERE (สร้างและจัดการคีย์ในโปรไฟล์ของคุณบน rembg.com)

พารามิเตอร์การค้นหา
พารามิเตอร์ประเภทคำอธิบาย
yearnumberปีปฏิทิน (1–9999) เดือนจะอ่านผู้ใช้คีย์ Redis:{uid}:app_usage:{year}:{month} หากละเว้น (และไม่ได้ใช้ periodStartUnix) จะใช้ค่าเริ่มต้นเป็นปี UTC ปัจจุบัน
monthnumber (1–12)ปฏิทินเดือนที่ 1–12 (แบบแผน UTC ที่ใช้สำหรับคีย์) หากละเว้น จะใช้ค่าเริ่มต้นเป็นเดือน UTC ปัจจุบัน
periodStartUnixnumberการประทับเวลา Unix เป็นวินาที: เริ่มต้นกรอบเวลาการเรียกเก็บเงิน อ่านผู้ใช้:{uid}:app_usage:cycle:{termStartUnix} และ api_usage:cycle:…. ไม่สามารถรวมกับปีหรือเดือนได้
expandstringธงที่คั่นด้วยเครื่องหมายจุลภาค รวม billing_cycle เพื่อเพิ่มออบเจ็กต์ billingCycle: ระยะเวลา Stripe ปัจจุบันเมื่อมี billing_ period_* มีอยู่ใน Redis ไม่เช่นนั้นจะเป็นเดือนตามปฏิทิน UTC ปัจจุบัน ใช้งานได้กับ periodStartUnix สำหรับหน้าต่างเฉพาะด้วย
includeBillingCycle1 / trueเช่นเดียวกับการขยายที่มี billing_cycle: ตั้งค่าเป็น 1 หรือ true เพื่อรวมออบเจ็กต์ billingCycle
listBillingCycles1 / trueโหมดเฉพาะ: listBillingCycles=1 หรือ true ส่งคืนเท่านั้น { billingCycles: [...] } สแกน Redis เพื่อหาปุ่มรอบสำหรับผู้ใช้รายนี้ พารามิเตอร์การสืบค้นอื่น ๆ จะถูกละเว้นในคำขอนี้

อย่าผ่าน periodStartUnix ร่วมกับปีหรือเดือน — API จะส่งคืน 400 โหมด listBillingCycles จะแยกจากกันและละเว้นพารามิเตอร์อื่นๆ

แสดงรายการช่วงเวลาที่เรียกเก็บเงิน

ใช้สิ่งนี้เพื่อเติมหน้าต่างการสมัครรับข้อมูลในอดีตและปัจจุบันแบบเลื่อนลง (แต่ละรายการคือ periodStartUnix คุณสามารถส่งกลับด้วย periodStartUnix=…) เวลาสิ้นสุดงวดจะถูกอนุมาน (งวดถัดไปเริ่มต้น − 1 หรือลาย current_ period_end สำหรับงวดที่ใช้งานอยู่)

curl --location 'https://www.rembg.com/api/membership-usage?listBillingCycles=1' \
  --header 'x-api-key: YOUR_API_KEY_HERE'

ตัวอย่างคำตอบ

{
  "billingCycles": [
    {
      "periodStartUnix": 1774268612,
      "periodEndUnix": 1776947012,
      "appUsage": 120,
      "apiUsage": 880
    }
  ]
}

จัดเรียงช่วงเวลาใหม่ล่าสุดก่อน จุดจะปรากฏขึ้นหากมีคีย์การใช้งานวงจรหรือหากเป็น billing_ period_start ปัจจุบันจาก Stripe webhooks

ออบเจ็กต์รอบการเรียกเก็บเงิน

แสดงเมื่อขยายรวม billing_cycle หรือ includeBillingCycle ถูกตั้งค่า ใช้สำหรับแดชบอร์ด: ขอบเขตการต่ออายุ การใช้งานในหน้าต่าง และเครดิตรวมที่เหลืออยู่

ฟิลด์คำอธิบาย
periodStartUnixเริ่มต้นกรอบเวลาการเรียกเก็บเงิน (วินาทียูนิกซ์) จับคู่ Stripe current_ period_start เมื่อบัญชีถูกซิงค์
periodEndUnixสิ้นสุดหน้าต่างปัจจุบัน (วินาทียูนิกซ์) เช่น ก่อนการต่ออายุครั้งถัดไป — แถบปัจจุบัน_ช่วงเวลา_end เมื่อซิงค์ ไม่เหมือนกับวันที่ยกเลิกการสมัครสมาชิก
appUsage / apiUsageรวมการใช้เครดิตที่นับในหน้าต่างนี้: เว็บ/ตัวแก้ไข (แอป) กับคีย์ API (api) usedIncludedCredits เท่ากับผลรวมของพวกเขา
includedCreditsค่าเผื่อเครดิตที่รวมไว้ (แผน) ที่จัดเก็บไว้ใน Redis (แนวคิดเดียวกับเครดิตระดับบนสุด)
prepaidCreditsยอดชำระล่วงหน้า (ซื้อแล้ว) ไม่รีเซ็ตแต่ละรอบบิล
usedIncludedCreditsการใช้งานรวมทั้งหมดในหน้าต่าง (แอป + API) การบริโภคแบบชำระล่วงหน้าไม่ได้ถูกเพิ่มที่นี่
remainingIncludedCreditsสูงสุด (0, รวมเครดิต − usedIncludedCredits) สำหรับภาพรวมนี้
stripeBillingSyncedเป็นจริงเมื่อ Redis มี billing_ period_start/end จาก Stripe ดังนั้นหน้าต่างจึงตรงกับการสมัครของคุณ เท็จหมายถึง API ถอยกลับไปเป็นเดือนตามปฏิทิน UTC สำหรับบล็อกนี้

จนกว่า Stripe webhooks จะเขียนช่วงเวลาการเรียกเก็บเงินสำหรับผู้ใช้ stripeBillingSynced อาจเป็นเท็จ และกรอบเวลาการเรียกเก็บเงินจะเป็นไปตามเดือนตามปฏิทิน UTC หลังจากการซิงค์ ขีดจำกัดการใช้งานบน API รูปภาพจะสอดคล้องกับคีย์วงจรเดียวกัน

สำหรับ “เครดิตที่ใช้ก่อนการต่ออายุครั้งถัดไป” ให้เรียกด้วยexpand=billing_cycle และใช้ billingCycle. periodEndUnix เป็นขอบเขตการต่ออายุ billingCycle.usedIncludedCredits (หรือ appUsage + apiUsage) และ billingCycle.remainingIncludedCredits เพิ่มเครดิตที่ชำระล่วงหน้าหากคุณต้องการยอดคงเหลือที่ใช้แล้วทิ้งทั้งหมด

ตัวอย่าง (cURL)

การใช้งานสำหรับเดือนปฏิทิน UTC ที่ระบุ

curl --location 'https://www.rembg.com/api/membership-usage?year=2026&month=3' \
  --header 'x-api-key: YOUR_API_KEY_HERE'

รอบการเรียกเก็บเงินปัจจุบัน + บล็อกรอบการเรียกเก็บเงินเต็ม (ต่ออายุเมื่อซิงค์)

# Current subscription window + usage (renewal-aligned when Stripe is synced)
curl --location 'https://www.rembg.com/api/membership-usage?expand=billing_cycle' \
  --header 'x-api-key: YOUR_API_KEY_HERE'

หนึ่งกรอบเวลาการเรียกเก็บเงินที่ผ่านมาตามช่วงเวลาที่เริ่มต้น (จากรายการรอบการเรียกเก็บเงินหรือแถบ)

# Specific billing period by Stripe period start (unix seconds)
curl --location 'https://www.rembg.com/api/membership-usage?periodStartUnix=1774268612&expand=billing_cycle' \
  --header 'x-api-key: YOUR_API_KEY_HERE'
ตัวอย่าง JSON (พร้อมรอบการเรียกเก็บเงิน)

app_usage ระดับบนสุดและ api_usage จะแสดงเดือนตามปฏิทินที่ร้องขอเสมอ หรือเมื่อตั้งค่า periodStartUnix ตัวนับของรอบนั้น เมื่อละเว้น expand=billing_cycle แสดงว่า BillingCycle หายไป

{
  "membership": "Premium-20000",
  "credits": 18500,
  "prepaidCredits": 2000,
  "app_usage": 8500,
  "api_usage": 10000,
  "billingCycle": {
    "periodStartUnix": 1740441600,
    "periodEndUnix": 1743033599,
    "appUsage": 4200,
    "apiUsage": 5100,
    "includedCredits": 18500,
    "prepaidCredits": 2000,
    "usedIncludedCredits": 9300,
    "remainingIncludedCredits": 9200,
    "stripeBillingSynced": true
  }
}

เมื่อ stripeBillingSynced เป็นจริง BillingCycle จะจับคู่การบังคับใช้ใน API การลบพื้นหลัง เมื่อเป็นเท็จ ให้ใช้ช่องเดือนตามปฏิทินหรือรอจนกว่า webhooks จะเติม billing_ period_* ใน Redis

Error Responses

All error responses return a JSON body with a status field matching the HTTP status code.

Single Error Response

HTTP/1.1 400 Bad Request

HTTP/1.1 400 Bad Request
Content-Type: application/json

{
  "error": "Image width (12000px) exceeds the maximum allowed width of 10000px.",
  "field": "image",
  "status": 400
}

HTTP/1.1 400 Bad Request

HTTP/1.1 400 Bad Request
Content-Type: application/json

{
  "error": "Multiple validation errors",
  "details": [
    { "field": "w", "message": "'w' must be an integer, got: 'abc'" },
    { "field": "angle", "message": "'angle' must be between -360 and 360 degrees, got: 500.0" }
  ],
  "status": 400
}
Error Reference
ScenarioStatusError Message
No image provided
400
No image file provided. Please include an 'image' field in your form data.
No file selected
400
No file selected. Please choose an image file to upload.
Unsupported file type
400
File type '.exe' is not supported. Allowed formats: webp, bmp, png, jpg, jpeg
Corrupt image
400
File does not appear to be a valid image (invalid file signature)
Empty file (0 bytes)
400
Uploaded file is empty (0 bytes).
File too large
400
File size (55.2 MB) exceeds the maximum allowed size of 50 MB.
Image too wide
400
Image width (12000px) exceeds the maximum allowed width of 10000px.
Image too tall
400
Image height (15000px) exceeds the maximum allowed height of 10000px.
Bad w or h value
400
'w' must be an integer, got: 'abc'
w or h out of range
400
'w' must be at least 1, got: -5
w or h above max
400
'w' must be at most 10000, got: 15000
Bad angle
400
'angle' must be a number, got: 'ninety'
Angle out of range
400
'angle' must be between -360 and 360 degrees, got: 500.0
Bad output format
400
Invalid format 'gif'. Allowed formats: PNG, JPEG, WEBP, BMP
Bad bg_color
400
Invalid color format '#12345'. Use hex format: #RGB, #RGBA, #RRGGBB, or #RRGGBBAA
mask + bg_color conflict
400
Cannot use 'mask=true' together with 'bg_color'. Choose one or the other.
Email not verified
403
Email address not verified – please check your inbox
Invalid API key
401
invalid api key
Both API key and user token sent
400
This is a mistake: Cannot use both API key and user token
Rate limit exceeded (short-term)
429
You're making requests too quickly, Please Upgrade or slow down.
Rate limit exceeded (daily, anonymous)
429
You've reached your daily limit. Please sign up for more access.
Rate limit exceeded (monthly, authenticated)
429
You've reached your monthly limit. Consider purchasing more credits.
Error Handling Guidelines
  • Check HTTP status code 400 to identify validation errors
  • Parse the error field for the error message
  • Check for the details array when handling multiple validation errors
  • Use the field property to map errors to specific request parameters