Cloudflare API v4 plumbing shared by every ::cloudflare::* module:
authentication, the {success, errors, messages, result} envelope,
readable errors, query strings, and page- and cursor-based pagination.
Use json / request directly for endpoints the curated modules
don't cover.
Per-call credentials. Every API-calling function in this package
takes an optional trailing opts Map, resolved by resolve: opts
first, then context. Nothing from opts is written to ctx, so one run
can act for many accounts and parallel flows don't share credentials.
Credentials in opts are masked in call records. One of these keys
present but blank or null is an error, never a fallback to context.
token: an API token for this callapi-keyandemail: a Global API Key pair for this call. Atokenin the sameoptswins.account-id: the account for account-scoped calls
Context Vars
cloudflare.tokencloudflare.api.keycloudflare.emailcloudflare.account.id
Values
DEFAULT_MAX_ITEMS
DEFAULT_MAX_ITEMS: Int 10000
IDENTITY_KEYS
IDENTITY_KEYS: Vec [
"token",
"api-key",
"email",
"account-id"
]
Functions
account-id
fn (): Str
fn (opts: Map): Str
The account-id in opts, else the cloudflare.account.id context
variable, or an Err carrying ConfigError when it is unset — never an
empty path segment. An account-id in opts that is blank or null is
an Err too.
Example
::cloudflare::api/account-id({account-id: "acc123"}) // "acc123"
account-json
fn (method: Str, suffix: Str): Any
fn (method: Str, suffix: Str, body: Any): Any
fn (method: Str, suffix: Str, body: Any, opts: Map): Any
json against an account-scoped path (suffix follows
/accounts/{id}).
Example
scripts ::cloudflare::api/account-json("GET", "/workers/scripts")
map(scripts, %.id)
// Another account
::cloudflare::api/account-json("GET", "/workers/scripts", "", {account-id: "acc456"})
account-page
fn (suffix: Str): ListPage
fn (suffix: Str, items-key: Str): ListPage
fn (suffix: Str, items-key: Str, opts: Map): ListPage
json-page against an account-scoped path.
Example
page ::cloudflare::api/account-page("/d1/database?per_page=100")
map(page.result, %.name)
buckets ::cloudflare::api/account-page("/r2/buckets", "buckets")
map(buckets.result, %.name)
account-path
fn (suffix: Str): Str
fn (suffix: Str, opts: Map): Str
/accounts/{cloudflare.account.id} plus suffix, or an Err carrying
ConfigError when the account id is unset.
Example
::cloudflare::api/account-path("/workers/scripts", {account-id: "acc123"}) // "/accounts/acc123/workers/scripts"
auth-headers
fn (): Map
fn (token: Str?, api-key: Str?, email: Str?): Map
The authentication headers for a request. An API token
(cloudflare.token, sent as Authorization: Bearer) wins; otherwise
the legacy Global API Key pair (cloudflare.api.key +
cloudflare.email, sent as X-Auth-Key / X-Auth-Email). Returns an
Err carrying ConfigError when neither is configured. The 3-arity
form takes the values explicitly (null for unset).
Example
// From context
headers ::cloudflare::api/auth-headers()
// From explicit values. Pass null for the ones you don't use.
token-headers ::cloudflare::api/auth-headers(::hot::ctx/get("cloudflare.token"), null, null)
blank-credential
fn (opts: Map, names: Vec): Str?
The first of names that opts has but with a blank or null value,
else null. Such a key is an error, never a fallback to context: a tenant
whose key is missing must not run on the project's credentials.
Example
::cloudflare::api/blank-credential({token: ""}, ["token", "account-id"]) // "token"
::cloudflare::api/blank-credential({account-id: "acc123"}, ["token", "account-id"]) // null
build-request
fn (method: Str, path: Str, headers: Map, body: Any, resolved: Map): ::hot::http/HttpRequest
The outgoing HttpRequest from resolved credentials (see resolve).
Pure; exposed for testing.
Example
settings ::cloudflare::api/resolve({})
req ::cloudflare::api/build-request("GET", "/zones", {}, "", settings)
req.url
check-response
fn (response: Result): ::hot::http/HttpResponse
Return the response when it succeeded, or an Err carrying HttpError
for a non-2xx status or a success: false envelope.
Example
ok ::hot::http/HttpResponse({status: 200, body: {success: true}})
::cloudflare::api/check-response(ok).status // 200
is-err(::cloudflare::api/check-response(::hot::http/HttpResponse({status: 200, body: {success: false}}))) // true
collect-cursor
fn (fetch: Fn, params: Map, max: Int): Vec
Walk a cursor-based list (KV keys, R2 buckets): call fetch(params) and
follow result_info.cursor until it is empty or max items are
collected.
Example
fetch (params) {
if(is-some(get(params, "cursor")),
::cloudflare::api/ListPage({result: ["b"], result_info: {cursor: ""}}),
::cloudflare::api/ListPage({result: ["a"], result_info: {cursor: "next"}}))
}
::cloudflare::api/collect-cursor(fetch, {}, 100) // ["a", "b"]
collect-pages
fn (fetch: Fn, params: Map, max: Int): Vec
Walk a page-based list: call fetch(params) (a function returning a
ListPage) with page = 1, 2, … until the last page or max items,
and return the items. Pages are fetched serially.
Example
fetch (params) { ::cloudflare::api/ListPage({result: [params.page], result_info: {total_pages: 3}}) }
::cloudflare::api/collect-pages(fetch, {per_page: 50}, 100) // [1, 2, 3]
::cloudflare::api/collect-pages(fetch, {per_page: 50}, 2) // [1, 2]
error-message
fn (status: Int, body: Any): Str
One readable line from a failed response: Cloudflare's errors joined
with ; (including any error_chain), else the text body, else HTTP <status>.
Example
::cloudflare::api/error-message(403, {errors: [{code: 10000, message: "Authentication error"}]}) // "10000: Authentication error"
::cloudflare::api/error-message(502, "") // "HTTP 502"
has-next-page
fn (info: Map, page: Int, page-items: Int, collected: Int): Bool
Whether a page-based list has another page after page, from its
result_info: total_pages when present, else total_count against
the collected count, else a full page (count/items equal to
per_page). An empty page always ends the walk.
Example
::cloudflare::api/has-next-page({total_pages: 3}, 1, 20, 20) // true
::cloudflare::api/has-next-page({total_pages: 3}, 3, 20, 60) // false
json
fn (method: Str, path: Str): Any
fn (method: Str, path: Str, body: Any): Any
fn (method: Str, path: Str, body: Any, opts: Map): Any
Like request, but return only the envelope's result.
Example
zone ::cloudflare::api/json("GET", "/zones/023e105f4ecef8ad9ca31a8372d0c353")
zone.name
json-page
fn (path: Str): ListPage
fn (path: Str, items-key: Str): ListPage
fn (path: Str, items-key: Str, opts: Map): ListPage
GET a list endpoint and return one ListPage.
Example
page ::cloudflare::api/json-page("/zones?per_page=50")
page.result_info.total_pages
// R2 nests its list under result.buckets
buckets ::cloudflare::api/json-page("/accounts/acc123/r2/buckets", "buckets")
map(buckets.result, %.name)
multipart-body
fn (boundary: Str, parts: Vec): Bytes
A complete multipart/form-data body (Bytes) from a Vec of parts
(see multipart-part), closed with the final boundary. Send it with
Content-Type: multipart/form-data; boundary=<boundary>.
Example
Str(::cloudflare::api/multipart-body("b", [{name: "branch", value: "main"}]))
// "--b\r\nContent-Disposition: form-data; name=\"branch\"\r\n\r\nmain\r\n--b--\r\n"
multipart-content-type
fn (boundary: Str): Str
The Content-Type header value for a multipart-body built with
boundary.
Example
::cloudflare::api/multipart-content-type("b") // "multipart/form-data; boundary=b"
multipart-part
fn (boundary: Str, part: Map): Bytes
One multipart/form-data part as Bytes. part is
{name, value, filename?, content_type?}; value is Str or Bytes
(a Map/Vec is JSON-encoded).
Example
Str(::cloudflare::api/multipart-part("b", {name: "branch", value: "main"}))
// "--b\r\nContent-Disposition: form-data; name=\"branch\"\r\n\r\nmain\r\n"
new-boundary
fn (): Str
A fresh random multipart boundary.
Example
boundary ::cloudflare::api/new-boundary()
::cloudflare::api/multipart-content-type(boundary)
next-cursor
fn (info: Map): Str?
The result_info.cursor of a cursor-based page, or null when it is
absent or empty (the last page).
Example
::cloudflare::api/next-cursor({cursor: "6Ck1la0VxJ0djhidm1MdX2FyD"}) // "6Ck1la0VxJ0djhidm1MdX2FyD"
::cloudflare::api/next-cursor({cursor: ""}) // null
query
fn (params: Map): Str
Build a ?a=1&b=2 suffix from a Map, dropping null values ("" when
nothing is left). Nested maps use Cloudflare's dot form
({name: {contains: "api"}} → name.contains=api) and Vecs repeat
the key, matching the official SDKs.
Example
::cloudflare::api/query({type: "A", name: {contains: "api"}, page: null}) // "?type=A&name.contains=api"
::cloudflare::api/query({}) // ""
request
fn (method: Str, path: Str): Map
fn (method: Str, path: Str, body: Any): Map
fn (method: Str, path: Str, body: Any, opts: Map): Map
Send a JSON request (body a Map/Vec, or "" for none) and return the
whole decoded envelope {success, errors, messages, result, result_info?}.
Example
envelope ::cloudflare::api/request("GET", "/zones?per_page=5")
envelope.result_info.total_count
// A JSON body
::cloudflare::api/request("POST", "/zones/023e105f4ecef8ad9ca31a8372d0c353/purge_cache", {purge_everything: true})
resolve
fn (opts: Map): Map
Credentials and settings for one call (see resolve-from). A token
or api-key from opts is masked in this run's records, and nothing
from opts is written to ctx.
Example
::cloudflare::api/resolve({token: "tenant-b-token", account-id: "acc123"}).account-id // "acc123"
cloudflare.token, cloudflare.api.key, cloudflare.email, cloudflare.account.id
resolve-from
fn (opts: Map, ctx-values: Map): Map
The one resolver, pure: {token, api-key, email, account-id} for a
call from opts first, then ctx-values (a Map of context variables);
what doesn't apply is null. A token wins over an api-key + email
pair, and credentials in opts win over any in context. An Err
carrying ConfigError when there are no credentials, when one of the
identity keys is in opts but blank or null (that never falls back to
context), or when opts has only half of the pair. account-id may
stay null: account-scoped calls check it. Exposed for testing;
resolve feeds it the context.
Example
settings ::cloudflare::api/resolve-from({account-id: "acc123"}, {"cloudflare.token": ::hot::ctx/get("cloudflare.token")})
settings.account-id
seg
fn (value: Any): Str
URL-encode one path segment (KV keys, script, bucket and project names).
Example
::cloudflare::api/seg("session:abc/1") // "session%3Aabc%2F1"
send
fn (method: Str, path: Str, headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse
Send an authenticated request to https://api.cloudflare.com/client/v4
plus path, with extra headers and a raw body ("" for none,
Str or Bytes otherwise). Returns the HttpResponse on success, or an
Err carrying HttpError / ConfigError. The building block for
non-JSON calls (KV values, multipart uploads).
Example
suffix "/accounts/acc123/storage/kv/namespaces/0f2ac74b498b48028cb68387c421e279/values/greeting"
response ::cloudflare::api/send("PUT", suffix, {Content-Type: "text/plain"}, "hello")
response.status
// Act for another account
::cloudflare::api/send("GET", "/user/tokens/verify", {}, "", {token: ::hot::ctx/get("tenant-b.cloudflare.token")})
send-multipart
fn (method: Str, path: Str, parts: Vec): Any
fn (method: Str, path: Str, parts: Vec, opts: Map): Any
Send a multipart request built from parts and return the envelope's
result.
Example
parts [{name: "branch", value: "main"}]
deployment ::cloudflare::api/send-multipart("POST", "/accounts/acc123/pages/projects/marketing-site/deployments", parts)
deployment.id
to-page
fn (envelope: Map, items-key: Str): ListPage
Turn a list envelope into a ListPage; items-key picks a nested list
(R2 nests buckets under result.buckets), "" uses result itself.
Example
::cloudflare::api/to-page({result: {buckets: [{name: "logs"}]}}, "buckets").result // [{name: "logs"}]
::cloudflare::api/to-page({result: [1, 2], result_info: {page: 1}}, "").result_info // {page: 1}
Types
ConfigError
ConfigError type {
message: Str
}
Missing or incomplete configuration (no credentials, no account id). Returned as an Err before any request is sent.
HttpError
HttpError type {
status: Int,
headers: Map,
body: Any,
errors: Vec,
message: Str
}
A failed Cloudflare call: a non-2xx status, or a 2xx envelope with
success: false. errors is Cloudflare's errors array
({code, message, error_chain?}); message joins it into one
readable line such as "10000: Authentication error".
ListPage
ListPage type {
result: Vec,
result_info: Map
}
One page of a list endpoint: result is the page's items and
result_info Cloudflare's paging metadata — {page, per_page, count, total_count, total_pages} for page-based endpoints, {cursor, count}
for cursor-based ones (KV keys, R2 buckets).