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 laterA 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.
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 laterCreate a free account and copy your API key from the dashboard.
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
});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");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);
}
}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();See what the API returns on real examples. Each pair shows the untouched source image and the cleaned FAPIhub result.
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
});
const form = new FormData();
form.append("image", new Blob([fs.readFileSync("photo.jpg")]), "photo.jpg");
form.append("radius", "15");
const response = await fetch("https://fapihub.com/v2/rembg/blur/", {
method: "POST",
headers: { ApiKey: "YOUR_API_KEY" },
body: form,
});
fs.writeFileSync("blur_bg.png", Buffer.from(await response.arrayBuffer()));
const form = new FormData();
form.append("image", new Blob([fs.readFileSync("photo.jpg")]), "photo.jpg");
form.append("bgcolor", "(255, 255, 255, 255)");
const response = await fetch("https://fapihub.com/v2/rembg/color/", {
method: "POST",
headers: { ApiKey: "YOUR_API_KEY" },
body: form,
});
fs.writeFileSync("white_bg.png", Buffer.from(await response.arrayBuffer()));
const form = new FormData();
form.append("image", new Blob([fs.readFileSync("photo.jpg")]), "photo.jpg");
form.append("top_color", "(0, 0, 200, 255)");
form.append("bottom_color", "(255, 255, 255, 255)");
const response = await fetch("https://fapihub.com/v2/rembg/gradient/", {
method: "POST",
headers: { ApiKey: "YOUR_API_KEY" },
body: form,
});
fs.writeFileSync("gradient_bg.png", Buffer.from(await response.arrayBuffer()));
const form = new FormData();
form.append("image", new Blob([fs.readFileSync("photo.jpg")]), "photo.jpg");
form.append("shadow_color", "(0, 0, 0, 150)");
form.append("offset", "(10, 10)");
form.append("blur_radius", "15");
const response = await fetch("https://fapihub.com/v2/rembg/shadow/", {
method: "POST",
headers: { ApiKey: "YOUR_API_KEY" },
body: form,
});
fs.writeFileSync("shadow.png", Buffer.from(await response.arrayBuffer()));Free tier includes 100 full-resolution requests/month. No credit card. Your API key is ready in 30 seconds.
Get Your Free API Key