Low-level Cal.com v2 transport: auth and version headers, response unwrapping, and HttpError for non-2xx responses. Endpoint namespaces build on request and request-envelope.

Per-call credentials. Every API-calling function in this package takes an optional trailing opts Map, resolved by resolve: opts first, then context variables, then defaults. Nothing from opts is written to ctx, so one run can act for many accounts and parallel flows don't share credentials. An api-key from opts is masked in call records. An api-key present but blank or null is an error, never a fallback to cal-com.api.key.

  • api-key: the API key or managed user's access token for this call
  • base-url: another API root, such as a self-hosted instance. It needs an api-key in the same opts: cal-com.api.key is never sent to a per-call URL.

Context Vars

Optional
  • cal-com.api.key
  • cal-com.url

Functions

base-url

fn (): Str

The API base URL: cal-com.url when set (self-hosted, trailing slash removed), otherwise https://api.cal.com/v2.

Example

url ::cal-com::api/base-url()
`${url}/me`
Context Vars: cal-com.url

build-headers

fn (api-key: Str, api-version: Str, has-body: Bool): Map

Request headers: Bearer auth, the family's cal-api-version (omitted when api-version is empty, for unversioned families such as webhooks), and Content-Type only when there is a body. Exposed for testing.

Example

::cal-com::api/build-headers("key-a", "2024-08-13", false)  // {Authorization: "Bearer key-a", cal-api-version: "2024-08-13"}
::cal-com::api/build-headers("key-a", "", true)  // {Authorization: "Bearer key-a", Content-Type: "application/json"}

compact

fn (body: Map): Map

Drop null-valued keys from a request body Map so unset optionals are omitted rather than sent as JSON null.

Example

::cal-com::api/compact({title: "Intro call", description: null})  // {title: "Intro call"}

normalize-base-url

fn (override: Str?): Str

Pick the base URL from an optional override: null or blank gives the cloud default; trailing /s are dropped. Exposed for testing.

Example

::cal-com::api/normalize-base-url("https://cal.example.com/api/v2/")  // "https://cal.example.com/api/v2"
::cal-com::api/normalize-base-url(null)  // "https://api.cal.com/v2"

path-segment

fn (value: Any): Str

Percent-encode one path segment (a booking uid, an id). Ints pass through as text.

Example

::cal-com::api/path-segment("a b")  // "a%20b"
::cal-com::api/path-segment(123)  // "123"

query-string

fn (options: Map): Str

Build a ?a=1&b=2 suffix from an options Map: null values are dropped, Vec values are joined with commas (Cal.com's multi-value form, e.g. status: ["upcoming", "unconfirmed"]), and an empty result yields "". Exposed for testing.

Example

::cal-com::api/query-string({take: 10, afterStart: null})  // "?take=10"
::cal-com::api/query-string({})  // ""

request

fn (method: Str, path: Str, body: Any, api-version: Str): Any
fn (method: Str, path: Str, body: Any, api-version: Str, api-key: Str): Any
fn (method: Str, path: Str, body: Any, api-version: Str, opts: Map): Any

Make an authenticated Cal.com v2 request with an explicit per-family cal-api-version header (pass "" for unversioned families). body is a Map, or "" for none. Returns the response's data (null for an empty 2xx body), or an Err carrying HttpError. The escape hatch for endpoints not wrapped by this package. The 5th argument is per-call opts (see resolve) or an API key.

Example

calendars ::cal-com::api/request("GET", "/calendars", "", "")
tenant ::cal-com::api/request("GET", "/calendars", "", "", {api-key: "cal_live_tenant_b"})
Context Vars: cal-com.api.key

request-envelope

fn (method: Str, path: Str, body: Any, api-version: Str): Any
fn (method: Str, path: Str, body: Any, api-version: Str, api-key: Str): Any
fn (method: Str, path: Str, body: Any, api-version: Str, opts: Map): Any

Like request, but returns the whole decoded body ({status, data, pagination?}) instead of just data. Used by paged lists.

Example

page ::cal-com::api/request-envelope("GET", "/bookings?take=10", "", ::cal-com/VERSION_BOOKINGS)
page.data
page.pagination.hasNextPage
Context Vars: cal-com.api.key

resolve

fn (opts: Map): Map

Credentials and settings for one call (see resolve-from). An api-key from opts is masked in call records with ::hot::ctx/mark-secret, and opts values are never written to ctx.

Example

::cal-com::api/resolve({api-key: "cal_live_tenant_b"}).base-url  // "https://api.cal.com/v2"
Context Vars: cal-com.api.key, cal-com.url

resolve-from

fn (opts: Map, ctx-values: Map): Map

The one resolver, pure: {api-key, base-url} for a call from opts first, then ctx-values (a Map of context variables), then defaults. An Err when no API key is set, when api-key is in opts but blank or null (that never falls back to context), or when opts has base-url without api-key. Exposed for testing; resolve feeds it the context.

Example

::cal-com::api/resolve-from({api-key: "key-a"}, {"cal-com.api.key": "key-b"})  // {api-key: "key-a", base-url: "https://api.cal.com/v2"}
::cal-com::api/resolve-from({}, {"cal-com.api.key": "key-b", "cal-com.url": "https://cal.example.com/api/v2/"}).base-url  // "https://cal.example.com/api/v2"
is-err(::cal-com::api/resolve-from({api-key: ""}, {"cal-com.api.key": "key-b"}))  // true

response-data

fn (body: Any): Any

Unwrap a decoded Cal.com response body. Cal.com wraps payloads as {status, data}; this returns data when present, the body itself for other Maps, and null for an empty body (204, or a 2xx with no content). Exposed for testing.

Example

::cal-com::api/response-data({status: "success", data: {id: 1}})  // {id: 1}
::cal-com::api/response-data({id: 1})  // {id: 1}
::cal-com::api/response-data("")  // null

response-envelope

fn (body: Any): Any

Like response-data, but keeps the whole envelope ({status, data, pagination}) so callers can page. Empty bodies become null. Exposed for testing.

Example

::cal-com::api/response-envelope({status: "success", data: [], pagination: {hasNextPage: false}}).pagination.hasNextPage  // false
::cal-com::api/response-envelope("")  // null

send

fn (method: Str, path: Str, body: Any, api-version: Str, opts: Map, unwrap: Fn): Any

Send a request for one call: resolve opts, then return unwrap of a 2xx body, or an Err carrying HttpError.

Example

// Prefer `request` or `request-envelope`. This takes the unwrap step directly.
profile ::cal-com::api/send("GET", "/me", "", "", {}, ::cal-com::api/response-data)
profile.username

Types

HttpError

HttpError type {
    status: Int,
    headers: Map,
    body: Any
}