Alpha Matte (Binary)#

Generate a grayscale alpha matte (transparency mask) for an input image. The response is a binary PNG where pixel intensity encodes opacity (0 = fully transparent, 255 = fully opaque).

When to Use This Endpoint#

Use the binary alpha matte endpoint when:

  • Server-to-server workflows: Direct file I/O without JSON overhead
  • CLI tools and scripts: Save the mask directly to disk
  • Backend processing pipelines: Efficient binary transfers for compositing workflows
  • Non-destructive editing: Keep the mask separate for advanced compositing, multiple background variants, or quality-preserving edits
Binary vs Base64: Choose binary for server environments and file-based workflows. Use the Base64 variant for browser-based apps, JSON APIs, or when embedding masks in structured data.
Matte vs Cutout: Use this endpoint when you need the transparency mask itself for compositing. If you want the final cut-out image with transparent background, use Background Removal instead.

Endpoint#

POST /v1.0/alpha-channel

Base URL: https://api.withoutbg.com

Description: Returns the alpha channel (mask) of the processed image.

Consumes: multipart/form-data

Produces: image/png (grayscale)

Authentication#

Send your API key in the request header:

Authentication Header

X-API-Key: YOUR_API_KEY
Security: Never embed API keys in client-side code. Use environment variables or a secrets manager, and proxy requests through your backend.

Request#

Headers#

  • X-API-Key (required) - Your API key

Body (multipart/form-data)#

  • file (required) - The image file to process

Supported input formats#

JPEG, PNG, WebP, TIFF, BMP, GIF. Max file size: 10 MB.

Response#

Success (200 OK)#

  • Content-Type: image/png
  • Body: Binary PNG containing the grayscale alpha matte

The response is a grayscale image where:

  • White (255) = fully opaque foreground
  • Black (0) = fully transparent background
  • Gray values = partial transparency (soft edges, hair, etc.)

Error Responses#

All errors return JSON:

Error Response Format

{
  "detail": "Error message here"
}
Rate limit: 7 requests per minute per API key
Examples

Examples#

cURL#

cURL Example

Upload an image file and save the alpha matte

curl -X POST "https://api.withoutbg.com/v1.0/alpha-channel" \
  -H "X-API-Key: $WITHOUTBG_API_KEY" \
  -F "file=@/path/to/input.jpg" \
  --output alpha-mask.png

This saves the alpha matte to alpha-mask.png.

Python (requests)#

Python Example

Process images using Python requests library

1import os
2import requests
3
4url = "https://api.withoutbg.com/v1.0/alpha-channel"
5headers = {"X-API-Key": os.environ["WITHOUTBG_API_KEY"]}
6
7with open("input.jpg", "rb") as f:
8    files = {"file": f}
9    r = requests.post(url, headers=headers, files=files, timeout=60)
10    r.raise_for_status()
11    
12    with open("alpha-mask.png", "wb") as out:
13        out.write(r.content)

Node.js (fetch)#

Node.js Example

Process images using Node.js with fetch and form-data

1import fs from "node:fs";
2import FormData from "form-data";
3
4const form = new FormData();
5form.append("file", fs.createReadStream("input.jpg"));
6
7const res = await fetch("https://api.withoutbg.com/v1.0/alpha-channel", {
8  method: "POST",
9  headers: { 
10    "X-API-Key": process.env.WITHOUTBG_API_KEY,
11    ...form.getHeaders()
12  },
13  body: form
14});
15
16if (!res.ok) {
17  const error = await res.text();
18  throw new Error(`HTTP ${res.status}: ${error}`);
19}
20
21const buffer = Buffer.from(await res.arrayBuffer());
22fs.writeFileSync("alpha-mask.png", buffer);

cURL with error handling#

cURL with Error Handling

Production-ready cURL example with proper error handling

1set -euo pipefail
2API=https://api.withoutbg.com/v1.0/alpha-channel
3
4http_code=$(curl -sS -w "%{http_code}" -o alpha-mask.png \
5  -H "X-API-Key: $WITHOUTBG_API_KEY" \
6  -F "file=@input.jpg" "$API")
7
8if [ "$http_code" != "200" ]; then
9  echo "Request failed with status: $http_code" >&2
10  cat alpha-mask.png >&2  # Error response is JSON
11  exit 1
12fi
Status Codes & Limits

Status Codes#

HTTP status codes and their meanings for the Alpha Matte (Binary) API
StatusDescriptionWhen it happens
401Invalid API KeyMissing or invalid API key
402Insufficient credit. Please top up API credits.Account has no credits. Check balance with /available-credit
403Credits Expired. Please top up API credits. If you have existing credits, they will be reactivated.Credits have expired. Top up to reactivate
413File size too large. Maximum file size is 10.0 MBFile exceeds 10 MB limit
415Unsupported Media Type. Supported formats are: JPEG, PNG, WebP, TIFF, BMP, GIF.Wrong file format
422Validation ErrorMissing required fields or corrupt image
429Rate limit (7 request/minute) exceededToo many requests. Implement exponential backoff
5xxInternal Server Error. Please contact support: contact@withoutbg.comTemporary server issue. Retry with backoff

Rate & Size Limits#

  • Rate limit: 7 requests/minute per API key
  • Max input size: 10 MB
Best Practices

Best Practices#

  • Timeouts: Set HTTP timeouts of at least 60 seconds to cover upload and processing time
  • Retries: Implement exponential backoff for transient 429 and 5xx errors
  • Pre-resize: Downscale large images before upload to reduce latency and cost
  • Monitor credits: Poll /available-credit to track usage and set up alerts
  • Rate limiting: Stay within 7 requests/minute. Queue requests on your side if needed
  • Security: Store API keys in environment variables or secrets managers, never in code repositories
  • Error handling: Parse JSON error responses and handle 402/403 by prompting users to top up credits
Troubleshooting

Troubleshooting#

  • 401 errors: Verify your API key is correct and included in the X-API-Key header
  • 402/403 credit errors: Check /available-credit and top up if needed
  • 413 errors: Input image exceeds 10 MB. Compress or resize before uploading
  • 415 errors: Ensure the file is one of the supported formats (JPEG, PNG, WebP, TIFF, BMP, GIF)
  • 422 errors: Verify the file field is present and contains a valid image
  • 429 rate limit: Implement exponential backoff. Wait progressively longer between retries
  • Mask appears all black/white: Some viewers don't render grayscale PNGs well. Open in an image editor to verify the gradient
  • Unexpected results: The alpha matte shows where the subject is (white) vs background (black). Use with Background Removal output for compositing
Related Endpoints
Quick Checklist

Quick Checklist#

  • POST to /v1.0/alpha-channel
  • X-API-Key header present with valid key
  • multipart/form-data with file field
  • File is under 10 MB and in supported format
  • HTTP timeout set to at least 60 seconds
  • Save response body as *.png
  • Handle 402/403 credit errors by checking /available-credit
  • Implement exponential backoff for 429 rate limit errors
  • Never expose API keys in client-side code