Code examples

Call nobg from your backend with tools you already use.

After deploying the Worker, set NOBG_URL to its base URL with no trailing slash in your backend environment. No API key is needed.

export NOBG_URL="https://nobg-api.YOUR-SUBDOMAIN.workers.dev"

curl

curl --fail-with-body \
  "$NOBG_URL/api/v1/remove-background" \
  -F "image=@photo.jpg" \
  -F "format=webp" \
  --output nobg.webp

JavaScript

Node.js 22 or newer:

import { readFile, writeFile } from 'node:fs/promises';

const form = new FormData();
form.set('image', new Blob([await readFile('photo.jpg')], {
  type: 'image/jpeg',
}), 'photo.jpg');
form.set('format', 'png');

const response = await fetch(
  `${process.env.NOBG_URL}/api/v1/remove-background`,
  {
    method: 'POST',
    body: form,
  },
);

if (!response.ok) {
  const { error } = await response.json();
  throw new Error(`${error.code}: ${error.message} (${error.requestId})`);
}

await writeFile('nobg.png', Buffer.from(await response.arrayBuffer()));

Python

Install requests, then run this from your server environment:

import os
import requests

with open("photo.jpg", "rb") as image:
    response = requests.post(
        f"{os.environ['NOBG_URL']}/api/v1/remove-background",
        files={"image": ("photo.jpg", image, "image/jpeg")},
        data={"format": "png"},
        timeout=120,
    )

if not response.ok:
    raise RuntimeError(response.json()["error"])

with open("nobg.png", "wb") as result:
    result.write(response.content)

Browser applications

The hosted demo calls the API directly from https://nobg.akshit.io. For a self-hosted browser app, add its exact origin to the API Worker’s ALLOWED_ORIGINS variable. Separate multiple origins with commas. The API exposes Retry-After and X-Request-Id to allowed origins; requests do not need cookies or credentials.

Direct browser uploads retain each visitor’s IP allowance. Requests forwarded by one backend share that backend’s outbound IP allowance. CORS controls browser response access, not authentication. On 429, wait for the Retry-After interval before trying again.