Skip to content
smallPict
Start free

Engineering2 min read

One API Request, Several Image Formats

smallPict's API can now return WebP, AVIF, JPEG XL, JPEG and PNG from a single upload and job. Here is how target_formats works and what it costs.

smallPict Team

Engineering

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:

JSON
{  "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:

Response · 200 OK
{  "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 url is 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 jxl the encoder could not make comes back as WebP with a format_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_formats only when GET /v1/quota reports capabilities.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_large with an upgrade_url. Read error.code from 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_url is processed.

Full details are in the Image Optimization Endpoint and Error Codes docs.

Written by smallPict Team

Engineering

We build image conversion and CDN delivery for websites and apps.

Try it on your own images.

100 MB a month free. About two minutes to set up on WordPress.

More on image performance, Core Web Vitals and cloud storage.

All articles