The official TypeScript SDK, @totemstudioai/sdk, provides typed access to the stable Totem API v1 from your server. It runs on Node.js 22.12 or later. A team administrator creates its key in Settings → Totem API. Public API access is included from Pro.
Install the stable SDK
The SDK covers CRM context and custom values, tasks and notifications, messages and content reads, calls, bookings and payment links, Library and knowledge, published assistant runs, operations and events. Methods and permissions follow the public API.
Install the current stable version from npm on your server:
npm install @totemstudioai/sdkTo pin a version, specify its number, for example npm install @totemstudioai/sdk@1.1.0. The official GitHub releases provide versioned archives and checksums.
Earlier published CRM release
SDK 0.3.0-beta.1 is published on npm, including the v1.0b CRM methods. Pin that release on your server:
npm install @totemstudioai/sdk@0.3.0-beta.1The npm beta tag retains this earlier CRM release. The latest tag selects the stable SDK, which includes these CRM methods and the expanded v1 contract.
The same verified package remains available in the official GitHub prerelease, with its package archive and SHA-256 checksum. The npm download matches that reviewed archive byte for byte.
The official repository contains the source and package build/verification instructions. An older SDK can still perform its supported reads; it does not gain the new write methods merely because the server was updated.
Make a typed request
The examples on this page are ES modules and use await at the top level. In a CommonJS project, put them inside an async function.
import { TotemClient } from '@totemstudioai/sdk'
const apiKey = process.env.TOTEM_API_KEY
if (!apiKey) throw new Error('Set TOTEM_API_KEY on your server')
const client = new TotemClient({ apiKey })
const integration = await client.me.get()
const teams = await client.teams.list()
console.log(integration.id, teams.data.map(team => team.id))For a key that reads several teams, select one with the client's team option. You can also pass team per request on the routes that read one team's collection:
const team = process.env.TOTEM_TEAM_ID
if (!team) throw new Error('Set TOTEM_TEAM_ID on your server')
const client = new TotemClient({ apiKey, team })
const page = await client.leads.list({ limit: 100 })
const matching = await client.leads.search({ email: 'alex.rivera@example.com' })
console.log(page.data.length, matching.data.length)A route that reads one record by its id, such as client.leads.get(id), takes the team from the id and has no team option: compare the record's team with the one you expect. The client supplies Authorization and Totem-Team, encodes filters and returns typed response objects. Field names stay the same as the API's JSON. Fields with a fixed set of values are typed so that a value added later still compiles, because enums can grow.
Read every page with the SDK
Use items to read a list without managing its page cursors:
for await (const lead of client.items('leads.list', { query: { limit: 100 } })) {
console.log(lead.id, lead.version)
}Use pages when you need the whole page envelope. It stops once has_more is false; it is not a scheduler. timeoutMs applies to each page; pass signal: AbortSignal.timeout(ms) to bound a whole traversal.
Read the changes feed with pages or client.sync.changes({ cursor, limit: 100 }), never with items: apply each page before saving its next_cursor, including on an empty page with has_more: false. A cursor that is undefined or null starts from the beginning. Keep one cursor per team and resume polling every 1 to 5 minutes. Use isFeedRemoval(item) to tell a removal from a record's current state, and apply an item only when its version is greater than the one you hold. Keep a copy in sync explains removals, versions and full resync; the SDK does not store your data or cursors for you.
Write CRM data with explicit permissions
The SDK exposes leads.upsert, update, archive, addNote, notes, updateNote and deleteNote. Use the ETag from the response header for conditional writes:
const result = await client.requestWithResponse('leads.upsert', {
body: { origin: 'import', email: 'alex.rivera@example.com', external_id: 'crm-contact-001' },
idempotencyKey: 'import-example-contact-001',
})
if (result.data.object === 'lead_mutation') {
const ifMatch = result.headers.get('ETag')
if (!ifMatch) throw new Error('Missing lead ETag')
await client.leads.update(result.data.lead.id, { name: 'Alex' }, {
ifMatch, idempotencyKey: 'edit-example-contact-001',
})
}The client preserves a generated key across retries within one call. Supply and persist idempotencyKey to resume a logical write across calls/process restarts. Keep the original ifMatch on an uncertain retry; use a new validator/key only after reviewing a conflict for a new logical change. Note reads require explicit content access; note write receipts omit bodies. See CRM writes and notes.
Read notifications with the SDK
The SDK provides methods to list and read business notifications:
const newAlerts = await client.notifications.list({ status: 'new', limit: 100 })
if (newAlerts.data[0]) {
const notification = await client.notifications.get(newAlerts.data[0].id)
console.log(notification.id, notification.lead, notification.status)
}
for await (const notification of client.items('notifications.list', { query: { limit: 100 } })) {
console.log(notification.id, notification.version)
}Use lead or status as a list filter, never both. The same SDK build types kind: 'notification' in the changes feed. Use that feed for ongoing CRM alerts, because someone can read an alert before a status: 'new' lookup sees it. Store your own processing state and deduplicate by notification id. A summary can contain personal data; keep it out of logs. Notifications explains the fields and removals.
SDK errors and response metadata
import { TotemApiError, TotemError } from '@totemstudioai/sdk'
try {
const response = await client.requestWithResponse('me.get')
console.log(response.requestId, response.data.id)
} catch (error) {
if (error instanceof TotemApiError) {
console.error(error.status, error.code, error.action, error.param, error.requestId)
} else if (error instanceof TotemError) {
console.error(error.name)
}
throw error
}Every error the SDK raises for a request it sent extends TotemError:
TotemApiErrorfor an error the API answered. It exposes the HTTP status, the API errorcode, its recommendedaction, theparamat fault,title, the request id andretryAfterMswhen supplied.TotemProtocolErrorfor a response the contract does not allow, such as a successful answer whose body is not JSON, a list page withoutdata, or a missing or repeated cursor duringpages()oritems().- In the candidate,
TotemUnknownOutcomeErrorextendsTotemProtocolErrorwhen a durable write returns a malformed or unrelated operation receipt. It retains the original idempotency key and request id, carries no response body, and is not retried automatically. Reconcile the same action before submitting another. TotemConnectionErrorfor a network failure that retries could not recover, andTotemTimeoutErrorfor the request's deadline.
Invalid options or arguments, such as an empty apiKey or team, throw a plain TypeError before any request is sent, and your own signal's abort reason is rethrown unchanged.
Branch on action, the SDK's grouping of error codes: fix_request, check_credentials, resync, wait_capture, drop_team, keep_and_retry, retry_after, backoff or contact_support. The exported errorCatalogue maps every documented code to its action, and the error catalogue explains each code; handle an unknown code by its HTTP status. On the changes feed, action === 'resync' (resync_required or sync_cursor_expired) means a full resync; capture_not_ready means keep your data and cursor and try again later; team_not_accessible means read GET /v1/teams again and drop the team only if it is no longer listed. Without the SDK, branch on the response's code. Keep the request id for support.
The client makes bounded, jittered retries for temporary failures and respects Retry-After. A wait beyond maxRetryDelayMs, or beyond the time left before the request's deadline, is returned to your code at once, rather than shortened. Use timeoutMs, maxRetries, maxRetryDelayMs and signal to fit your job's deadline. Authentication, authorization and invalid-request errors need a correction, not a retry loop.
Verify your SDK integration
Inject a fetch implementation through new TotemClient({ apiKey, fetch }) for deterministic HTTP tests. Test your receiver's version handling, removals and cursor persistence separately, then run real requests with your own key. Testing and go-live lists the behavior to verify; a mocked response does not prove a live key or server works.