RemBG Background Removal API – Developer Docs & Integration Guide






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-background-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 خود را تعریف کنید. هر دوی این callback ها 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 (میراث)، بر اساس دوره صورت‌حساب همتراز با نوار (برای نظارت از طریق تمدید)، یا فهرست دوره‌های صورت‌حساب شناخته‌شده را جستجو کنید. فقط با کلید 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 keys را می خواند:{uid}:app_usage:{year}:{month}. اگر حذف شود (و periodStartUnix استفاده نشود)، به طور پیش‌فرض به سال جاری UTC تبدیل می‌شود.
monthnumber (1–12)ماه تقویم 1-12 (کنوانسیون UTC برای کلیدها استفاده می شود). اگر حذف شود، به طور پیش‌فرض روی ماه جاری UTC تنظیم می‌شود.
periodStartUnixnumberمهر زمانی یونیکس در چند ثانیه: شروع یک پنجره صورتحساب. کاربر را می خواند:{uid}:app_usage:cycle:{periodStartUnix} و api_usage:cycle:…. نمی توان با سال یا ماه ترکیب کرد.
expandstringپرچم های جدا شده با کاما. برای افزودن شیء billingCycle، billing_cycle را اضافه کنید: دوره 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، یا Stripe 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 باشد، یک نقطه ظاهر می‌شود.

شیء چرخه صورتحساب

هنگامی که گسترش شامل billing_cycle یا includeBillingCycle تنظیم شده است، ارائه شود. از آن برای داشبوردها استفاده کنید: مرز تمدید، استفاده در پنجره، و اعتبارات اضافه شده باقیمانده.

فیلدتوضیحات
periodStartUnixشروع پنجره صورتحساب (یونیکس ثانیه). وقتی حساب همگام‌سازی می‌شود، Stripe current_period_start را مطابقت می‌دهد.
periodEndUnixپایان پنجره فعلی (یونیکس ثانیه)، یعنی درست قبل از تمدید بعدی - وقتی همگام‌سازی می‌شود، current_period_end را خط بکشید. نه با تاریخ لغو اشتراک.
appUsage / apiUsageاستفاده از اعتبار گنجانده شده در این پنجره محاسبه می شود: وب/ویرایشگر (برنامه) در مقابل کلید API (api). usedIncludedCredits برابر است با مجموع آنها.
includedCreditsکمک هزینه اعتباری شامل (طرح) شما همانطور که در Redis ذخیره شده است (همان ایده اعتبارات سطح بالا).
prepaidCreditsموجودی پیش پرداخت (خریداری شده)؛ هر دوره صورتحساب را بازنشانی نمی کند.
usedIncludedCreditsکل استفاده گنجانده شده در پنجره (برنامه + API). مصرف پیش پرداخت در اینجا اضافه نمی شود.
remainingIncludedCreditsحداکثر (0، شامل اعتبار - usedIncludedCredits) برای این عکس فوری.
stripeBillingSyncedدرست است زمانی که Redis دارای billing_period_start/end از Stripe باشد تا پنجره با اشتراک شما مطابقت داشته باشد. false به این معنی است که API برای این بلوک به یک ماه تقویمی UTC بازگشت.

تا زمانی که وب‌قلاب‌های Stripe دوره‌های صورت‌حساب را برای کاربر نوشته باشند، stripeBillingSynced ممکن است نادرست باشد و پنجره billingCycle از ماه تقویم UTC پیروی می‌کند. پس از همگام‌سازی، محدودیت‌های استفاده در API تصویر با کلیدهای چرخه مشابه تراز می‌شوند.

برای «اعتبارهای استفاده شده قبل از تمدید بعدی»، با expand=billing_cycle تماس بگیرید و از billingCycle.periodEndUnix به عنوان مرز تمدید، billingCycle.usedIncludedCredits (یا appUsage + apiUsage) و billingCycle.remainingIncludedCredit استفاده کنید. اگر کل موجودی یکبار مصرف را می خواهید، اعتبارات پیش پرداخت اضافه کنید.

مثالها (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'

یک پنجره صورت‌حساب تاریخی بر اساس شروع دوره (از listBillingCycles یا Stripe)

# 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 حذف پس‌زمینه مطابقت دارد. در صورت نادرست بودن، به فیلدهای ماه تقویم تکیه کنید یا منتظر بمانید تا webhook ها 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