Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including remove.bg with all npm packages installed. Try it out:

var removeBg = require("remove.bg")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

remove.bg v1.1.0

A simple wrapper for the remove.bg API

remove.bg API wrapper for Node.js

NPM version Downloads Twitter Follow

The AWESOME remove.bg API is quite easy to use, but it can always be easier - that's where this package comes in.

Requirements

Get your API key from the remove.bg website. At the moment it's early access, so it may take some time to get yours.

Installation

npm i remove.bg

Examples

Look at the various removeFrom*.ts files in the examples folder, or check out the snippets below.

Curious how to use async/await? That's another reason to check out those examples.

API

The common input parameters of all three currently supported removeBackgroundFrom* functions are:

PropertyMandatoryTypeDescription
apiKeyYstringThe API key you got from the remove.bg website.
sizeN"regular" / "medium" / "hd" / "4k"The returned size of the image. The cheaper "regular" option is default.
typeN"auto" / "person" / "product"Help the API a little by telling the type of image you want to extract the background from. Default "auto".
outputFileNstringThe path to save the returned file to.

And the output properties are:

PropertyTypeDescription
base64imgstringBase64 encoded representation of the returned image.
creditsChargednumberAmount of credits charged for this call, based on the output size of the response.
detectedTypestringEither a person or a product.
resultWidthnumberThe width of the result image, in pixels.
resultHeightnumberThe height of the result image, in pixels.

removeBackgroundFromImageFile

Remove the background from a local file.

import { RemoveBgResult, RemoveBgError, removeBackgroundFromImageFile } from "remove.bg";

const localFile = "./local/file/name.jpg";
const outputFile = `${__dirname}/out/img-removed-from-file.png`;

removeBackgroundFromImageFile({
  path: localFile,
  apiKey: "YOUR-API-KEY",
  size: "regular",
  type: "auto",
  outputFile
}).then((result: RemoveBgResult) => {
 console.log(`File saved to ${outputFile}`);
  const base64img = result.base64img;
}).catch((errors: Array<RemoveBgError>) => {
 console.log(JSON.stringify(errors));
});

removeBackgroundFromImageUrl

Remove the background from a remote file (URL).

import { RemoveBgResult, RemoveBgError, removeBackgroundFromImageUrl } from "remove.bg";

const url = "https://domain.tld/path/file.jpg";
const outputFile = `${__dirname}/out/img-removed-from-file.png`;

removeBackgroundFromImageUrl({
  url,
  apiKey: "YOUR-API-KEY",
  size: "regular",
  type: "person",
  outputFile
}).then((result: RemoveBgResult) => {
 console.log(`File saved to ${outputFile}`);
  const base64img = result.base64img;
}).catch((errors: Array<RemoveBgError>) => {
 console.log(JSON.stringify(errors));
});

removeBackgroundFromImageBase64

Remove the background from a base64 encoded file.

import { RemoveBgResult, RemoveBgError, removeBackgroundFromImageBase64 } from "remove.bg";
import * as fs from "fs";

const localFile = "./local/file/name.jpg";
const base64img = fs.readFileSync(localFile, { encoding: "base64" });
const outputFile = `${__dirname}/out/img-removed-from-file.png`;

removeBackgroundFromImageBase64({
  base64img,
  apiKey: "YOUR-API-KEY",
  size: "regular",
  type: "product",
  outputFile
}).then((result: RemoveBgResult) => {
 console.log(`File saved to ${outputFile}`);
  const base64img = result.base64img;
}).catch((errors: Array<RemoveBgError>) => {
 console.log(JSON.stringify(errors));
});
RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free