Low-level HubSpot client: authenticated requests, response unwrapping, date-versioned CRM path building, query strings, and cursor pagination over paging.next.after.

Per-call credentials. Every API-calling function in this package takes an optional trailing opts Map, resolved by resolve: opts first, then the hubspot.* context variables, then the defaults. A per-call token is masked in call records and never written to ctx, so one run can act for many portals.

  • token: the private app or OAuth access token for this call
  • api-version: the date-based API version for this call

A token that is present but blank or null is an Err naming the key, never a fallback to context.

Context Vars

Optional
  • hubspot.token
  • hubspot.api.version

Values

DEFAULT_API_VERSION

DEFAULT_API_VERSION: Str "2026-09"

Functions

api-version

fn (): Str
fn (opts: Map): Str

The date-based API version used in CRM paths: api-version in opts, then the hubspot.api.version context variable (e.g. "2026-03"), otherwise DEFAULT_API_VERSION.

Example

::hubspot::api/api-version()  // "2026-09"
::hubspot::api/api-version({api-version: "2026-03"})  // "2026-03"
Context Vars: hubspot.api.version

build-request

fn (method: Str, path: Str, additional-headers: Map, body: Any, token: Str): ::hot::http/HttpRequest

The outgoing HttpRequest for a token. A "" body sends no body and no Content-Type; additional-headers win over the defaults. Pure; exposed for testing.

Example

req ::hubspot::api/build-request("POST", "/crm/objects/2026-09/contacts", {}, {properties: {email: "ada@example.com"}}, "pat-example")
req.url  // "https://api.hubapi.com/crm/objects/2026-09/contacts"
req.headers  // {Authorization: "Bearer pat-example", Content-Type: "application/json"}

crm-path

fn (api: Str, rest: Str): Str
fn (api: Str, rest: Str, opts: Map): Str

A date-versioned CRM path: crm-path("objects", "/contacts/123") gives /crm/objects/2026-09/contacts/123. api is the API family: objects, associations, pipelines, properties, owners, or lists. opts can carry the call's api-version.

Example

::hubspot::api/crm-path("objects", "/contacts/123")  // "/crm/objects/2026-09/contacts/123"
::hubspot::api/crm-path("lists", "", {api-version: "2026-03"})  // "/crm/lists/2026-03"

error-message

fn (error: Any): Str?

The message from a failed call's error value: HubSpot's body.message when present, otherwise the stringified body.

Example

e ::hubspot::api/HttpError(::hot::http/HttpResponse({status: 409, headers: {}, body: {message: "Contact already exists"}}))
::hubspot::api/error-message(e)  // "Contact already exists"

handle-response

fn (response: Result): Any

Unwrap a response: the decoded body on 2xx (null for an empty 204), an Err carrying HttpError otherwise. Exposed for testing.

Example

::hubspot::api/handle-response(::hot::http/HttpResponse({status: 200, headers: {}, body: {id: "1"}}))  // {id: "1"}
::hubspot::api/handle-response(::hot::http/HttpResponse({status: 204, headers: {}, body: ""}))  // null
is-err(::hubspot::api/handle-response(::hot::http/HttpResponse({status: 409, headers: {}, body: {message: "Conflict"}})))  // true

json

fn (method: Str, path: Str): Any
fn (method: Str, path: Str, opts: Map): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, token: Str): Any
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): Any

Like request, but unwraps: the decoded JSON body on 2xx, or an Err carrying HttpError (HubSpot's category/message in body). Transport failures pass through as Errs.

Example

contact ::hubspot::api/json("GET", ::hubspot::api/crm-path("objects", "/contacts/12345"))
contact.properties.email

created ::hubspot::api/json("POST", ::hubspot::api/crm-path("objects", "/contacts"), {}, {properties: {email: "ada@example.com"}})
created.id

mark-credentials

fn (opts: Map): Any

Mask the token in opts in this run's call records. Called first wherever opts arrives, so the records of the calls after it are masked too. Returns null.

Example

opts {token: ::hot::ctx/get("tenant.hubspot.token")}
::hubspot::api/mark-credentials(opts)

next-after

fn (page: Any): Str?

The cursor for the next page (paging.next.after) of a HubSpot list or search response, or null on the last page.

Example

::hubspot::api/next-after({results: [], paging: {next: {after: "100"}}})  // "100"
::hubspot::api/next-after({results: []})  // null

paginate

fn (fetch: Fn): Vec
fn (fetch: Fn, max-pages: Int): Vec

Follow paging.next.after cursors and concatenate every page's results. fetch receives the cursor (null for the first page) and returns one page. Stops after max-pages pages (default 10) so a runaway listing cannot loop forever; returns the first Err.

Example

::objects ::hubspot::objects

contacts ::hubspot::api/paginate((after) {
  ::objects/list-objects("contacts", {limit: 100, after: after, properties: ["email"]})
}, 5)
length(contacts)

query-string

fn (params: Map): Str

Build a query string from a Map, omitting nulls; Vec values become comma-separated (HubSpot's properties=email,firstname style).

Example

::hubspot::api/query-string({limit: 10, after: null})  // "?limit=10"

request

fn (method: Str, path: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, opts: Map): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, token: Str): ::hot::http/HttpResponse
fn (method: Str, path: Str, additional-headers: Map, body: Any, opts: Map): ::hot::http/HttpResponse

Make an authenticated HubSpot request and return the raw HttpResponse. path is relative to api.hubapi.com (e.g. /crm/objects/2026-09/contacts). A "" body sends no body; anything else is sent as JSON. Build the path with crm-path and the same opts so a per-call api-version reaches it.

Example

response ::hubspot::api/request("GET", ::hubspot::api/crm-path("objects", "/contacts/12345"))
response.status

// Another portal's token for this call
opts {token: ::hot::ctx/get("tenant.hubspot.token")}
::hubspot::api/request("POST", ::hubspot::api/crm-path("objects", "/contacts", opts), {}, {properties: {email: "ada@example.com"}}, opts)

resolve

fn (opts: Map): Map

Credentials and settings for one call (see resolve-from). The token is masked in this run's call records, and nothing from opts is written to ctx.

Example

::hubspot::api/resolve({token: "pat-tenant", api-version: "2026-03"}).api-version  // "2026-03"
Context Vars: hubspot.token, hubspot.api.version

resolve-from

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

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

Example

::hubspot::api/resolve-from({api-version: "2026-03"}, {"hubspot.token": "pat-example"}).api-version  // "2026-03"
is-err(::hubspot::api/resolve-from({}, {}))  // true

segment

fn (value: Any): Str

Percent-encode one path segment (an id, object type, or name). Exposed for testing.

Example

::hubspot::api/segment("a b/c")  // "a%20b%2Fc"

Types

HttpError

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

A non-2xx HubSpot response. HubSpot's error body carries status, message, category, correlationId, and sometimes errors/context.