Skip to content

Getting started ​

@uploadcare/cdn-url builds and parses Uploadcare CDN URLs: image transformations, file groups, delivery proxy and conversion paths. It ships full TypeScript types and one runtime dependency, @uploadcare/cname-prefix, used only if you ask it to derive your CDN base from a public key.

Beta

The API may still change in a minor release while this notice is here, so pin an exact version (npm install --save-exact).

Install ​

sh
npm install @uploadcare/cdn-url
sh
pnpm add @uploadcare/cdn-url
sh
yarn add @uploadcare/cdn-url
sh
bun add @uploadcare/cdn-url
sh
deno add npm:@uploadcare/cdn-url

No bundler at all? The IIFE global build works from a <script> tag via unpkg/jsdelivr.

Supported environments ​

The library touches no DOM, and its one dependency picks node:crypto under Node, so it runs anywhere modern JavaScript runs. sideEffects: false, ESM only.

EnvironmentSupport
Node.js≥ 20. Production bundle by default; node --conditions=development opts into dev checks
Browsersevergreen: Chrome/Edge 87+, Firefox 78+, Safari 14+. The full test suite runs in real Chromium on every change
Bun, Deno✓ via npm compatibility
Edge runtimes (Cloudflare Workers, Vercel Edge, …)✓, standard JavaScript only
React Native✓ with a spec-compliant URL polyfill (Hermes' built-in URL is incomplete)
No bundler✓ via the IIFE global build

The only platform features required are URL and String.prototype.replaceAll (ES2021), present in every runtime above. Build artifacts are additionally smoke-tested in plain Node (every entry point) on every build.

Build a URL ​

Every URL needs the host your project delivers from — its CDN base. https://1s4oyld5dc.ucarecd.net below is a project-prefixed base, derived from a public key with prefixedCdnBase; that page covers legacy ucarecdn.com and custom CNAMEs too.

Compose typed operations into a URL:

ts
import { serializeCdnUrl } from '@uploadcare/cdn-url'
import { preview } from '@uploadcare/cdn-url/ops'

serializeCdnUrl({
  cdnBase: 'https://1s4oyld5dc.ucarecd.net',
  uuid: 'c2499162-eb07-4b93-b31e-94a89a47e858',
  operations: [preview(1000, 400)]
})
// → https://1s4oyld5dc.ucarecd.net/c2499162-…/-/preview/1000x400/
// format/auto + adaptive quality are applied by the CDN automatically

Every operation creator validates its input eagerly during development, so quality('ultra') throws a RangeError telling you the allowed values. In production builds these checks are stripped for size: see Dev & production bundles for the exact contract.

Parse and edit a URL ​

Any CDN URL decomposes into a plain object you can edit and serialize back:

ts
import { parseFileUrl, serializeCdnUrl } from '@uploadcare/cdn-url'
import { stripMeta } from '@uploadcare/cdn-url/ops'

const parsed = parseFileUrl(
  'https://1s4oyld5dc.ucarecd.net/c2499162-eb07-4b93-b31e-94a89a47e858/-/resize/300x/photo.jpg'
)
// { kind: 'file', uuid: 'c2499162-…', operations: [{ name: 'resize', params: ['300x'] }], filename: 'photo.jpg', … }

serializeCdnUrl({
  ...parsed,
  operations: [...parsed.operations, stripMeta('sensitive')]
})
// → https://1s4oyld5dc.ucarecd.net/c2499162-…/-/resize/300x/-/strip_meta/sensitive/photo.jpg

Parsing is lenient: operations the library doesn't know (including internal @-prefixed ones) pass through untouched, so serializeCdnUrl(parseCdnUrl(url)) === url always holds.

Pick your entry points ​

The package is split into atomic entry points, so you import only what you use:

EntryWhat's inside
@uploadcare/cdn-urlparseCdnUrl plus per-kind parsers and guards, serializeCdnUrl, parseOperations, domain helpers, types — and everything from /tiny
@uploadcare/cdn-url/ops48 image operation creators, including a rawOp escape hatch
@uploadcare/cdn-url/groupgroup ids, nth elements, archives
@uploadcare/cdn-url/proxydelivery proxy URLs for remote sources
@uploadcare/cdn-url/videovideo conversion paths
@uploadcare/cdn-url/documentdocument conversion paths
@uploadcare/cdn-url/gif2videoanimated image → video URLs
@uploadcare/cdn-url/builderchainable CdnUrl facade
@uploadcare/cdn-url/validatecross-operation chain diagnostics, plus the rule sets behind them
@uploadcare/cdn-url/tinystring-level file-URL split/join and typed modifier literals, no runtime checks
@uploadcare/cdn-url/cdn-baseprefixedCdnBase and its async twin: your project's own CDN host

Prefer one import for everything? ​

The fluent entry trades tree-shaking for convenience: the whole library behind a single chainable object.

ts
import { cdn } from '@uploadcare/cdn-url/fluent'
import { prefixedCdnBase } from '@uploadcare/cdn-url/cdn-base'

const myCdn = cdn.base(prefixedCdnBase('demopublickey'))

myCdn.file(uuid).preview(1000, 400).stripMeta('sensitive').href

See Functional core vs builder vs fluent.

Where next ​