Remove Background: Custom Background
Replace the removed background with a solid color. Generate transparent and colored variants in one call.
Note: The two steps use different content types. Upload uses
multipart/form-data. Process uses application/json.Step 1 Upload Image
| Method | POST |
| Endpoint | /v3/file-ops/upload/ |
| Content-Type | multipart/form-data |
| Auth | Token YOUR_API_KEY |
| Body | files + upload_path |
| Returns | file_paths |
Step 2 Process Image
| Method | POST |
| Endpoint | /v3/tools/ai/remove-bg/process/ |
| Content-Type | application/json |
| Auth | Token YOUR_API_KEY |
| Body | JSON with input_file_path + output_variants |
| Returns | output_file_paths |
background_color: Any hex color string —
#ffffff (white), #000000 (black), #1A01FF (blue), #FF5733 (orange), or 8-digit form for alpha (#ffffff80). Omit or set to null for transparent output. When using a solid background prefer format: "jpeg" or format: "webp" — they produce smaller files since no alpha channel is needed.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"
]
} | Field | Type | Description |
|---|---|---|
output_file_paths | string[] | 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."
} | Status | Meaning |
|---|---|
400 | Bad request — invalid hex color, missing fields, or malformed JSON |
401 | Missing or invalid Authorization header |
402 | Insufficient credits |
404 | File not found — input_file_path does not exist |
429 | Rate 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_variantsarray. One upload, one processing call, multiple outputs. - Reuse the image — call
/v3/tools/ai/remove-bg/process/again later with the sameinput_file_pathto try new background colors. See Image Reuse.