@kentcdodds/cloudflare
Cloudflare helpers: API v4, static site publishing to Pages, analytics, WAF rules, logs, and docs.
OtherTopics: cloudflare, workers, analytics, waf, logs, api, accounts, r2, pages, static-hosting, dns, custom-domains
- Version
- 1.1.0
- License
- MIT
- Published
- November 9, 2026
- Pinned commit
b52e175- Rating
- No ratings yet
- Forks
- 0
- Adaptation effort
- —
README
Agent docs@kentcdodds/cloudflare
Intent
Account-aware Cloudflare helpers for Kody workflows: API v4 (JSON, multipart,
and raw bodies), public static-site publishing to Cloudflare Pages, GraphQL
analytics, Rulesets/WAF, Log Explorer, developer docs, and pointing a custom
domain at a Kody package app. Token aliases
(default, kody, pages) pick the right secret, and each user stores their
own Cloudflare account ids once in this package's settings, so nothing personal
is hardcoded and anyone can fork or use it with their own account.
What it does
- Publish a static site to Cloudflare Pages in one call (
publish-static) - Point a custom domain (like
app.example.com) at a Kody package app in one call, and undo it later (custom-domain,custom-domain-remove) - Call Cloudflare API v4 for zones, Workers, D1, R2, KV, Pages, and related
resources (
api-v4, withform/rawBodywhen JSON is not enough) - Auto-paginate list endpoints (
api-v4-list; Pages projects max out atper_page=10) - Discover which token alias and account id to use (
accounts,verify), and remember your account ids (settings) - Summarize zone HTTP traffic; search Log Explorer; preview or apply WAF rules
- Fetch allowlisted Cloudflare developer documentation
- PUT binary objects into R2 (
r2-put-object)
Publishing a static site
publish-static is the general path for public HTML/CSS/JS hosting. It runs
Cloudflare Pages Direct Upload (the same flow as wrangler pages deploy):
- Resolve the Cloudflare account id (from the alias, or
accountId). - Get or optionally create the Pages project (Direct Upload only, never Git).
- Scan text files for forbidden content (built-in credential patterns + your
scan.forbid) and refuse when something matches. - Fetch a short-lived upload JWT, upload only missing files, and upsert hashes.
- Create a multipart deployment (
manifest+_headers/_redirects). - Wait until the deployment is live (opt out with
wait: false).
Pass the site as a files map (text or base64) and/or a public sourceUrl to a
.zip / .tar / .tar.gz. If you saved a dedicated Pages token, use the
pages account alias for the least privilege.
Custom domains and Cloudflare Access are out of scope for this helper: the
Pages token usually cannot edit DNS. Point DNS at the returned *.pages.dev
hostname yourself (or with a token that has Zone DNS edit). Deployment list /
rollback / prune and project delete are also deferred.
Pointing a custom domain at a Kody app
custom-domain does what used to take several manual steps: register the
hostname with Kody, add a DNS-only CNAME (to domains.kody.run) and whatever
validation records Kody asks for in the right Cloudflare zone, then keep
checking with Kody until the hostname and its certificate are both active.
- It works with any Cloudflare account and any package app. It finds the zone from the hostname (the most specific zone your token can see wins), so you only pass the hostname and the package.
- It refuses hostnames under
kody.codesorkody.run. Kody blocks those on purpose so package app code never shares a site with Kody's own cookies. - It refuses to touch a hostname that already has other DNS records, and changes nothing in that case.
- It handles every way Cloudflare can ask to validate the hostname or its certificate: TXT and CNAME records are created for you, and HTTP validation needs no record at all (the CNAME is enough). Email validation, or anything unexpected, is listed as a clear next step for you while the rest of the setup still finishes.
- It is safe to run again. Records already in place are reused, records it did not create are never changed or deleted, and other hostnames are left alone.
- A dry run shows exactly what it would do, record by record. A hostname that is already set up reports "already configured" and changes nothing.
- Cloudflare usually takes a few minutes. If the call returns
pending, Kody keeps checking on its own; run it again later to see the final status. - It must be run by you (an org admin on a paid Kody plan) from an interactive Kody session, not from a scheduled job or webhook.
The result tells you the hostname and certificate status, every record it created or reused, and any next steps.
custom-domain-remove undoes it: it deletes only the DNS records the helper
created plus the Kody registration, and only when you pass confirm: true.
Prerequisites / setup
Up to three saved Cloudflare API token secrets (user scope), one per account
alias. Only default is required; the other two are optional.
| Account | Secret | Use when |
|---|---|---|
default | cloudflareApiToken | Routine API, DNS, analytics, logs, and WAF on the accounts your primary token covers |
kody | cloudflareApiTokenKodyAccount | A second account the default token cannot reach (usually an account-scoped token) |
pages | cloudflarePagesApiToken | A least-privilege token for Cloudflare Pages Direct Upload and Pages projects |
- Save each token you need in Kody (never paste values in chat).
- Approve host
api.cloudflare.comfor each secret you use. - Grant firewall/ruleset edit permission only if you will call
rulesetswithapply: true. - For
custom-domain, the token needs Zone: Read and DNS: Edit on the zone that owns the hostname. Create one in the Cloudflare dashboard under API Tokens (the "Edit zone DNS" template works), scoped to that zone or to all zones you want to use. - Agents: see
AGENTS.mdfor import paths, dry-run snippets, and account-id discovery notes.
Authenticated exports accept optional account (default 'default'). For a
token outside the table, pass apiTokenSecret with the Kody secret name
(never a raw token). Do not pass both account and apiTokenSecret.
Your Cloudflare account ids
Helpers that work on one account (Pages publishing, R2 uploads, account-level logs, account token checks) need a Cloudflare account id. They pick it in this order:
- An
accountIdyou pass on the call. - The id you stored for that token alias with the
settingsexport. This is saved per user in the package's storage, so it survives updates and no republish is needed. - The only account the token can see (
GET /accounts), when there is exactly one.
If none of those works, the call stops with a message that says how to store
the id. To find your id, open the Cloudflare dashboard: it is the long hex part
of the URL (dash.cloudflare.com/<accountId>/...). verify({ account }) also
lists the accounts a token can see. Pages-only tokens usually cannot list
accounts, so store an id for pages if you use one.
Verifying tokens
Cloudflare has two verify endpoints. /user/tokens/verify alone often returns
401 Invalid API Token for a valid account-scoped token. Prefer this
package's verify export (tries user, then account when an account id is
known). After verify succeeds: 403 = lacks permission; 400/404 on a
mutating call against a bad/missing target = authorized for that operation.
Done when
- Needed token secrets are saved and
api.cloudflare.comis approved verifyfor the intended account returnsok: true(and anaccountIdwhen you need one)publish-staticwithdryRun: trueshows the plan; a real run returnslive: trueand a*.pages.devURL- Rulesets stay dry-run until an explicit
apply: trueafter review r2-put-objectwithconfirm: truereturnsok: truefor a test objectcustom-domainwithdryRun: trueon an existing hostname reportsalready_configured; a new hostname endsactive(after a re-run if needed) with no next steps left
R2 upload limits
The R2 single-object helper accepts source URLs up to 8 MiB and requires a strong source ETag or expected SHA-256. Inline base64 remains limited to small payloads. It does not implement large-object multipart recovery; larger files are explicitly refused. Uncertain destination completion must be checked before retrying. No extra credentials or permissions are required.
Report this listing
Log in to report this listing.