واجهة برمجة تطبيقات إزالة الخلفية RemBG - مستندات المطور ودليل التكامل






OpenAPI · الملعب المباشر

استكشف واجهة برمجة التطبيقات الكاملة - قم بتشغيل الطلبات الحقيقية في متصفحك

تصفح كل نقطة نهاية ومعلمات واستجابات. جرّب الطلبات باستخدام مفتاح 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 الخاصة بك. يقبل كل من عمليات الاسترجاعات هذه 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 الخاص بك فقط.

بالنسبة للمخططات والأمثلة ووحدة التحكم التجريبية، راجع نفس نقطة النهاية في المرجع الكامل: فتح مستندات واجهة برمجة التطبيقات

نقطة النهاية
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:{periodStartUnix} وapi_usage:cycle:.... لا يمكن دمجها مع السنة أو الشهر.
expandstringعلامات مفصولة بفواصل. قم بتضمين billing_cycle لإضافة كائن billingCycle: فترة الشريط الحالية عند وجود billing_period_* في Redis، وإلا فإن الشهر التقويمي UTC الحالي. يعمل أيضًا معperiodStartUnix لنافذة معينة.
includeBillingCycle1 / trueمثل التوسيع المحتوي على billing_cycle: اضبط على 1 أو صحيح لتضمين كائن billingCycle.
listBillingCycles1 / trueالوضع المخصص: listBillingCycles=1 أو الإرجاعات الحقيقية فقط { billingCycles: [...] }. يقوم بمسح Redis بحثًا عن مفاتيح الدورة لهذا المستخدم؛ يتم تجاهل معلمات الاستعلام الأخرى في هذا الطلب.

لا تقم بتمريرperiodStartUnix مع السنة أو الشهر — تقوم واجهة برمجة التطبيقات (API) بإرجاع 400. وضع listBillingCycles منفصل ويتجاهل المعلمات الأخرى.

قائمة فترات الفوترة

استخدم هذا لملء القائمة المنسدلة لنوافذ الاشتراك السابقة والحالية (كل إدخال هوperiodStartUnix يمكنك تمريره مرة أخرى باستخدامperiodStartUnix=...). يتم استنتاج أوقات نهاية الفترة (بداية الفترة التالية - 1، أو الشريط الحالي_فترة_نهاية للفترة النشطة).

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
    }
  ]
}

تم فرز الفترة الأحدث أولاً. تظهر الفترة إذا كان هناك مفتاح استخدام للدورة أو إذا كانت فترة_الفوترة_الحالية_تبدأ من خطافات الويب الخاصة بـ Stripe.

كائن دورة الفوترة

يتم تقديمه عند تعيين التوسيع الذي يتضمن Billing_cycle أو includeBillingCycle. استخدمه للوحات المعلومات: حدود التجديد، والاستخدام في النافذة، والاعتمادات المضمنة المتبقية.

الحقلالوصف
periodStartUnixبداية نافذة الفوترة (ثواني يونكس). يطابق الشريط current_period_start عند مزامنة الحساب.
periodEndUnixنهاية النافذة الحالية (ثواني Unix)، أي قبل التجديد التالي مباشرة — شريط current_period_end عند المزامنة. ليس نفس تاريخ إلغاء الاشتراك.
appUsage / apiUsageيتم احتساب استخدام الرصيد المضمن في هذه النافذة: الويب/المحرر (التطبيق) مقابل مفتاح واجهة برمجة التطبيقات (api). useIncludedCredits يساوي مجموعهم.
includedCreditsبدل الائتمان (الخطة) المضمن الخاص بك كما هو مخزن في Redis (نفس فكرة أرصدة المستوى الأعلى).
prepaidCreditsالرصيد المدفوع مسبقًا (المشترى)؛ لا يتم إعادة ضبط كل فترة زمنية للفوترة.
usedIncludedCreditsإجمالي الاستخدام المضمن في النافذة (التطبيق + واجهة برمجة التطبيقات). لا يتم إضافة الاستهلاك المدفوع مقدمًا هنا.
remainingIncludedCreditsكحد أقصى (0، أرصدة مضمنة - أرصدة مضمنة) لهذه اللقطة.
stripeBillingSyncedصحيح عندما يكون لدى Redis Billing_period_start/end من Stripe بحيث تتطابق النافذة مع اشتراكك؛ خطأ يعني أن واجهة برمجة التطبيقات (API) عادت إلى شهر تقويم UTC لهذه الكتلة.

حتى تكتب خطافات الويب Stripe فترات الفوترة للمستخدم، قد يكون stripeBillingSynced خاطئًا وتتبع نافذة BillingCycle الشهر التقويمي UTC. بعد المزامنة، تتم محاذاة حدود الاستخدام في واجهة برمجة تطبيقات الصورة مع نفس مفاتيح الدورة.

بالنسبة إلى "الأرصدة المستخدمة قبل التجديد التالي"، اتصل بـexpand=billing_cycle واستخدم billingCycle.periodEndUnix كحدود للتجديد، وbilingCycle.usedIncludedCredits (أو appUsage + apiUsage)، وbilingCycle.remainingIncludedCredits. أضف الاعتمادات المدفوعة مسبقًا إذا كنت تريد إجمالي الرصيد المتاح.

أمثلة (cURL)

الاستخدام لشهر تقويمي محدد بالتوقيت العالمي

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'

نافذة فوترة تاريخية واحدة حسب بداية الفترة (من قائمة BillingCycles أو 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، عدادات تلك الدورة. عندما يتم حذفتوسيع=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 يطابق التنفيذ على واجهة برمجة تطبيقات إزالة الخلفية. عندما تكون خاطئة، اعتمد على حقول الشهر التقويمي أو انتظر حتى تقوم خطافات الويب بملء 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