www.rembg.com API wrapper for Node.js
A simple, FREE AI background removal tool for Node.js. Currently, this is THE ONLY FREE library available for personal usage from API. Check out our website at www.rembg.com for more details.
To use @remove-background-ai/rembg.js, you must have Node.js v18.17.0 or higher installed.
β οΈ WARNING: Node.js v18 is already out of support. It's strongly recommended to use at least Node.js v20 or higher for better security, performance, and long-term support.
Get your FREE API Key from the https://www.rembg.com/api-usage PS: you can still use the API Keys without key but it is very limited
npm i --save @remove-background-ai/rembg.js| Parameter | Type(s) | Required | Default | Notes |
|---|---|---|---|---|
| apiKey | string |
βοΈ | β | Rembg API key (free tier available). |
| inputImage | string Buffer { base64: string } |
βοΈ | β | Image path, buffer or base-64 wrapper. (npm) |
| onDownloadProgress | (AxiosProgressEvent)=>void |
β | β | Called repeatedly while result is streamed. (npm) |
| onUploadProgress | (AxiosProgressEvent)=>void |
β | β | Called repeatedly while upload is streamed. (npm) |
| options.format | webp (default) png |
β | webp |
Encoding format, by default server returns WEBP image. |
| options.returnBase64 | boolean |
β | false |
Return base-64 instead of temp file. (npm) |
| options.returnMask | boolean |
β | false |
Return alpha-mask image only. (npm) |
| options.w | number |
β | β | Target width (keeps aspect). (npm) |
| options.h | number |
β | β | Target height (keeps aspect). (npm) |
| options.exact_resize | boolean |
β | false |
Force exact w Γ h (can distort). (npm) |
| options.angle | number |
β | β | Rotation angle in degrees. (npm) |
| options.expand | boolean |
β | true |
Add padding so rotated images aren't cropped. (npm) |
| options.bg_color | string |
β | β | Optional solid background color in hex (e.g. #FFFFFFFF) or named color (e.g. "red", "blue"). (npm) |
inputImage can be one of these:
string | Buffer | { base64: string };
// 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',
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();
});if you wish to return a Base64 instead of temporary URL you can use retrunBase64 parameter:
import { rembg } from '../dist/index.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',
onDownloadProgress,
onUploadProgress,
options: {
returnBase64: true
format: 'png' // or webp
}
}).then(({ base64Image }) => {
console.log(`β
π background removed ${base64Image}`);
});Since version 1.1.8, the rembg function can accept an input image as a Buffer or Base64 object. Below is a quick demonstration:
import { rembg } from '../dist/index.js';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
const base64Input = 'data:image/png;base64,/9j/4AAQSkZJRgABAQEASABIAAD/.....etc'
// 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: {base64: base64Input}, // or simply an imageBuffer
onDownloadProgress,
onUploadProgress,
options: {
returnBase64: true
}
}).then(({ base64Image }) => {
console.log(`β
π background removed ${base64Image}`);
});The library provides an option to return a mask of the image instead of the processed image. This is controlled by the returnMask parameter.
When set to true, the function returns a mask. By default (if omitted), this parameter is set to false.
rembg({
apiKey: API_KEY,
inputImage: './input.jpg',
onDownloadProgress,
onUploadProgress,
options: {
returnMask: true, // <----- Set to true to get the mask of the image
returnBase64: true // Set to true to receive the result as a Base64 string
}
}).then(({ base64Image }) => {
console.log(`β
π Mask retrieved: ${base64Image}`);
});Below is the generated mask image using the rembg function with the mask option enabled:
This image demonstrates the result of the mask generation process. The mask typically highlights the main subject of the image with the background removed or made transparent.
This is very useful to work with Stable Diffusion for perfect area of inpainting, for example.
rembg({
apiKey: API_KEY,
inputImage: './input.jpg',
onDownloadProgress,
onUploadProgress,
options: {
w: 1000,
h: 760 // When both `w` and `h` are provided, RemBG will find the best dimensions that fit within the requested width and height while preserving the aspect ratio.
// If you want to guarantee the exact size, set `exact_resize: true`.
// exact_resize: true !!! be careful it can cause distortion !!!
}
}).then(({ base64Image }) => {
console.log(`β
π Mask retrieved: ${base64Image}`);
});The library now supports advanced image processing options including rotation, padding, and background color customization:
rembg({
apiKey: API_KEY,
inputImage: './input.jpg',
onDownloadProgress,
onUploadProgress,
options: {
w: 800,
h: 600,
format: "WEBP",
angle: 90, // Rotate image 90 degrees
expand: true, // Add padding so rotated images aren't cropped
bg_color: "blue" // Set background color (hex: "#FF5733" or named: "blue")
}
}).then(({ outputImagePath, cleanup }) => {
console.log(`β
π Background removed with rotation and custom background: ${outputImagePath}`);
cleanup();
});
