Serving modern images properly usually means more than one file per image: AVIF for browsers that support it, WebP as the next choice, and a JPEG for everything else. Until now that meant one API request per format, one upload each, and one job to poll each. With target_formats, it is one of each.
The request
Add options.target_formats with up to five of webp, avif, jxl, jpg and png:
{ "filename": "hero.jpg", "mime_type": "image/jpeg", "filesize": 2450000, "options": { "target_formats": [ "avif", "webp", "jpg" ], "mode": "lossy" }}Send it to POST /v1/optimize as usual, PUT the bytes once to the upload_url you get back, and poll the job.
The result
When the job completes, its status has an outputs array in the order you asked for:
{ "status": "completed", "format": "avif", "outputs": [ { "format": "avif", "content_type": "image/avif", "size": 610000, "url": "https://..." }, { "format": "webp", "content_type": "image/webp", "size": 980000, "url": "https://..." }, { "format": "jpg", "content_type": "image/jpeg", "size": 1638000, "url": "https://..." } ]}- The first format also fills the single-format fields (
url,format), so older code keeps working. - Each
urlis a link valid for 10 minutes; poll again for a fresh one. - A format that could not be made is listed in
failed_formats. The job fails only if every format failed. - A
jxlthe encoder could not make comes back as WebP with aformat_fallback_reason.
What it costs
One job with several formats counts as one request against your monthly quota and the per-minute limit. You pay for the image, not for each format.
Things to check
- Send
target_formatsonly whenGET /v1/quotareportscapabilities.multi_format: true. Older servers ignore the field. - An empty list, more than five formats or an unknown format answers
400 VALIDATION_FAILED. - A file over your plan's per-file limit answers
400 file_too_largewith anupgrade_url. Readerror.codefrom the body.
Also new for developers
- JPEG XL on every API plan:
"format": "jxl". With"mode": "lossless"and a JPEG source, it is a lossless recompression that keeps every pixel. - Unhurried uploads. A job starts when its upload lands, so an upload anywhere inside the 15 minutes of the
upload_urlis processed.
Full details are in the Image Optimization Endpoint and Error Codes docs.