Background Removal
Removes the background from a photo and keeps the subject. One request in, two PNGs out: image (the subject on a transparent background) and mask (8-bit greyscale), both at the input's upright size. It usually takes under 3 seconds per image.
Create a job
POST /v1/image/remove-background with the image and, optionally, the model and a webhook URL. It answers 202 Accepted with the Job. Send an Idempotency-Key so a retry never creates a second job. Scopes: jobs:write, plus files:write with input_url or a multipart file (the input becomes a File).
curl -s https://api.zilapi.com/v1/image/remove-background \
-H "Authorization: Bearer $ZILAPI_KEY" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"input_url": "https://example.com/product.jpg",
"model": "zilapi/bgr-1.0",
"webhook_url": "https://your-app.example.com/hooks/zilapi"}' \
> job.json
JOB_ID=$(jq -r .id job.json)| Field | Type | Description |
|---|---|---|
| input_url | string | An https URL we fetch. The job waits (waiting_for_file) until the image is fetched and checked. |
| file | string | Instead of input_url: a file_… id from Files, or, in a multipart request, the image itself. Send exactly one input. |
| model | string | Optional. Default zilapi/bgr-1.0. |
| webhook_url | string | Optional. An https URL that receives this job's job.succeeded, job.failed or job.canceled event, signed. See Webhooks. Example: https://your-app.example.com/hooks/zilapi. |
| end_user | string | Optional. Your own id for the user or workspace this job is for: 1 to 128 characters from A-Z a-z 0-9 . _ : @ -. Send an opaque or hashed id, never a raw email. See Jobs. |
There are no other options: the model finds the subject itself, and the output is always a full-size PNG. Background colours, crops and resizing are done on your side from the cut-out (see Migrating from remove.bg).
Send a local image
For an image on your machine, send it in the same request as multipart/form-data, in a field named file (up to 50 MB; 25 MB with a test key). model, webhook_url and end_user go along as plain fields if you want them. Above about 5 MB, or far from the US East Coast, upload it straight to storage first (below).
curl -s https://api.zilapi.com/v1/image/remove-background \
-H "Authorization: Bearer $ZILAPI_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-F "file=@product.jpg;type=image/jpeg" \
> job.json
JOB_ID=$(jq -r .id job.json) # job_…, status "waiting_for_file" or "queued"POST /v1/files, PUT the image to the signed URL, POST /v1/files/{id}/complete, then create the job with file (example below). The bytes go straight to storage instead of through our API servers on the US East Coast, so uploads from far away are more reliable, and a failed PUT can simply be sent again to the same URL. Keep the one-request upload for small files. More on uploads.#!/usr/bin/env bash
set -euo pipefail # stop at the first failed step
FILE=product.jpg
AUTH="Authorization: Bearer $ZILAPI_KEY"
# 1. Start the upload: a file id and a signed PUT URL, valid for 6 hours
curl -fsS https://api.zilapi.com/v1/files -H "$AUTH" \
-H "Content-Type: application/json" \
-d '{"filename": "'"$FILE"'", "size_bytes": '"$(wc -c < "$FILE")"', "content_type": "image/jpeg"}' \
> upload.json
FILE_ID=$(jq -er .id upload.json)
[ "$(jq -r .upload.method upload.json)" = PUT ] || { echo "over 100 MB: upload it in parts, see https://zilapi.com/docs/files/#multipart" >&2; exit 1; }
# 2. PUT the bytes straight to storage, with every header in upload.headers.
# A failed PUT is safe to send again to the same URL: --retry does that.
mapfile -t HEADERS < <(jq -r '.upload.headers | to_entries[] | "-H", "\(.key): \(.value)"' upload.json)
curl -fsS --retry 3 --retry-all-errors -X PUT "$(jq -r .upload.url upload.json)" "${HEADERS[@]}" --data-binary @"$FILE"
# 3. Finish. Usually "ready" in this response; a large file can still be "validating" for a moment
curl -fsS -X POST https://api.zilapi.com/v1/files/$FILE_ID/complete -H "$AUTH" > file.json
while [ "$(jq -r .status file.json)" = validating ]; do
sleep 2
curl -fsS https://api.zilapi.com/v1/files/$FILE_ID -H "$AUTH" > file.json
done
[ "$(jq -r .status file.json)" = ready ] || { jq -c '{status, error}' file.json >&2; exit 1; }
# 4. Create the job with the file id
curl -fsS https://api.zilapi.com/v1/image/remove-background -H "$AUTH" \
-H "Content-Type: application/json" \
-H "Idempotency-Key: $(uuidgen)" \
-d '{"file": "'"$FILE_ID"'", "model": "zilapi/bgr-1.0"}' \
> job.json
JOB_ID=$(jq -er .id job.json)
echo "$JOB_ID $(jq -r .status job.json)" # job_… queuedOutputs: image and mask
A succeeded job lists both files in output.files (format: png). Ask for each with GET /v1/jobs/{id}/output?name=…: a signed download URL, valid for up to 1 hour, plus the file's name. Without name you get the first, image. Wait until the job has succeeded first (or use webhook_url).
while :; do
STATUS=$(curl -s https://api.zilapi.com/v1/jobs/$JOB_ID -H "Authorization: Bearer $ZILAPI_KEY" | jq -r .status)
case "$STATUS" in waiting_for_file|queued|processing) sleep 1 ;; *) break ;; esac
done
echo "$STATUS" # succeeded (or failed, canceled)| Name | Ask with | Filename | Contains |
|---|---|---|---|
| image | ?name=image | <stem>-cutout.png | The subject on a transparent background: PNG with alpha (RGBA), at the input's upright size |
| mask | ?name=mask | <stem>-mask.png | The alpha mask: 8-bit greyscale PNG, the same size (white = subject) |
for NAME in image mask; do
curl -s "https://api.zilapi.com/v1/jobs/$JOB_ID/output?name=$NAME" -H "Authorization: Bearer $ZILAPI_KEY" > output.json
curl -s -o "$(jq -r .filename output.json)" "$(jq -r .url output.json)" # product-cutout.png, product-mask.png
done<stem> is your input's filename without its extension. Outputs are deleted 7 days after the job succeeds.
Speed and throughput
- Usually under 3 seconds per image.
- The 120 job creates per minute per API key apply to image jobs too (the shared create bucket, see Rate limits).
- Up to 3 background removal jobs run in parallel per account. More are queued and run as slots free up, within the 24-hour job lifetime: they do not fail because they waited. Send a big batch at your own pace and collect results by webhook or polling.
- Your account's limit on live jobs at once still applies: 10 jobs before your first top-up; after it, 500 image jobs, counted separately from your audio jobs. Over it, a create answers
429 too_many_active_jobswithRetry-After, and the sameIdempotency-Keymakes the retry safe. See account limits.
Price and limits
zilapi/bgr-1.0costs $0.01 / image, for both outputs. Failed and canceled jobs, and test keys, cost nothing.- Inputs: PNG, JPEG and WebP up to 50 MB, 10,000 px per side and 50 megapixels (WebP, and progressive JPEGs saved without colour subsampling (4:4:4), in CMYK or with unusual colour sampling, up to 20 megapixels). The EXIF orientation is applied. Animated images and HEIC are not supported.
- A file that is not a supported image fails the job with
invalid_image; one over the pixel limits withimage_too_large; a file over 50 MB gets413 file_too_large. Nothing is charged. - Images and audio are never used for training without your consent. See the Privacy Policy.
Statuses, polling and canceling work as for every job: see Jobs. Model details: zilapi/bgr-1.0. Compare every model on Pricing.