Skip to content

Cookbook ​

Task-shaped index. Find what you're trying to do, take the snippet. The other how-to pages go deep on one topic; this one goes wide across tasks.

Every recipe is shown in all three API styles. Pick a tab and stay in it. A fourth style, the string level, trades the operation model for bytes; it gets one recipe of its own rather than a tab, since it only handles single-file URLs.

Before you start ​

The three styles do the same work at different prices. Every operation creator is an independent atom and every entry point is separate, so a bundler drops whatever you don't name:

tabwhat you importgzipped
string leveltinyParse/tinyBuild + a chain470 B
Atomicfunctional core + one op creator1677 B
BuilderCdnUrl + one op creator2315 B
Fluentcdn + a chain4903 B

Measured on one task — parse a URL, add one operation, serialize. None of them include prefixedCdnBase: recipes that build from a uuid need a CDN base, and deriving it from a public key adds 1.2 kB gzipped unless you paste the host as a literal.

Atomic is the smallest and the most explicit. Builder wraps one URL in an immutable class. Fluent puts every URL flavor behind one chainable object, and it is the only style that turns an invalid combination into a compile error: cdn.video(uuid).blur(10) does not typecheck, because video chains carry no image methods. API styles & tree-shaking compares them properly.

Everything is immutable. No method mutates in place; use the return value.

Creators double as references. Anywhere a recipe identifies an operation you can pass its name ('resize'), an operation object, or the uncalled creator (resize). The creator form is typo-proof and resolves aliases, so cropByRatio refers to the crop directive it produces.

Every snippet below assumes these bindings:

ts
import type { CdnOperation } from '@uploadcare/cdn-url'
import {
  operationMatches,
  parseCdnUrl,
  serializeCdnUrl
} from '@uploadcare/cdn-url'
import { blur, overlay, resize, scaleCrop } from '@uploadcare/cdn-url/ops'

const uuid = 'c2499162-eb07-4b93-b31e-94a89a47e858'
const cdnBase = 'https://1s4oyld5dc.ucarecd.net'
const stored = `${cdnBase}/${uuid}/-/resize/300x/-/quality/smart/`

/** Rewrite a URL's operation chain and serialize it back. */
function mapOperations(
  url: string,
  fn: (operations: CdnOperation[]) => CdnOperation[]
): string {
  const parsed = parseCdnUrl(url)
  // group roots carry no operations at all
  if (!('operations' in parsed)) return url
  return serializeCdnUrl({ ...parsed, operations: fn([...parsed.operations]) })
}
ts
import { operationMatches } from '@uploadcare/cdn-url'
import { CdnUrl } from '@uploadcare/cdn-url/builder'
import { blur, overlay, resize, scaleCrop } from '@uploadcare/cdn-url/ops'

const uuid = 'c2499162-eb07-4b93-b31e-94a89a47e858'
const cdnBase = 'https://1s4oyld5dc.ucarecd.net'
const stored = `${cdnBase}/${uuid}/-/resize/300x/-/quality/smart/`

const url = CdnUrl.parse(stored)
ts
import { operationMatches } from '@uploadcare/cdn-url'
import { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'
import { blur, overlay, resize } from '@uploadcare/cdn-url/ops'

const myCdn = cdn.base(prefixedCdnBase('demopublickey'))
const uuid = 'c2499162-eb07-4b93-b31e-94a89a47e858'
const cdnBase = 'https://1s4oyld5dc.ucarecd.net'
const stored = `${cdnBase}/${uuid}/-/resize/300x/-/quality/smart/`

// cdn.parse returns a union, so narrow once to get a FileChain
const parsed = cdn.parse(stored)
if (parsed.kind !== 'file') throw new Error('expected a file url')
const chain = parsed

parseCdnUrl and cdn.parse throw a TypeError on anything that isn't a CDN URL. Malformed strings and URLs with an empty path both fail, so wrap them when the input is a database row you don't control.

Getting a URL out ​

I want a thumbnail from a uuid ​

ts
serializeCdnUrl({
  cdnBase,
  uuid,
  operations: [scaleCrop(300, 300, { type: 'smart' })]
})
// → https://1s4oyld5dc.ucarecd.net/<uuid>/-/scale_crop/300x300/smart/
ts
new CdnUrl({
  cdnBase,
  uuid,
  operations: [scaleCrop(300, 300, { type: 'smart' })]
}).href
ts
myCdn.file(uuid).scaleCrop(300, 300, { type: 'smart' }).href
ts
import { modifiers, tinyBuild } from '@uploadcare/cdn-url/tiny'

tinyBuild({ cdnBase, uuid, modifiers: modifiers('scale_crop/300x300/smart') })

The input is loose in all three: cdnBase plus one addressing field (uuid, group or sourceUrl) is the minimum, and operations, filename, conversion, search and hash are optional. serializeCdnUrl({ cdnBase, uuid }) alone gives you the bare file URL.

One core operation is enough. The CDN applies format/auto on its own once a chain does any processing, and adaptive quality is the default for projects created on or after 2025-08-04.

I want to swap the CDN domain ​

ts
serializeCdnUrl({
  ...parseCdnUrl(stored),
  cdnBase: 'https://1zlmtnsbgr.ucarecd.net'
})
ts
url.base('https://1zlmtnsbgr.ucarecd.net').href
ts
chain.base('https://1zlmtnsbgr.ucarecd.net').href
ts
import { tinyBuild, tinyParse } from '@uploadcare/cdn-url/tiny'

// file URLs only — see the caveat in "smallest possible bundle" below
tinyBuild({ ...tinyParse(stored), cdnBase: 'https://1zlmtnsbgr.ucarecd.net' })

ucarecd.net is not a typo. It's the project-prefixed zone, distinct from the legacy ucarecdn.com.

I want a download filename on the URL ​

ts
const file = parseCdnUrl(stored)
if (file.kind === 'file') {
  serializeCdnUrl({ ...file, filename: 'invoice-2026.pdf' })
}
// → https://1s4oyld5dc.ucarecd.net/<uuid>/-/resize/300x/-/quality/smart/invoice-2026.pdf
ts
url.filename('invoice-2026.pdf').href
ts
chain.filename('invoice-2026.pdf').href
ts
import { tinyBuild, tinyParse } from '@uploadcare/cdn-url/tiny'

tinyBuild({ ...tinyParse(stored), filename: 'invoice-2026.pdf' })

Pass null to clear it. Group roots and proxy URLs cannot carry a filename at all. The builder and fluent forms throw in the development bundle and quietly do nothing in production; the atomic form makes you narrow on kind before you get the chance.

Note the asymmetry: what parseCdnUrl returns is fully populated, so filename is string | null and never missing. Only the input side is optional.

I want the original file, no operations ​

ts
mapOperations(stored, () => [])
// → https://1s4oyld5dc.ucarecd.net/<uuid>/
ts
url.updateOperations(() => []).href
ts
chain.updateOperations(() => []).href
ts
import { modifiers, tinyBuild, tinyParse } from '@uploadcare/cdn-url/tiny'

// the empty chain is `modifiers()` — a bare '' is not a ModifiersChain
tinyBuild({ ...tinyParse(stored), modifiers: modifiers() })

This clears the operation chain only. A filename, a ?token= query and a conversion prefix all survive. Clear the query explicitly:

ts
const withToken = parseCdnUrl(`${stored}?token=abc123`)
serializeCdnUrl({ ...withToken, search: '' })

Editing a chain ​

The string level is absent from this section on purpose: it can append to a chain and replace one wholesale, but it has no has/replace/without and no positional view, so every recipe below would mean writing your own string surgery. Appending is the one case it covers — see smallest possible bundle.

I want to change one operation and keep the rest ​

ts
mapOperations(stored, (ops) =>
  ops.map((op) => (operationMatches(op, resize) ? resize({ width: 500 }) : op))
)
// → https://1s4oyld5dc.ucarecd.net/<uuid>/-/resize/500x/-/quality/smart/
ts
url.replace(resize({ width: 500 })).href
ts
chain.replaceOp(resize({ width: 500 })).href

The three forms differ here

replace and replaceOp append when nothing matches. If the stored URL sized its image with scale_crop, replacing resize gives you a URL carrying both operations. The atomic map leaves it untouched instead.

Guard the facades when the incoming chain isn't yours:

ts
const sized = url.has(resize) ? url.replace(resize({ width: 500 })) : url

I want to change the second overlay, not the first ​

ts
const replacement = overlay(uuid, { size: ['50p', '50p'] })

let seen = 0
mapOperations(stored, (ops) =>
  ops.map((op) =>
    operationMatches(op, overlay) && seen++ === 1 ? replacement : op
  )
)
ts
const replacement = overlay(uuid, { size: ['50p', '50p'] })

let seen = 0
url.updateOperations((ops) =>
  ops.map((op) =>
    operationMatches(op, overlay) && seen++ === 1 ? replacement : op
  )
).href
ts
const replacement = overlay(uuid, { size: ['50p', '50p'] })

let seen = 0
chain.updateOperations((ops) =>
  ops.map((op) =>
    operationMatches(op, overlay) && seen++ === 1 ? replacement : op
  )
).href

seen++ === 1 picks the second match, counting from zero like an array index. replace and replaceOp only ever touch the first match. replaceAll and replaceAllOps collapse every match into one, and append when there are none.

I want to add an operation only if it isn't there ​

ts
mapOperations(stored, (ops) =>
  ops.some((op) => operationMatches(op, blur)) ? ops : [...ops, blur(10)]
)
ts
const next = url.has(blur) ? url : url.with(blur(10))
ts
const next = chain.hasOp(blur) ? chain : chain.blur(10)

I want to see what's in a URL ​

ts
const current = parseCdnUrl(stored)
const ops = 'operations' in current ? current.operations : []

ops.some((op) => operationMatches(op, blur)) // → false
ops.find((op) => operationMatches(op, resize)) // → { name: 'resize', params: ['300x'] }
ops.filter((op) => operationMatches(op, overlay)) // → []
ts
url.operations // the whole chain, as a copy
url.has(blur) // → false
url.get(resize) // → { name: 'resize', params: ['300x'] }
url.getAll(overlay) // → []
ts
chain.operations // the whole chain, as a copy
chain.hasOp(blur) // → false
chain.getOp(resize) // → { name: 'resize', params: ['300x'] }
chain.getAllOps(overlay) // → []

An operation is { name, params }, and params holds plain strings. Reading a current width means reading params[0], which is '300x' here rather than a number. The library never parses parameter values back out.

I want to insert at the front, or reorder ​

ts
mapOperations(stored, (ops) => [blur(10), ...ops])
mapOperations(stored, (ops) => [...ops.slice(0, 1), blur(10), ...ops.slice(1)])
mapOperations(stored, (ops) => ops.reverse())
ts
url.updateOperations((ops) => [blur(10), ...ops]).href
url.updateOperations((ops) => ops.reverse()).href
ts
chain.updateOperations((ops) => [blur(10), ...ops]).href
chain.updateOperations((ops) => ops.reverse()).href

updateOperations hands the callback a copy and requires an array back. A block-bodied arrow that forgets the return throws a TypeError in the development bundle and leaves the chain untouched in production.

Before reordering, check that you aren't separating an operation from the one it configures. See why isn't my text styled.

I want to edit a video conversion path ​

ts
import { size, thumbs, videoPath } from '@uploadcare/cdn-url/video'

const video = [size({ width: 720 }), thumbs(5)]
const smaller = video.map((op) =>
  operationMatches(op, size) ? size({ width: 480 }) : op
)

videoPath(uuid, smaller)
// → /<uuid>/video/-/size/480x/-/thumbs~5/
ts
import { size, thumbs } from '@uploadcare/cdn-url/video'

new CdnUrl({
  cdnBase,
  uuid,
  conversion: 'video',
  operations: [size({ width: 480 }), thumbs(5)]
}).href
// → https://1s4oyld5dc.ucarecd.net/<uuid>/video/-/size/480x/-/thumbs~5/
ts
import { size } from '@uploadcare/cdn-url/video'

cdn
  .video(uuid)
  .size({ width: 720 })
  .thumbs(5)
  .updateOperations((ops) =>
    ops.map((op) => (operationMatches(op, size) ? size({ width: 480 }) : op))
  ).path
// → /<uuid>/video/-/size/480x/-/thumbs~5/

Paths, not URLs

Video and document conversions are paths you submit to the REST convert API, not URLs you serve. Atomic and fluent both give you /<uuid>/video/…. The builder attaches the CDN base and gives you a full URL, which is the wrong shape for the convert API. Reach for it only when you actually want a URL.

Conversion chains also can't be re-parsed: parseCdnUrl and cdn.parse only read file, group, group-element, archive and proxy URLs. Build the operations before you serialize.

Match with operationMatches, never op.name === 'size'. Counted operations fuse the count into the name: thumbs(5) is literally called thumbs~5, so a string comparison against 'thumbs' never fires.

I want the smallest possible bundle ​

When a size budget decides the design and the URL is a single file you already know the shape of, skip the operation model: edit the chain as a string.

ts
import {
  joinModifiers,
  modifiers,
  tinyBuild,
  tinyParse
} from '@uploadcare/cdn-url/tiny'

const parts = tinyParse(stored)
const url = tinyBuild({
  ...parts,
  modifiers: joinModifiers(parts.modifiers, modifiers('blur/10'))
})
// → https://1s4oyld5dc.ucarecd.net/<uuid>/-/resize/300x/-/quality/smart/-/blur/10/

470 B gzipped against 1677 B for the Atomic tab. The operations stay type-checked, but nothing is validated at run time, there are no kinds, and it is file URLs only — replacing the chain on a group element or a conversion result destroys the URL. Read The string-level API before using it; it is the one style that can hand you a broken URL without an error.

Understanding a chain ​

These read a chain rather than build one, so they are the same in every style.

I want to strip GPS and EXIF before sharing ​

ts
myCdn.file(uuid).stripMeta('sensitive').href
// → …/-/strip_meta/sensitive/

'sensitive' drops identifying tags but keeps orientation and colour profile, so photos do not rotate or shift colour. 'all' and 'none' are the other two modes. It affects the derivative, not the stored original — see Redact & strip metadata.

I want to blur a face or a region ​

ts
myCdn.file(uuid).blurRegion({ faces: true }).href
// → …/-/blur_region/faces/

myCdn.file(uuid).blurRegion({
  width: '30p',
  height: '20p',
  x: '10p',
  y: '15p',
  strength: 250
}).href
// → …/-/blur_region/30px20p/10p,15p/250/

Face detection is best-effort, percentages travel better than pixels across sizes, and anyone holding the URL can delete the operation. Redact & strip metadata has the caveats.

I want a watermark in the corner ​

ts
myCdn.file(uuid).overlay(logoUuid, {
  size: ['20p', '20p'],
  position: ['90p', '90p'],
  opacity: '60p'
}).href
// → …/-/overlay/:logoUuid/20px20p/90p,90p/60p/

There is no 'se' keyword — the five are center, top, right, bottom, left — so a corner needs offsets. The options are positional: position requires size, opacity requires position. Text & watermarks explains what happens when you skip one.

I want to know if an operation can be repeated ​

ts
import { isStackable } from '@uploadcare/cdn-url/validate'
import { quality } from '@uploadcare/cdn-url/ops'

isStackable(overlay) // → true, layering several is meaningful
isStackable(quality) // → false, a second one just wins

That tells you how to edit it. false means replace the existing occurrence in place; true means decide deliberately between collapsing the duplicates into one and appending another layer.

I want to know why my text isn't styled ​

font, text_align and text_box are state for the text that follows them. Put one after its text and it silently does nothing.

ts
import { operationInputs } from '@uploadcare/cdn-url/validate'
import { font, text, textAlign } from '@uploadcare/cdn-url/ops'

const styled = [
  font({ size: 24 }),
  textAlign('center', 'bottom'),
  text(['80p', '20p'], 'bottom', 'Hi')
]

operationInputs(styled, 'text').map((edge) => edge.operation.name)
// → ['font', 'text_align'], what actually applies to that text

Each edge is { kind, operation, index, reason }. The nearest preceding modifier wins, so a second font overrides the first. A name argument resolves to the first matching operation; pass an element of the array to pin a specific one. validateOperations reports the orphaned case as modifier-without-target.

I want to know if a string is even an Uploadcare URL ​

ts
import { detectDomainKind, isUploadcareDomain } from '@uploadcare/cdn-url'

isUploadcareDomain('https://cdn.example.com') // → false
detectDomainKind('https://1zlmtnsbgr.ucarecd.net') // → 'prefixed'

detectDomainKind returns 'legacy' (ucarecdn.com), 'prefixed' (*.ucarecd.net), 'proxy' (*.ucr.io), or 'custom' for anything else. A CNAME you own reports custom, which is a correct answer rather than a failure. Input that isn't a URL at all throws a TypeError.

Things that don't work the way you'd expect ​

I want to keep my signed URL working after editing it ​

It depends on the token's acl. Secure delivery tokens survive parse and serialize untouched, and the CDN checks the request path against the ACL the token carries. Signed for an exact path (acl=/:uuid/-/preview/300x300/), any edit is a 403. Signed with a wildcard (acl=/:uuid/*), editing is fine and the token keeps verifying. Signed URLs covers both, and which to choose.

ts
const signed = `${cdnBase}/${uuid}/-/preview/300x300/?token=abc123`

mapOperations(signed, (ops) => [...ops, resize({ width: 400 })])
// still carries ?token=abc123, and the CDN will now reject it
ts
const signed = `${cdnBase}/${uuid}/-/preview/300x300/?token=abc123`

CdnUrl.parse(signed).with(resize({ width: 400 })).href
// still carries ?token=abc123, and the CDN will now reject it
ts
const signed = `${cdnBase}/${uuid}/-/preview/300x300/?token=abc123`

const s = cdn.parse(signed)
if (s.kind === 'file') s.resize({ width: 400 }).href
// still carries ?token=abc123, and the CDN will now reject it

With an exact-path ACL: re-sign after editing, or build the final chain before signing. This library preserves tokens but never generates them; signing needs your secret and happens in your backend.

Parsing does not validate values ​

parseCdnUrl and parseOperations are deliberately lenient. They accept any well-formed chain, including unknown operations and out-of-range numbers, and pass them through verbatim. blur/99999 parses fine.

For untrusted input, run validateOperations explicitly, or build operations from typed parameters instead of accepting raw modifier strings.

Development-bundle checks don't run in production ​

Operation creators validate ranges and enums eagerly, but only in the development bundle. Production strips those checks and the contract becomes garbage in, garbage out. The validate entry is the exception: it works identically in both. See dev & production bundles.

Going deeper ​

The editing recipes above are the full treatment; there's no deeper page for them. For everything else:

I want to…See
render URLs stored in a databaseRender stored URLs
build a srcsetResponsive images
crop faces into square avatarsAvatars
transform a remote imageRemote images via proxy
address group files, build a zipGroups & archives
convert video or documentsVideo & documents
vet user-supplied modifiersValidate user input
choose between the four stylesAPI styles & tree-shaking
edit URLs on a byte budgetThe string-level API
use it without a bundlerGetting started