Back to Blog

How to Remove Backgrounds with BiRefNet API

Andrew Adams

Andrew Adams

·8 min read
How to Remove Backgrounds with BiRefNet API

Removing backgrounds from images used to require manual masking in Photoshop or unreliable automated tools that left jagged edges around hair, fur, and transparent objects. BiRefNet (Bilateral Reference Network) changed that by introducing a deep learning architecture purpose-built for high-resolution dichotomous image segmentation. Wireflow gives you direct access to BiRefNet as a node in visual AI workflows, so you can integrate background removal into automated pipelines without managing infrastructure or writing model-serving code.

This guide walks through every step: how the BiRefNet API works, which model variant to choose, how to call the endpoint with code, and how to chain it with other processing steps for production use.

What Is BiRefNet and Why Does It Matter?

BiRefNet stands for Bilateral Reference Network. Unlike older segmentation models such as U2-Net or rembg, BiRefNet uses a bilateral reference framework that processes images at multiple resolutions simultaneously. The result is sharper masks, especially around fine details like individual hair strands, semi-transparent fabrics, and complex edges where the subject blends into the background.

The model ships in several variants, each tuned for different use cases:

  • BiRefNet-DIS_ep580 (Light): fastest inference, suitable for bulk processing where speed matters more than pixel-perfect accuracy
  • BiRefNet-massive-epoch_240 (Heavy): highest accuracy for complex scenes with multiple overlapping subjects
  • BiRefNet-portrait-TR_P3M_10k (Portrait): optimized specifically for human subjects, delivering the cleanest results on headshots, full-body shots, and group photos

All three variants support resolution processing at 1024x1024 for standard workflows or 2048x2048 for high-resolution source images, giving you control over the quality-speed tradeoff.

Setting Up Your Environment

Before calling the BiRefNet API, you need an API key and a basic HTTP client. If you are working within Wireflow's API ecosystem, you can skip manual setup entirely and use the visual canvas to drag a BiRefNet node into your workflow.

For direct API calls, here is what you need:

  1. Get an API key from the provider hosting BiRefNet (fal.ai is the most common host for the BiRefNet endpoint)
  2. Install a client library or use plain curl/fetch
  3. Prepare your input image as a publicly accessible URL or base64-encoded string

Setting up BiRefNet API environment

Here is a minimal setup using Node.js with the fal client:

import { fal } from "@fal-ai/client";

fal.config({ credentials: "YOUR_FAL_API_KEY" });

For Python, the equivalent setup is:

import fal_client
fal_client.api_key = "YOUR_FAL_API_KEY"

No GPU provisioning or container management required. The API handles all inference server scaling behind the scenes, which is a significant advantage over self-hosting background removal models on your own infrastructure.

Calling the BiRefNet API: Step by Step

Step 1: Send the Image

The core API call accepts an image URL and returns a mask plus the background-removed result. Here is a complete curl example:

curl -X POST "https://fal.run/fal-ai/birefnet" \
  -H "Authorization: Key $FAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "image_url": "https://example.com/photo.jpg",
    "model": "General Use (Light)",
    "operating_resolution": "1024x1024",
    "output_format": "png"
  }'

The response includes two outputs: the segmented image with a transparent background and the raw binary mask. Both are useful depending on your downstream pipeline. For e-commerce product photo workflows, you typically want the transparent PNG directly.

Step 2: Choose the Right Model Variant

Match the model to your content type:

Content Type Recommended Model Why
Product photos General Use (Light) Clean edges on solid objects, fast processing
Portraits and headshots Portrait Handles hair and skin tones with fewer artifacts
Complex scenes General Use (Heavy) Multiple overlapping subjects, fine detail
Batch processing General Use (Light) Lowest latency per image

Step 3: Handle the Response

The API returns a JSON object with URLs to the processed images:

const result = await fal.subscribe("fal-ai/birefnet", {
  input: {
    image_url: "https://example.com/photo.jpg",
    model: "General Use (Heavy)",
    operating_resolution: "1024x1024",
    output_format: "png"
  }
});

// result.data.image.url → transparent PNG
// result.data.mask.url  → binary segmentation mask

Save both outputs. The mask is valuable if you plan to composite new backgrounds or apply further edits like color grading or shadow generation.

Processing pipeline results

Step 4: Refine the Foreground (Optional)

Enable the refine_foreground parameter to apply mask-guided edge refinement. This adds a small amount of processing time but reduces halo artifacts around semi-transparent areas:

{
  "image_url": "https://example.com/photo.jpg",
  "model": "General Use (Heavy)",
  "refine_foreground": true,
  "output_format": "png"
}

This is especially useful for fashion photography and transparent background generation where even small edge artifacts are visible on white or patterned backgrounds.

Building a Production Pipeline with BiRefNet

Single API calls work for one-off tasks, but production use cases demand batching, error handling, and chaining with downstream models. A typical automated pipeline might look like this:

  1. Receive product images via webhook or S3 event
  2. Run BiRefNet background removal on each image
  3. Composite onto a branded background template
  4. Upscale the result to 4K using an AI upscaler
  5. Upload finished assets to CDN

You can build this entire sequence visually in Wireflow without writing orchestration code. Each step becomes a node on the canvas, with data flowing automatically between them. The platform handles retries, parallel execution, and batch processing across hundreds of images.

For developers who prefer code, the same pipeline is achievable through Wireflow's REST API. Create a workflow via POST /api/v1/workflows, define your nodes and edges, then trigger execution via POST /api/v1/workflows/{id}/execute. Poll for results or set up a webhook to receive a callback when processing completes.

Production pipeline architecture

BiRefNet vs Other Background Removal Models

How does BiRefNet compare to alternatives? Here is a practical breakdown for developers evaluating their options, based on insights from tools used by AI developers in 2026:

Feature BiRefNet rembg (U2-Net) Remove.bg API
Edge quality on hair Excellent Good Very Good
Processing speed ~1.5s per image ~0.8s per image ~2s per image
Self-hosting option Yes (HuggingFace) Yes (pip install) No
API availability fal.ai, Wireflow Self-host only Proprietary
Resolution support Up to 2048x2048 Up to 1024x1024 Up to 4096x4096
Mask output Yes Yes Yes (paid tier)
Batch pricing Pay-per-call Free (self-host) Subscription

BiRefNet wins on edge quality for complex subjects, while rembg is hard to beat on cost if you are already running GPU infrastructure. For most teams, the managed API approach through platforms like Wireflow's node-based editor eliminates the infrastructure overhead entirely.

Tips for Getting the Best Results

  • Use high-resolution source images. BiRefNet performs better with more pixel data. Upscale low-resolution inputs before sending them through the API.
  • Match the model to the subject. Portrait mode on product photos wastes processing time. Light mode on complex portraits loses edge detail.
  • Enable foreground refinement selectively. It adds latency, so only use it when edge quality is critical, like product photography for online stores.
  • Cache masks for re-compositing. If you plan to test multiple backgrounds, save the mask separately and reuse it instead of re-running BiRefNet each time.

Optimization tips for BiRefNet

Try It Yourself

Ready to build your own background removal pipeline? Open this BiRefNet workflow in Wireflow to see the exact node setup discussed above, pre-configured with an image input feeding directly into a BiRefNet processing node. You can also explore the full API documentation to integrate background removal into your own applications programmatically.

FAQ

What is BiRefNet?

BiRefNet (Bilateral Reference Network) is an AI model designed for dichotomous image segmentation. It separates foreground subjects from backgrounds with high precision, producing clean alpha masks even around fine details like hair, fur, and semi-transparent objects.

Is the BiRefNet API free to use?

BiRefNet is open source and can be self-hosted for free. Managed API access through providers like fal.ai charges per call, typically around $0.01-0.03 per image depending on resolution and model variant.

Which BiRefNet model should I choose?

Use the Light model for speed-sensitive batch processing, the Heavy model for maximum accuracy on complex scenes, and the Portrait model for human subjects where hair and skin edge quality matters most.

Can I process multiple images at once?

Yes. Most API providers support concurrent requests. With Wireflow, you can configure batch nodes that process hundreds of images in parallel through a single workflow execution.

What output formats does BiRefNet support?

The API returns PNG images with transparent backgrounds by default. You also receive the raw binary segmentation mask, which you can use for custom compositing or further processing.

How does BiRefNet compare to Remove.bg?

BiRefNet offers comparable edge quality with the added benefit of self-hosting options and mask-only output. Remove.bg provides a more polished consumer experience but locks you into proprietary pricing. For API-driven workflows, BiRefNet provides more flexibility.

Can I chain BiRefNet with other AI models?

Yes. Background removal is often the first step in multi-model pipelines. Common next steps include background replacement, image upscaling, color correction, and format conversion. Wireflow's visual canvas makes these chains straightforward to build and test.

What image sizes does BiRefNet handle?

BiRefNet processes images at configurable operating resolutions of 1024x1024 or 2048x2048 pixels. Input images are scaled to the operating resolution for segmentation, then the mask is applied back at the original resolution for full-quality output.