Low-level Zendesk transport: subdomain normalising, API token
(Basic) or OAuth (Bearer) auth, HttpError with a parsed
retry-after for 429s, and cursor pagination (page[size],
page[after], meta.has_more, meta.after_cursor).
Per-call credentials. Every API-calling function in this package
takes an optional trailing opts Map, resolved by resolve: opts
first, then context variables. Nothing from opts is written to ctx,
so one run can act for many accounts and parallel flows don't share
credentials. Credentials from opts are masked in call records.
emailandapi-token: API token auth for this call (both, in the sameopts)oauth-token: an OAuth access token for this call. It wins overemailandapi-token.subdomain: another account's subdomain. It needs credentials in the sameopts: context credentials are never sent to a per-call account.
With credentials in opts, context credentials are ignored for that
call. A credential key present but blank or null is an error, never a
fallback to context.
Context Vars
zendesk.subdomainzendesk.emailzendesk.api.tokenzendesk.oauth.token
Values
CREDENTIAL_KEYS
CREDENTIAL_KEYS: Vec [
"email",
"api-token",
"oauth-token"
]
Functions
auth-header
fn (email: Str, token: Str): Str
Zendesk API token auth: Basic base64("{email}/token:{api-token}").
Exposed for testing.
Example
::zendesk::api/auth-header("ada@example.com", "TOKEN") // "Basic YWRhQGV4YW1wbGUuY29tL3Rva2VuOlRPS0VO"
base-url
fn (subdomain: Str): Str
https://{subdomain}.zendesk.com/api/v2 for a handle or full
*.zendesk.com host (see normalize-subdomain).
Example
::zendesk::api/base-url("acme.zendesk.com") // "https://acme.zendesk.com/api/v2"
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
::zendesk::api/blank-credential({email: "ada@example.com", api-token: ""}, ["email", "api-token"]) // "api-token"
::zendesk::api/blank-credential({}, ["email", "api-token"]) // null
build-headers
fn (authorization: Str, has-body: Bool): Map
Request headers: Authorization, Accept: application/json, and
Content-Type only when there is a body. Exposed for testing.
Example
::zendesk::api/build-headers("Bearer TOKEN", false) // {Authorization: "Bearer TOKEN", Accept: "application/json"}
collect
fn (path: Str, key: Str, options: Map, limit: Int): Vec
fn (path: Str, key: Str, options: Map, limit: Int, opts: Map): Vec
Collect up to limit items from a cursor-paginated list: key is
the resource key in each page ("tickets", "users", ...).
options takes the same filters as list-page.
Example
groups ::zendesk::api/collect("/groups", "groups", {}, 500)
collect-pages
fn (fetch: Fn, key: Str, cursor: Str?, limit: Int, acc: Vec): Vec
Follow cursors, concatenating each page's key Vec, until limit
items or meta.has_more is false. fetch is a Fn taking the cursor
(null for the first page) and returning one page. Exposed for
testing; collect uses it. A failed page comes back as its Err.
Example
fetch (cursor) {
if(is-null(cursor),
{items: [1, 2], meta: {has_more: true, after_cursor: "c2"}},
{items: [3], meta: {has_more: false}})
}
::zendesk::api/collect-pages(fetch, "items", null, 10, []) // [1, 2, 3]
compact
fn (body: Map): Map
Drop null-valued keys so unset optionals are omitted rather than sent as JSON null.
Example
::zendesk::api/compact({name: "Acme", details: null}) // {name: "Acme"}
cursor-query
fn (options: Map): Str
Build a cursor-paginated query string. options takes page_size
(sent as page[size], default 100) and after (sent as
page[after], the previous page's meta.after_cursor); every other
key (sort, include, filters) passes through. Exposed for testing.
Example
::zendesk::api/cursor-query({page_size: 50, sort: "-updated_at"}) // "?sort=-updated_at&page%5Bsize%5D=50"
list-page
fn (path: Str, options: Map): Any
fn (path: Str, options: Map, opts: Map): Any
GET one cursor page of path (relative to /api/v2, without a
query string). Returns the body: the item Vec under its resource key
plus meta: {has_more, after_cursor, before_cursor} and links.
Read the next cursor with next-after.
Example
page ::zendesk::api/list-page("/groups", {page_size: 50})
more ::zendesk::api/list-page("/groups", {page_size: 50, after: ::zendesk::api/next-after(page)})
next-after
fn (page: Any): Str?
The page[after] cursor for the page after page: meta.after_cursor
when meta.has_more is true, otherwise null.
Example
::zendesk::api/next-after({meta: {has_more: true, after_cursor: "abc"}}) // "abc"
::zendesk::api/next-after({meta: {has_more: false, after_cursor: "abc"}}) // null
normalize-subdomain
fn (subdomain: Str): Str
Reduce acme, acme.zendesk.com, https://acme.zendesk.com/ or
https://acme.zendesk.com/agent to the bare handle acme
(lowercased). Anything that isn't a plain Zendesk handle afterwards
(dots, slashes, @, empty) is an Err, so a bad value can't point
requests and credentials at another host. Exposed for testing.
Example
::zendesk::api/normalize-subdomain("https://Acme.zendesk.com/agent") // "acme"
path-segment
fn (value: Any): Str
Percent-encode one path segment (an id). Ints pass through as text.
Example
::zendesk::api/path-segment(35436) // "35436"
::zendesk::api/path-segment("a b") // "a%20b"
query-string
fn (options: Map): Str
Build a ?a=1 suffix from a Map, dropping null values; "" when
nothing remains. Vec values are comma-joined (Zendesk's ids=1,2,3
form).
Example
::zendesk::api/query-string({ids: [1, 2, 3], page: null}) // "?ids=1%2C2%2C3"
::zendesk::api/query-string({}) // ""
request
fn (method: Str, path: Str, body: Any): Any
fn (method: Str, path: Str, body: Any, opts: Map): Any
fn (method: Str, path: Str, body: Any, subdomain: Str, email: Str, api-token: Str): Any
Make an authenticated Zendesk Support request. path is relative to
/api/v2 (e.g. /groups). Returns the decoded body on 2xx (null when
empty), or an Err carrying HttpError. The 4-arity form takes per-call
opts (see resolve); the 6-arity form takes an explicit subdomain,
email and API token.
Example
groups ::zendesk::api/request("GET", "/groups", "")
tenant ::zendesk::api/request("GET", "/groups", "", {subdomain: "tenant-b", oauth-token: "tenant-b-token"})
zendesk.subdomain
request-with-auth
fn (method: Str, path: Str, body: Any, subdomain: Str, authorization: Str): Any
Make a Zendesk request with an explicit subdomain and Authorization
header value, which is masked in call records. path is relative to
/api/v2; body is a Map, or "" for none. Returns the decoded body
on 2xx (null when empty), or an Err carrying HttpError.
Example
token ::hot::ctx/get("zendesk.oauth.token")
body ::zendesk::api/request-with-auth("GET", "/groups", "", "acme", `Bearer ${token}`)
body.groups
resolve
fn (opts: Map): Map
The base URL and Authorization header for one call (see
resolve-from). An api-token or oauth-token from opts is masked
in call records with ::hot::ctx/mark-secret, and opts values are
never written to ctx.
Example
::zendesk::api/resolve({subdomain: "tenant-b", oauth-token: "tenant-b-token"}).base-url // "https://tenant-b.zendesk.com/api/v2"
zendesk.subdomain, zendesk.email, zendesk.api.token, zendesk.oauth.token
resolve-auth
fn (oauth-token: Str?, email: Str?, api-token: Str?): Str
Pick the Authorization header: an OAuth access token gives Bearer {token} and wins when set; otherwise email plus API token gives
Basic token auth. Returns an Err naming the missing context
variables when neither is complete. Exposed for testing.
Example
::zendesk::api/resolve-auth("TOKEN", null, null) // "Bearer TOKEN"
resolve-from
fn (opts: Map, ctx-values: Map): Map
The one resolver, pure: {base-url, authorization} for a call from
opts first, then ctx-values (a Map of context variables). An Err
when a credential key in opts is blank or null (that never falls back
to context), when opts has only half of email and api-token, when
opts has subdomain without credentials, or when no subdomain or
credentials are set. Exposed for testing; resolve feeds it the context.
Example
ctx-values {"zendesk.subdomain": "acme", "zendesk.oauth.token": "TOKEN"}
::zendesk::api/resolve-from({}, ctx-values) // {base-url: "https://acme.zendesk.com/api/v2", authorization: "Bearer TOKEN"}
response-body
fn (body: Any): Any
Normalize a decoded 2xx body: empty text (204 No Content, e.g. deletes) becomes null; anything else passes through. Exposed for testing.
Example
::zendesk::api/response-body("") // null
::zendesk::api/response-body({ticket: {id: 1}}) // {ticket: {id: 1}}
retry-after
fn (headers: Map): Int?
Parse the Retry-After header (seconds) from response headers, or null
when absent or not a number. Exposed for testing.
Example
::zendesk::api/retry-after({"retry-after": "30"}) // 30
::zendesk::api/retry-after({}) // null
Types
HttpError
HttpError type {
status: Int,
headers: Map,
body: Any,
retry-after: Int?
}
A non-2xx Zendesk response. retry-after is the parsed Retry-After header in seconds (set on 429 rate limits and some 503s), or null.