Back to Library
RemoveBG
js • 1/27/2026
10
const axios = require("axios");
const FormData = require("form-data");
async function fetchBuffer(url) {
try {
const response = await axios.get(url, { responseType: 'arraybuffer' });
return Buffer.from(response.data);
} catch (error) {
throw new Error("Failed to download image");
}
}
async function removeBg(url) {
try {
const buffer = await fetchBuffer(url);
const form = new FormData();
form.append("image", buffer, { filename: "image.png" });
form.append("format", "png");
form.append("model", "v1");
const headers = {
...form.getHeaders(),
"accept": "application/json, text/plain, */*",
"x-client-version": "web",
"x-locale": "en",
};
const res = await axios.post("https://api2.pixelcut.app/image/matte/v1", form, {
headers,
responseType: "arraybuffer",
});
return Buffer.from(res.data);
} catch (error) {
throw new Error(error.message);
}
}
module.exports = { removeBg };