Code Examples
Production-ready code snippets in cURL, Python, JavaScript, Go, and PHP. Copy, paste, and integrate background removal in minutes.
Jump to Example
Basic Background Removal
BeginnerRemove the background from an image using a URL. The simplest way to get started with the API.
curl -X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"image_url": "https://example.com/photo.jpg"
}'File Upload
BeginnerUpload a local image file directly using multipart form data instead of providing a URL.
curl -X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-F "image=@/path/to/photo.jpg" \
-F "output_format=png"Custom Background Color
BeginnerReplace the background with a solid color instead of making it transparent. Useful for product photography and e-commerce.
curl -X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"image_url": "https://example.com/product.jpg",
"bg_color": "#FFFFFF",
"output_format": "jpeg",
"output_quality": 95
}'Batch Processing
IntermediateProcess multiple images in a single API call. Ideal for e-commerce catalogs and large media libraries.
# Step 1: Submit batch
curl -X POST https://api.bgremover.dev/v1/remove/batch \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"images": [
{ "image_url": "https://example.com/photo1.jpg" },
{ "image_url": "https://example.com/photo2.jpg" },
{ "image_url": "https://example.com/photo3.jpg" }
],
"output_format": "webp"
}'
# Step 2: Poll for results
curl https://api.bgremover.dev/v1/batch/batch_abc123 \
-H "Authorization: Bearer YOUR_API_KEY"Webhook Integration
IntermediateUse webhooks to receive real-time notifications when images finish processing, instead of polling the API.
# Submit with webhook
curl -X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"image_url": "https://example.com/photo.jpg",
"webhook_url": "https://yourapp.com/api/webhooks/bgremover"
}'
# Your webhook endpoint will receive:
# POST https://yourapp.com/api/webhooks/bgremover
# {
# "event": "image.processed",
# "data": {
# "id": "img_abc123",
# "output_url": "https://cdn.bgremover.dev/results/img_abc123.png",
# "processing_time_ms": 1240
# },
# "timestamp": "2026-02-13T22:30:00Z",
# "signature": "sha256=abc123..."
# }Robust Error Handling
AdvancedImplement production-grade error handling with retries, exponential backoff, and detailed error logging.
# Check response status code
response=$(curl -s -w "\n%{http_code}" \
-X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"image_url": "https://example.com/photo.jpg"}')
http_code=$(echo "$response" | tail -1)
body=$(echo "$response" | sed '$d')
if [ "$http_code" -eq 200 ]; then
echo "Success: $body"
elif [ "$http_code" -eq 429 ]; then
retry_after=$(echo "$body" | jq -r '.error.details.retry_after // 30')
echo "Rate limited. Retry after $retry_after seconds."
sleep "$retry_after"
else
echo "Error $http_code: $body"
fiDownload Result to File
BeginnerProcess an image and download the result directly to your local filesystem in one operation.
# Step 1: Remove background
result=$(curl -s -X POST https://api.bgremover.dev/v1/remove \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"image_url": "https://example.com/photo.jpg"}')
# Step 2: Extract output URL and download
output_url=$(echo "$result" | jq -r '.data.output_url')
curl -o result.png "$output_url"
echo "Downloaded to result.png"Ready to Build?
Get your API key and start processing images. 50 free API calls per day — no credit card required.