How to Remove Image Backgrounds in Node.js

A complete guide to integrating FAPIhub's background removal API into your Node.js project. Takes less than 10 minutes.

This guide shows how to call the FapiHub background removal API from Node.js using the built-in fetch (Node 18+) and the form-data package for multipart file uploads. No SDK required — just standard HTTP.

Prerequisites

  • A FAPIhub API key (get one free at fapihub.com/dashboard)
  • Node.js 18 or higher installed
  • Basic understanding of HTTP requests

Step-by-Step Integration

1

Install dependencies

Install any libraries needed for HTTP requests.

# Node.js 18+ has built-in fetch — no install needed for basic use.
# For streaming and FormData with file streams, install form-data:
npm install form-data

# If using Node.js 16 or lower, also run:
npm install node-fetch
2

Get your API key

Create a free account and copy your API key from the dashboard.

3

Basic background removal

Upload an image and receive transparent PNG output.

const formData = new FormData();
formData.append("image", imageFile);

const response = await fetch("https://fapihub.com/v2/rembg/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY" },
  body: formData
});
4

Save and verify output

Write API response bytes to a PNG file and verify output.

const response = await fetch("https://fapihub.com/v2/rembg/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY" },
  body: formData,
});

const buffer = await response.arrayBuffer();
fs.writeFileSync("result.png", Buffer.from(buffer));
console.log("Saved result.png");
5

Handle errors

Check HTTP status codes and catch network errors.

try {
  const response = await fetch("https://fapihub.com/v2/rembg/", {
    method: "POST",
    headers: { token: "YOUR_API_KEY" },
    body: formData,
    signal: AbortSignal.timeout(30000),
  });

  if (!response.ok) {
    if (response.status === 401) throw new Error("Invalid API key");
    if (response.status === 429) throw new Error("Rate limit reached");
    throw new Error(`API error: ${response.status}`);
  }

  const buffer = await response.arrayBuffer();
  fs.writeFileSync("result.png", Buffer.from(buffer));
} catch (err) {
  if (err.name === "TimeoutError") {
    console.error("Request timed out after 30 seconds");
  } else {
    console.error("Error:", err.message);
  }
}
6

Batch processing

Loop through image paths for high-volume jobs.

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

const API_KEY = "YOUR_API_KEY";
const INPUT_DIR = "./images";
const OUTPUT_DIR = "./results";

if (!fs.existsSync(OUTPUT_DIR)) fs.mkdirSync(OUTPUT_DIR);

const files = fs.readdirSync(INPUT_DIR)
  .filter(f => /\.(jpg|jpeg|png|webp)$/i.test(f));

for (const file of files) {
  const form = new FormData();
  form.append("image", fs.createReadStream(path.join(INPUT_DIR, file)));

  const response = await fetch("https://fapihub.com/v2/rembg/", {
    method: "POST",
    headers: { token: API_KEY, ...form.getHeaders() },
    body: form,
  });

  if (response.ok) {
    const buffer = await response.buffer();
    const outName = path.basename(file, path.extname(file)) + ".png";
    fs.writeFileSync(path.join(OUTPUT_DIR, outName), buffer);
    console.log(`Done: ${file}`);
  } else {
    console.error(`Error on ${file}: ${response.status}`);
  }
}

Advanced Usage

Blur endpoint

Apply blur to the original background.

const form = new FormData();
form.append("image", fs.createReadStream("photo.jpg"));
form.append("blur_radius", "15");

const response = await fetch("https://fapihub.com/v2/rembg/blur/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY", ...form.getHeaders() },
  body: form,
});
fs.writeFileSync("blur_bg.png", Buffer.from(await response.arrayBuffer()));

Color endpoint

Set solid RGB background output.

const form = new FormData();
form.append("image", fs.createReadStream("photo.jpg"));
form.append("background_color", "255,255,255");

const response = await fetch("https://fapihub.com/v2/rembg/color/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY", ...form.getHeaders() },
  body: form,
});
fs.writeFileSync("white_bg.png", Buffer.from(await response.arrayBuffer()));

Gradient endpoint

Set top and bottom gradient colors.

const form = new FormData();
form.append("image", fs.createReadStream("photo.jpg"));
form.append("top_color", "0,0,200");
form.append("bottom_color", "255,255,255");

const response = await fetch("https://fapihub.com/v2/rembg/gradient/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY", ...form.getHeaders() },
  body: form,
});
fs.writeFileSync("gradient_bg.png", Buffer.from(await response.arrayBuffer()));

Shadow endpoint

Generate shadow-enhanced product images.

const form = new FormData();
form.append("image", fs.createReadStream("photo.jpg"));
form.append("offset", "10");
form.append("blur", "15");

const response = await fetch("https://fapihub.com/v2/rembg/shadow/", {
  method: "POST",
  headers: { token: "YOUR_API_KEY", ...form.getHeaders() },
  body: form,
});
fs.writeFileSync("shadow.png", Buffer.from(await response.arrayBuffer()));

Ready to Start?

Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.

Get Your Free API Key