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/file-ops/upload/
Content-Typemultipart/form-data
AuthToken YOUR_API_KEY
Bodyfiles + upload_path
Returnsfile_paths
Step 2 Process Image
MethodPOST
Endpoint/v3/tools/ai/remove-bg/process/
Content-Typeapplication/json
AuthToken YOUR_API_KEY
BodyJSON with input_file_path + output_variants
Returnsoutput_file_paths
import requests

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

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

upload_response.raise_for_status()
input_file_path = upload_response.json()["file_paths"][0]

# Step 2 — Process with custom background colors (application/json)
result = requests.post(
    f"{API_BASE}/v3/tools/ai/remove-bg/process/",
    headers={**HEADERS, "Content-Type": "application/json"},
    json={
        "input_file_path": input_file_path,
        "output_variants": [
            {
                "output_file_path": "my-project/blue-bg.jpg",
                "background_color": "#1A01FF",
                "format": "jpeg",
                "output_compression": 90
            },
            {
                "output_file_path": "my-project/white-bg.jpg",
                "background_color": "#ffffff",
                "format": "jpeg",
                "output_compression": 90
            },
            {
                "output_file_path": "my-project/transparent.png",
                "format": "png"
                # background_color omitted = transparent
            }
        ]
    },
    timeout=300
)

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

blue_path, white_path, transparent_path = output["output_file_paths"]
print("Blue:       ", blue_path)
print("White:      ", white_path)
print("Transparent:", transparent_path)
Response
{
    "output_file_paths": [
        "my-project/blue-bg.jpg",
        "my-project/white-bg.jpg",
        "my-project/transparent.png"
    ]
}
FieldTypeDescription
output_file_pathsstring[]Ordered list of output file paths in your space, one per variant in the same order as output_variants.
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 fields, or malformed JSON
401Missing or invalid Authorization header
402Insufficient credits
404File not found — input_file_path does not exist
429Rate limit exceeded — slow down requests
Tips
  • Use JPEG or WebP for solid color backgrounds — 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.
  • Reuse the image — call /v3/tools/ai/remove-bg/process/ again later with the same input_file_path to try new background colors. See Image Reuse.