Name your image sizes once with lenses
How CalmLens lenses turn resize and format rules into short CDN URLs, and how to define them from your deploy script.
Most applications show the same image at several sizes. A grid needs a small thumbnail, a detail page needs something wider, and you probably want a modern format wherever the browser supports it. Spelling out width, fit and format in every URL works until you need to change one of those values and have to find every place that uses it.
A lens gives that set of rules a name. It is a named transform that the CalmLens CDN applies when it serves an asset. CalmLens stores and reuses the derived image without creating a separate asset record for you to manage. Each variant is charged once on its first billable request; storage and delivery use the normal rates. You request it by adding its name to the asset's URL.
Request a lens by name
Assets live under https://cdn.calmlens.com/v1. To apply a lens, append /@ and the lens identifier to the asset's rendered URL:
/assets/:projectId/:asset/rendered/@thumbnailThe @ prefix tells the CDN that what follows is a lens name rather than a transform selector, so the two forms never collide. :asset is the asset ID, or ext: followed by your own externalId.
Request it through an SDK or over plain HTTP:
const response = await client.cdn.serve({
assetId,
lens: "thumbnail",
});
If you name a lens that doesn't exist, the CDN returns 404 with the error code lens/not-found.
Built-in lenses
Every project can use a set of built-in lenses without creating anything. They all use scale-down, so a small source image is never enlarged.
| Lens | Width |
|---|---|
thumbnail | 128 pixels |
sm | 640 pixels |
md | 768 pixels |
lg | 1024 pixels |
xl | 1280 pixels |
2xl | 1536 pixels |
None of them sets a format, so the CDN serves AVIF or WebP to browsers that accept them and the source format to everything else. Add an option after the lens to pin one: @thumbnail,format=png. The widths follow common responsive breakpoints, so a srcset can list several of them for one image.
Built-in names are resolved before your project's own lenses, so give custom lenses identifiers of their own.
Custom lenses
When the built-in sizes don't fit, define your own. A custom lens belongs to one project and has an identifier, a display name, an asset type and one set of transform options, which the CDN applies together in one pass. Identifiers use lowercase letters, numbers and hyphens, up to 50 characters.
The options cover width, height and dpr, fit (scale-down, contain, pad, squeeze, cover or crop), format (jpg, png, webp, avif or gif), quality (a number or low, medium or high), anim, rotate and flip, plus adjustments such as blur, sharpen, gravity and trim.
Define your lenses in code so they travel with your deploys. client.ensureLenses() takes a lenses array and creates or updates every definition in one request:
const response = await client.ensureLenses({
"lenses": [
{
"identifier": "card",
"name": "Card",
"type": "image",
"active": true,
"transform": {
"fit": "cover",
"width": 600,
"height": 400,
"format": "webp"
}
}
]
});
client.ensureLens() does the same for a single lens. Both calls are idempotent: sending a definition that is already saved succeeds without changing anything, and a changed definition updates the lens, so you can run them on every deploy. Managing lenses through the API requires a project API key.
Updates replace cached images
The CDN caches derived images. Without care, widening the card lens could leave cached 600-pixel images in circulation next to the new size until those entries expired.
CalmLens records a new revision whenever a lens's definition changes, and that revision is part of the CDN cache key and the ETag. A request after the update can't match an entry cached under the old definition, so the new output appears without a manual purge. There is no version for you to track: send the complete definition when you update, not only the fields that changed.
Direct transforms for one-off sizes
Not every size deserves a name. For a single use, put the options in the URL without the @, as one path segment of comma-separated options:
/assets/:projectId/:asset/rendered/fit=cover,format=png,width=1024Direct transform definitions aren't saved as lenses, so they suit experiments and rare cases. A selector that keeps reappearing across your code is a candidate for a lens. Malformed selectors return transform/invalid; unsupported options return transform-options/invalid.
Private assets and signed URLs
A lens changes the image, not who may see it. The usual delivery rules still apply: content must be retained, blocked, purged or removed assets are never served, and private assets need authorisation.
For a private asset shown in a browser, sign its asset URL on your server and append the lens to it, as you would to a public asset's URL:
const response = await client.cdn.signAssetUrl({
"assetId": "asset_5Gm2Rk8XqT3wLp9VbN4c",
"expiresIn": 900
});
The signature covers the project, the asset, whether the URL serves the original file, and its expiry. Lenses and transforms are added to the URL beneath it, such as /rendered/@thumbnail,format=png, where the direct options replace the lens's options of the same name, so one signed asset URL serves every lens.
The lenses and transforms guide covers every transform option, the bulk endpoint and the full lens API reference.