Skip to content

API Reference

Uploads the files and creates a Pages deployment. Returns a promise for the result.

import { deployFiles } from "pages-direct-upload";
const result = await deployFiles(files, {
accountId: "...",
projectName: "...",
apiToken: "...",
});

files is a plain object or a Map of path to content. Content is a string (encoded as UTF-8) or a Uint8Array.

  • Paths get a leading /, and backslashes become /.
  • Empty paths, directory paths, and . or .. segments throw. Two paths that normalize to the same value throw.
  • _headers and _redirects are sent as special form parts and are not served as files.
  • _worker.js and _routes.json throw, because this package deploys static files only.
  • A single file can be at most 25 MiB.
  • The number of files is limited by your project. The limit is read from the upload token (20000 on the project this was tested with), and a deployment over it throws before anything is uploaded.
option type default required
accountId string — yes
projectName string — yes
apiToken string — yes
branch string — no
commitMessage string — no
commitHash string — no
commitDirty boolean — no
contentTypes object built-in table no
maxBatchChars number 4000000 no
concurrency number 3 no
retries number 5 no
retryDelayMs number 1000 no
skipCaching boolean false no
skipIfInFlight boolean false no
signal AbortSignal — no
fetch function global fetch no
baseUrl string https://api.cloudflare.com/client/v4 no
onProgress function — no

branch: the production branch of the project deploys to production, any other name creates a preview deployment. If omitted, Cloudflare decides.

commitMessage: truncated to 384 UTF-8 bytes, the same limit wrangler uses.

contentTypes: maps extensions to content types and overrides the built-in table, for example { lua: "text/plain" }. Keys are case-insensitive and a leading dot is ignored.

maxBatchChars: the maximum number of base64 characters per upload request. A file larger than this is sent alone. Batches are also capped at 1000 files. The Cloudflare API accepted a 40 MB batch in testing, so this default is a conservative memory limit, not a server limit.

concurrency: how many upload requests run in parallel.

retries: how many times a failed request is retried. Retries use exponential backoff starting at retryDelayMs and capped at 16 seconds. Network errors, 408, 429, and 5xx responses are retried, and so is an expired upload token, after the token is refreshed. Creating the deployment is retried at most 3 times and only for Cloudflare error 8000000, so a failed request cannot create a duplicate deployment.

skipCaching: skips the check for assets Cloudflare already has and uploads everything.

skipIfInFlight: before doing any work, checks the project’s latest deployment with GET .../deployments?per_page=1. If it hasn’t reached a terminal stage yet (nothing failure or canceled, and not every stage success), deployFiles throws DeploymentInFlightError instead of starting a new deployment. This check uses your API token directly, not the upload token, so it costs one extra request and no JWT fetch when it skips. It’s a look-then-act check against Cloudflare’s deployment list, not an atomic lock, so a race is still possible between two runs starting within the same instant — see Scheduled deploys without overlap for the full pattern, including a hash-based way to skip deploys with no changed files at all.

signal: an AbortSignal to cancel the deployment.

fetch: a custom fetch implementation, for tests or proxies.

baseUrl: replaces the Cloudflare API base URL, for example to route through a proxy.

onProgress: (done, total) => void, called after each upload batch. total is the number of unique assets, and files Cloudflare already had count as done from the start.

field type description
id string Deployment ID
url string Deployment URL
environment string production or preview
aliases string[] Deployment aliases, empty if none
fileCount number Files in the deployment, not counting _headers and _redirects
uploaded number Unique assets uploaded in this call
skipped number Unique assets Cloudflare already had
hashesUpserted boolean Whether the final cache-update call succeeded
raw object The deployment object exactly as Cloudflare returned it

uploaded and skipped count unique assets, not paths: identical content under two paths is uploaded once.

hashesUpserted is false if the cache-update call failed. That does not fail the deployment. It only means the next deploy may re-upload files.

The deployment is created and processed asynchronously, so deployFiles returns before it is live.

Thrown when the Cloudflare API responds with an error.

property type description
label string Which step failed, for example upload token or deployment
status number HTTP status
code number or undefined Cloudflare’s error code, when present
body string The raw response body
import { CloudflareApiError, deployFiles } from "pages-direct-upload";
try {
await deployFiles(files, options);
} catch (error) {
if (error instanceof CloudflareApiError) {
console.error(error.label, error.status, error.code);
}
}

Invalid options and invalid files throw a plain TypeError or Error before any request is made.

Thrown by deployFiles instead of starting a deployment, when skipIfInFlight is true and the project’s latest deployment hasn’t reached a terminal stage.

property type description
deploymentId string ID of the deployment that’s still in progress
import { DeploymentInFlightError, deployFiles } from "pages-direct-upload";
try {
await deployFiles(files, { ...options, skipIfInFlight: true });
} catch (error) {
if (error instanceof DeploymentInFlightError) {
console.log("skipping, still deploying", error.deploymentId);
} else {
throw error;
}
}

Does the preparation step of deployFiles without any network calls. Returns { manifest, assets, headers, redirects }, where manifest maps each path to its hash and assets maps each unique hash to its base64 content and content type. Useful for tests, or to preview what a deployment contains.

Returns the 32-character hash Pages uses to identify a file. content is a string or Uint8Array, and extension is the file extension without the dot.

Returns a 32-character fingerprint of a manifest (the manifest field from buildManifest’s result), independent of key order. Compare it to the fingerprint saved after your last successful deploy to skip calling deployFiles entirely when no file changed. See Scheduled deploys without overlap.

Each asset is stored with a content type, and Pages serves it exactly as stored. The type comes from a small built-in table of common extensions (HTML, CSS, JavaScript, JSON, images, fonts, audio, video, and a few others). Anything not in the table, including files with no extension, gets application/octet-stream. Use contentTypes to override or extend it.

Pages may add ; charset=utf-8 to text types when serving.

Files are identified by the first 32 hex characters of the BLAKE3 hash of the file’s base64 content followed by its extension. This is the same scheme wrangler uses, and the hashes were checked against the BLAKE3 library wrangler uses.

Extensions follow Node’s path.extname rules: a file with no dot, or only a leading dot such as .gitignore, has an empty extension.

Cloudflare does not document the Direct Upload endpoints. This package follows the behavior of wrangler 4.169.1 and was checked against the live API:

step endpoint
Get an upload token GET /accounts/{id}/pages/projects/{name}/upload-token
Check for an in-flight deployment (skipIfInFlight) GET /accounts/{id}/pages/projects/{name}/deployments?per_page=1
Find missing assets POST /pages/assets/check-missing
Upload assets POST /pages/assets/upload
Update the asset cache POST /pages/assets/upsert-hashes
Create the deployment POST /accounts/{id}/pages/projects/{name}/deployments

The asset endpoints use the upload token, which lasts 30 minutes. The token is refreshed automatically when it is about to expire. The other two endpoints use your API token.

Because the endpoints are undocumented, they can change without notice.