Remove Background: Custom Background

Replace the removed background with a solid color. Generate transparent and colored variants in one call.

Step 1 Upload Image
MethodPOST
Endpoint/v3/remove-bg/upload-image/
Content-Typemultipart/form-data
AuthBearer YOUR_API_KEY
BodyForm field: image (file)
Returnsunique_filename
Step 2 Process Image
MethodPOST
Endpoint/v3/remove-bg/process-image/
Content-Typeapplication/json
AuthBearer YOUR_API_KEY
BodyJSON with unique_filename + output_variants
Returnsoutput_urls keyed by variant_name
import requests

API_KEY = "YOUR_API_KEY"
API_BASE = "https://api.backgroundcut.co"
HEADERS = {"Authorization": f"Bearer {API_KEY}"}

# Step 1 — Upload image (multipart/form-data)
with open("photo.jpg", "rb") as f:
    upload_response = requests.post(
        f"{API_BASE}/v3/remove-bg/upload-image/",
        headers=HEADERS,
        files={"image": f},
        timeout=60
    )

upload_response.raise_for_status()
unique_filename = upload_response.json()["unique_filename"]

# Step 2 — Process with custom background colors (application/json)
result = requests.post(
    f"{API_BASE}/v3/remove-bg/process-image/",
    headers={**HEADERS, "Content-Type": "application/json"},
    json={
        "unique_filename": unique_filename,
        "output_variants": [
            {
                "variant_name": "blue_bg",
                "background_color": "#1A01FF",
                "format": "jpg",
                "output_compression": 90
            },
            {
                "variant_name": "white_bg",
                "background_color": "#ffffff",
                "format": "jpg",
                "output_compression": 90
            },
            {
                "variant_name": "transparent",
                "format": "png"
                # background_color omitted = transparent
            }
        ]
    },
    timeout=300
)

result.raise_for_status()
output = result.json()

print("Blue:       ", output["output_urls"]["blue_bg"])
print("White:      ", output["output_urls"]["white_bg"])
print("Transparent:", output["output_urls"]["transparent"])
Response
{
    "unique_filename": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "input_url": "https://cdn.backgroundcut.co/.../input.jpg",
    "output_urls": {
        "blue_bg": "https://cdn.backgroundcut.co/.../blue_bg.jpg",
        "white_bg": "https://cdn.backgroundcut.co/.../white_bg.jpg",
        "transparent": "https://cdn.backgroundcut.co/.../transparent.png"
    }
}
FieldTypeDescription
unique_filenamestringSame identifier from the upload step. Reuse to generate additional variants without re-uploading.
input_urlstringURL to the original uploaded image.
output_urlsobjectMap of variant_name → output image URL. One entry per requested variant.
Error Response

All errors return JSON with a detail field:

{
    "detail": "Invalid background_color. Expected hex string like #ffffff."
}
StatusMeaning
400Bad request — invalid hex color, missing image, or malformed JSON
401Missing or invalid Authorization header
402Insufficient credits
404Image not found — unique_filename does not exist or has expired
429Rate limit exceeded — slow down requests
Tips
  • Use JPG or WebP for solid color backgrounds — they are significantly smaller than PNG when no transparency is needed. PNG is best reserved for transparent outputs.
  • Generate transparent and colored variants together — include both in a single output_variants array. One upload, one processing call, multiple outputs.
  • Output URLs are temporary — download or cache results promptly. URLs will expire.
  • Reuse the image — call /v3/remove-bg/process-image/ again later with the same unique_filename to try new background colors. See Image Reuse.