Authenticated requests against the org's instance_url. Every module in this package goes through json (or send for CSV); use them directly for any REST resource without a wrapper.

Non-2xx responses come back as an Err carrying HttpError, whose message flattens Salesforce's [{message, errorCode, fields}] error array (or an OAuth {error, error_description} body) into one readable line, with error-code set to the first code.

Per-call credentials. Every API-calling function in this package takes an optional trailing opts Map, resolved by resolve: opts first, then the salesforce.* context variables, then the defaults. Per-call credentials are masked in call records and only the minted session is cached, keyed by a hash of them, so one run can act for many orgs. ::salesforce::auth lists the keys; api-version picks the REST API version for the call.

Functions

api-version

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

The REST API version: api-version in opts, then salesforce.api.version ("66.0" and "v66.0" both work), else ::salesforce/DEFAULT_API_VERSION.

Example

::salesforce::api/api-version({api-version: "62.0"})  // "v62.0"
Context Vars: salesforce.api.version

build-headers

fn (token: Str, has-body: Bool, options: Map): Map

Request headers for a session. options: content-type (defaults to JSON when a body is sent), accept (defaults to JSON), headers (extra headers, e.g. Sforce-Query-Options). Exposed for testing.

Example

::salesforce::api/build-headers("TOKEN", false, {})  // {Authorization: "Bearer TOKEN", Accept: "application/json"}
::salesforce::api/build-headers("TOKEN", true, {content-type: "text/csv"}).Content-Type  // "text/csv"

build-request

fn (method: Str, path: Str, body: Any, options: Map, session: Map): ::hot::http/HttpRequest

The outgoing HttpRequest from a resolved session (see resolve). Pure; exposed for testing.

Example

session {token: "TOKEN", instance-url: "https://acme.my.salesforce.com", api-version: "v66.0"}
::salesforce::api/build-request("GET", "/limits", null, {}, session).url
// "https://acme.my.salesforce.com/services/data/v66.0/limits"

build-url

fn (instance-url: Str, path: Str): Str
fn (instance-url: Str, path: Str, version: Str): Str

The absolute URL for a resource on an instance: build-url("https://acme.my.salesforce.com/", "/limits"). Exposed for testing.

Example

::salesforce::api/build-url("https://acme.my.salesforce.com/", "/limits", "v66.0")  // "https://acme.my.salesforce.com/services/data/v66.0/limits"

data-path

fn (path: Str): Str
fn (path: Str, version: Str): Str

Prefix a REST resource with the data API root: /sobjects/Lead → /services/data/v68.0/sobjects/Lead. Paths that already start with /services/ (e.g. a nextRecordsUrl) pass through unchanged. The 2-arity form takes the version explicitly. Exposed for testing.

Example

::salesforce::api/data-path("/sobjects/Lead", "v66.0")  // "/services/data/v66.0/sobjects/Lead"
::salesforce::api/data-path("/services/data/v60.0/limits", "v66.0")  // "/services/data/v60.0/limits"

error-code

fn (body: Any): Any

The first Salesforce error code in an error body (errorCode of the first array entry, or the OAuth error), or null. Exposed for testing.

Example

::salesforce::api/error-code([{errorCode: "INVALID_FIELD", message: "No such column"}])  // "INVALID_FIELD"
::salesforce::api/error-code({error: "invalid_grant"})  // "invalid_grant"

error-message

fn (status: Int, body: Any): Str

Flatten a Salesforce error body into one line, prefixed with the HTTP status: salesforce 400: INVALID_FIELD: No such column 'Foo' (fields: Foo). Handles the REST error array, OAuth {error, error_description} bodies, and plain text. Exposed for testing.

Example

::salesforce::api/error-message(400, [{errorCode: "INVALID_FIELD", message: "No such column 'Foo'", fields: ["Foo"]}])
// "salesforce 400: INVALID_FIELD: No such column 'Foo' (fields: Foo)"

is-invalid-session

fn (response: Any): Bool

True for a 401 response, which Salesforce returns with INVALID_SESSION_ID when a session has expired or been revoked. Exposed for testing.

Example

::salesforce::api/is-invalid-session({status: 401})  // true
::salesforce::api/is-invalid-session({status: 200})  // 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

Make an authenticated JSON request and unwrap the response. path is relative to the data API root (e.g. /sobjects/Lead), or a full /services/... path. Salesforce returns 204 for updates and deletes: those yield null. The 4-arity form takes per-call credentials (see resolve). A session ({token, instance-url}, e.g. from ::salesforce::auth/session() or managed by the caller) works too, and skips the cache and retry.

Example

limits ::salesforce::api/json("GET", "/limits")
limits.DailyApiRequests.Remaining

// For another org
tenant {refresh-token: "5Aep...", client-id: "3MVG..."}
limits ::salesforce::api/json("GET", "/limits", null, tenant)

normalize-version

fn (version: Any): Str

"66.0"/"v66.0"/66 → "v66.0"; null or blank → the default version. Exposed for testing.

Example

::salesforce::api/normalize-version("66.0")  // "v66.0"
::salesforce::api/normalize-version(66)  // "v66.0"

query-string

fn (params: Map): Str

Build ?a=1&b=2 from a Map, dropping null values; Vec values are joined with commas. Returns "" when nothing is left. Exposed for testing.

Example

::salesforce::api/query-string({a: 1, b: null})  // "?a=1"
::salesforce::api/query-string({a: null})  // ""

resolve

fn (opts: Map): Map

The session and settings for one call: {token, instance-url, flow, expires-at, api-version} from opts first, then context, then defaults (see ::salesforce::auth/resolve-config), or an Err. A credential in opts that is blank or null, or instance-url or my-domain-url without one, is an Err. Credentials from opts are masked in call records and never written to ctx.

Example

::salesforce::api/resolve({access-token: "00D...", instance-url: "https://acme.my.salesforce.com"}).api-version
// "v68.0"
Context Vars: salesforce.api.version

segment

fn (value: Any): Str

URL-encode one path segment (a record id, an external id value). Exposed for testing.

Example

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

send

fn (method: Str, path: Str, body: Any, options: Map): ::hot::http/HttpResponse
fn (method: Str, path: Str, body: Any, options: Map, opts: Map): ::hot::http/HttpResponse

Make an authenticated request and return the raw HttpResponse (any status). body is JSON-encoded unless it is already a Str (CSV for Bulk uploads); pass null or "" for none. options: content-type, accept, headers. On a 401 from a minted session, drops the cached session, mints a new one, and retries once. The 5-arity form takes per-call credentials (see resolve), or a session from ::salesforce::auth/session, which is used as is with no retry.

Example

// The raw response, at any status
response ::salesforce::api/send("GET", "/sobjects/Lead/00Q000000000001", null, {})
if(eq(response.status, 404), null, response.body.Email)

// For another org
tenant {access-token: ::hot::ctx/get("tenant.salesforce.token"), instance-url: "https://tenant.my.salesforce.com"}
::salesforce::api/send("GET", "/limits", null, {}, tenant)

unwrap

fn (response: Result): Any

The decoded body of a 2xx response (null for 204), or an Err carrying HttpError. Exposed for testing.

Example

::salesforce::api/unwrap(::hot::http/HttpResponse({status: 200, headers: {}, body: {id: "001"}}))  // {id: "001"}
::salesforce::api/unwrap(::hot::http/HttpResponse({status: 204, headers: {}, body: null}))  // null

Types

HttpError

HttpError type {
    status: Int,
    headers: Map,
    body: Any,
    message: Str,
    error-code: Str?
}

A non-2xx Salesforce response: status, headers, the decoded body, a readable message (see error-message), and the first error-code (e.g. INVALID_FIELD, INVALID_SESSION_ID).