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 18+ using the built-in fetch, FormData, and Blob APIs. No SDK or npm packages 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+ ships with fetch, FormData, and Blob built in.
# No npm packages are required.
node --version  # should print v18 or later
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 fs = require("fs");

const formData = new FormData();
formData.append("image", new Blob([fs.readFileSync("photo.jpg")]), "photo.jpg");

const response = await fetch("https://fapihub.com/v2/rembg/", {
  method: "POST",
  headers: { ApiKey: "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: { ApiKey: "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: { ApiKey: "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 API_KEY = "YOUR_API_KEY";
const INPUT_DIR = "./images";
const OUTPUT_DIR = "./results";

fs.mkdirSync(OUTPUT_DIR, { recursive: true });

async function main() {
  const files = fs.readdirSync(INPUT_DIR)
    .filter((f) => /\.(jpe?g|png|webp)$/i.test(f));

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

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

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

main();

Original vs Result

See what the API returns on real examples. Each pair shows the untouched source image and the cleaned FAPIhub result.

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