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
npm install @uploadcare/cdn-urlpnpm add @uploadcare/cdn-urlyarn add @uploadcare/cdn-urlbun add @uploadcare/cdn-urldeno add npm:@uploadcare/cdn-urlNo 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.
| Environment | Support |
|---|---|
| Node.js | ≥ 20. Production bundle by default; node --conditions=development opts into dev checks |
| Browsers | evergreen: 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:
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 automaticallyEvery 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:
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.jpgParsing 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:
| Entry | What's inside |
|---|---|
@uploadcare/cdn-url | parseCdnUrl plus per-kind parsers and guards, serializeCdnUrl, parseOperations, domain helpers, types — and everything from /tiny |
@uploadcare/cdn-url/ops | 48 image operation creators, including a rawOp escape hatch |
@uploadcare/cdn-url/group | group ids, nth elements, archives |
@uploadcare/cdn-url/proxy | delivery proxy URLs for remote sources |
@uploadcare/cdn-url/video | video conversion paths |
@uploadcare/cdn-url/document | document conversion paths |
@uploadcare/cdn-url/gif2video | animated image → video URLs |
@uploadcare/cdn-url/builder | chainable CdnUrl facade |
@uploadcare/cdn-url/validate | cross-operation chain diagnostics, plus the rule sets behind them |
@uploadcare/cdn-url/tiny | string-level file-URL split/join and typed modifier literals, no runtime checks |
@uploadcare/cdn-url/cdn-base | prefixedCdnBase 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.
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').hrefSee Functional core vs builder vs fluent.
Where next
- CDN URL anatomy: how the URLs are structured and what the parser gives you
- Render stored URLs: the most common real-world task
- Functional core vs builder: the API styles, and when to use which
- The string-level API: when a size budget decides the design
- Dev & production bundles: what throws where, and why production is silent
- API Reference: every function, with examples