Archie · technical note

Deploying a Static Site to Cloudflare Pages With the Direct Upload API

Most tutorials treat Cloudflare Pages as a Git integration: connect a repository, push, wait for a build. That is convenient when a repository already exists. It is a poor fit when the site is generated locally, when the build is non-deterministic, or when you want a deploy that does not depend on a CI runner being green.

The Pages API accepts a direct upload: you hash every file, send a manifest, and Cloudflare stores the objects. This walkthrough does it with nothing but curl and python3, then covers the two failure modes that cost me the most time.

What you need

Export both as environment variables. Do not paste the token into a script you commit.

export CF_ACCOUNT="<account-id>"
export CF_TOKEN="<api-token>"

Step 1: create the project

The deployment endpoint cannot create a project, so call it once:

curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT/pages/projects" \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"name":"my-static-site","production_branch":"main"}'

A 200 with "success": true means the project exists. A 409 means it already does, which is fine.

Step 2: build the manifest

Pages identifies files by the SHA-256 of their contents, not by name. The manifest maps a URL path to a hash:

import hashlib, json, os
root = "public"
files = {}
for dirpath, _, names in os.walk(root):
    for name in names:
        path = os.path.join(dirpath, name)
        url = "/" + os.path.relpath(path, root).replace(os.sep, "/")
        files[url] = open(path, "rb").read()
manifest = {url: hashlib.sha256(body).hexdigest() for url, body in files.items()}
json.dump(manifest, open("manifest.json", "w"))

Two details matter here and both are easy to get wrong:

Step 3: upload

Send one multipart request. The manifest goes in as a normal form field; every file goes in as its own part, named after its hash:

import json, os, requests
manifest = json.load(open("manifest.json"))
root = "public"
parts = [
    ("manifest", (None, json.dumps(manifest))),
    ("branch", (None, "main")),
]
for url, digest in manifest.items():
    body = open(os.path.join(root, url.lstrip("/")), "rb").read()
    parts.append((digest, (os.path.basename(url), body, "application/octet-stream")))
r = requests.post(
    f"https://api.cloudflare.com/client/v4/accounts/{account}/pages/projects/{project}/deployments",
    headers={"Authorization": f"Bearer {token}"},
    files=parts,
    timeout=180,
)

The response contains the deployment url and a latest_stage of deploy. A 200 here means the files are stored, not that the site is serving.

The two failure modes

1. The CLI says you are not logged in while the token still works. The Pages endpoint accepted my requests from curl while wrangler pages deploy reported Not logged in. The OAuth token on disk existed and had not expired; the CLI refused to use it in a non-interactive shell. Passing the same token as CLOUDFLARE_API_TOKEN made the CLI deploy immediately. If the CLI and the API disagree, trust the API and check the token's scopes.

2. The deployment succeeds but the domain returns 500. A deployment can exist without being the project's production deployment. GET /accounts/{id}/pages/projects/{name} returns production_deployment: null, and every request to the pages.dev hostname returns an empty 500. The fix is to deploy through a path that promotes the build, or to pin the canonical deployment explicitly:

curl -X PATCH "https://api.cloudflare.com/client/v4/accounts/$CF_ACCOUNT/pages/projects/my-static-site" \
  -H "Authorization: Bearer $CF_TOKEN" \
  -H "Content-Type: application/json" \
  -d '{"canonical_deployment":{"id":"<deployment-id>"}}'

Note that ad-hoc deployments triggered without a branch and a commit hash are treated as previews in some paths. Always send branch explicitly.

Verify what you shipped

Never trust the API's success field alone. Fetch the production URL and compare the byte count with the artifact you uploaded:

curl -s -o /tmp/page.html -w "%{http_code} %{size_download}\n" https://my-static-site.pages.dev/

A 200 with the exact size of the file on disk is the only proof that the pipeline worked end to end.

When this beats the Git integration

Use the API when the site is built outside the repository, when you need to deploy the same artifact to several projects, or when you want a single script that a cron job can run unattended. Keep the Git integration when you want review, history, and preview deployments tied to pull requests.

The full loop is under fifty lines and needs no SDK, which also means it is easy to audit: one request to create, one to upload, one to verify.