Skip to content

Image Optimization

Transform and optimize images served from IPFS on-the-fly using query parameters. This is a public endpoint that requires no authentication.

Optimize Image

GET /image/:cid

Returns the image at the given CID, transformed according to the provided query parameters. If no parameters are provided, the request redirects to the original image.

Path parameters

ParameterTypeRequiredDescription
cidstringYesThe IPFS content identifier of the image.

Query parameters

ParameterTypeDefaultDescription
wnumberOutput width in pixels. Max 4096.
hnumberOutput height in pixels. Max 4096.
formatstringOutput format: webp, jpeg, png, or avif.
qualitynumber80Compression quality, 1-100. Only applies when format is set.
fitstringcoverHow the image should fit the dimensions: cover, contain, fill, inside, or outside.

Fit modes

ModeBehavior
coverCrop to cover both dimensions (default).
containFit within both dimensions, preserving aspect ratio. May leave empty space.
fillStretch to fill both dimensions exactly. May distort the image.
insideLike contain, but only scales down, never up.
outsideLike cover, but only scales down, never up.

Example requests

Resize to 400px wide, convert to WebP:

bash
curl "https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=400&format=webp"

Resize and crop to 200x200 thumbnail as JPEG at 60% quality:

bash
curl "https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=200&h=200&format=jpeg&quality=60&fit=cover"

Using in HTML

Reference optimized images directly in img tags:

html
<img
  src="https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=800&format=webp&quality=75"
  alt="Optimized IPFS image"
/>

Serve different sizes with srcset:

html
<img
  srcset="
    https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=400&format=webp 400w,
    https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=800&format=webp 800w,
    https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=1200&format=webp 1200w
  "
  sizes="(max-width: 600px) 400px, (max-width: 1000px) 800px, 1200px"
  src="https://api.ipfs.ninja/image/QmXmCX9S6ANV...?w=800&format=webp"
  alt="Responsive IPFS image"
/>

Caching

Responses are served with immutable cache headers. Since IPFS content is content-addressed, the same CID with the same parameters always produces the same output. Browsers and CDNs can cache these responses indefinitely.

Availability

Image optimization is available on all plans, including the free Dharma plan.