Logo
RemovebgAPI
PlaygroundPricingAPI DocsBlog
Tutorial

How to Use a Background Removal API with Node.js | RemovebgAPI

A step-by-step guide for developers on SaaS background removal API integration. Learn to automatically remove image backgrounds in your Node.js application.

January 18, 20257 min read

Automating image processing is a core requirement for modern applications, especially in e-commerce and content management. This guide will walk you through a complete SaaS background removal API integration using Node.js. It's the perfect starting point for any developer looking to use a background removal API.

Prerequisites

Before we start, make sure you have the following:

  • Node.js and npm installed on your machine.
  • An API key from RemovebgAPI. You can get a free API key for remove bg from your dashboard.
  • An image file you want to process.

Step 1: Set Up Your Node.js Project

Create a new directory for your project and initialize it. We'll need node-fetch for making requests and form-data to handle the file upload. Note that we are using version 2 of node-fetch for CommonJS compatibility.

mkdir node-removebg-demo
cd node-removebg-demo
npm init -y
npm install node-fetch@2 form-data

Step 2: Write the API Integration Code

Create a file named index.js and add the following code. This script defines a function that takes an image path, sends it to our real-time remove background API, and saves the resulting image.

const fetch = require('node-fetch');
const FormData = require('form-data');
const fs = require('fs');

async function removeBackground(imagePath) {
// Get your free API key from https://removebgapi.com/dashboard/api-keys
const apiKey = 'YOUR_API_KEY';
const url = 'https://removebgapi.com/api/v1/remove';

const formData = new FormData();
formData.append('image', fs.createReadStream(imagePath));

try {
const response = await fetch(url, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiKey}`,
...formData.getHeaders()
},
body: formData,
});

if (!response.ok) {
const errorData = await response.json();
throw new Error(`API Error: ${response.status} - ${errorData.error || 'Unknown error'}`);
}

const imageBuffer = await response.buffer();
fs.writeFileSync('result.png', imageBuffer);
console.log('Background removed successfully! Saved as result.png');
} catch (error) {
console.error('Failed to remove background:', error.message);
}
}

// Usage:
removeBackground('./path/to/your/image.jpg');

Step 3: Run the Script

Replace 'YOUR_API_KEY' with your actual remove background api key and update the path to your image. Then, run the script from your terminal:

node index.js

In seconds, you'll see a new file named result.png in your directory with the background successfully removed!

Conclusion

You've successfully performed an automatic background removal from an image using an API. This simple script is the foundation for building a powerful, scalable background removal API solutioninto any application, from an e-commerce backend to a user-facing content creation tool.

Read Next

Review

Affordable and Accurate Free Background Removal APIs with Easy API Key Access in 2025

Explore the top free and affordable background removal APIs for 2025, focusing on easy API key access, cost, and accuracy, with a spotlight on RemovebgAPI.

Guide

How to Choose the Best Background Removal API for SaaS & E-commerce | RemovebgAPI

A guide for SaaS and e-commerce businesses on choosing the best background removal API, focusing on accuracy, scalability, and cost-efficiency.

Logo
RemovebgAPI

The affordable, high-accuracy background removal API for developers and businesses.

Product

  • Playground
  • Pricing
  • API Docs
  • Features

Company

  • About Us
  • Blog
  • Careers
  • Changelog

Legal

  • Privacy Policy
  • Terms of Service

Connect

SupportTwitterFacebookYoutubeSlack

© 2025 RemovebgAPI, Inc. All rights reserved.