Remove Background: Image Reuse

Generate new output variants from a previously uploaded image without re-uploading it.

How it works:

  1. Upload your image once to get a unique_filename. Save it.
  2. Call /v3/remove-bg/process-image/ with your initial output_variants.
  3. Later, call /v3/remove-bg/process-image/ again with the same unique_filename and new variants — no re-upload, no extra upload cost.
import requests

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

# =============================================
# FIRST TIME — upload + initial processing
# =============================================

# 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"]
print("unique_filename:", unique_filename)  # save this

# Step 2 — Process with initial variant (application/json)
result1 = 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": "initial", "quality": "low"}
        ]
    },
    timeout=300
)

result1.raise_for_status()
print("Initial:", result1.json()["output_urls"]["initial"])

# =============================================
# LATER — new variants, NO re-upload needed
# =============================================
result2 = requests.post(
    f"{API_BASE}/v3/remove-bg/process-image/",
    headers={**HEADERS, "Content-Type": "application/json"},
    json={
        "unique_filename": unique_filename,      # same filename
        "output_variants": [
            {
                "variant_name": "high_res",
                "max_resolution": 4000000,
                "quality": "high",
                "autocrop": True
            },
            {
                "variant_name": "red_bg",
                "background_color": "#FF5733",
                "format": "jpg",
                "output_compression": 90
            }
        ]
    },
    timeout=300
)

result2.raise_for_status()
output = result2.json()
print("High res:", output["output_urls"]["high_res"])
print("Red BG:  ", output["output_urls"]["red_bg"])
Process Response

The response is identical whether this is the first or a subsequent processing call:

{
    "unique_filename": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "input_url": "https://cdn.backgroundcut.co/.../input.jpg",
    "output_urls": {
        "high_res": "https://cdn.backgroundcut.co/.../high_res.png",
        "red_bg": "https://cdn.backgroundcut.co/.../red_bg.jpg"
    }
}
FieldTypeDescription
unique_filenamestringThe image identifier. Same value across all processing calls for this image.
input_urlstringURL to the original uploaded image.
output_urlsobjectMap of variant_name → output URL. Contains one entry per variant in this call.
Error Response

All errors return JSON with a detail field:

{
    "detail": "Image not found or has expired."
}
StatusMeaning
400Bad request — missing or invalid fields, malformed JSON
401Missing or invalid Authorization header
402Insufficient credits — each processing call deducts credits
404Image not found — unique_filename has expired or never existed
429Rate limit exceeded — slow down requests
Use Cases
  • E-commerce: Upload once, then generate marketplace-specific variants (Amazon, Shopify, eBay) with different sizes and backgrounds on demand.
  • A/B testing: Try different background colors or quality settings on the same image without paying to re-upload each time.
  • Progressive generation: Generate a fast low-quality preview first, then request a high-resolution variant once the user confirms.
  • Responsive images: Generate additional output sizes later as new requirements arise.
Tips
  • Each process call costs credits — only the upload is free to reuse. Each call to /v3/remove-bg/process-image/ deducts credits based on the variants requested.
  • Output URLs are temporary — download or cache results. The unique_filename persists but output URLs will expire.
  • Images expireunique_filename references are not permanent. Plan to process within a reasonable window after upload.