SDK reference

Everything exported by @relay/sdk and @relay/sdk/server.

createRelay(options)

Creates a client. Pass an event map type to get typed track() calls.

client
import { createRelay, type PhotoEditEvents } from "@relay/sdk";

const relay = createRelay<PhotoEditEvents>({ publicKey, endpoint, attest });

Options

OptionDefaultDescription
publicKeyrequiredYour public key, pk_dev_… or pk_live_…
endpointrequiredThe Relay API origin sent with your keys.
attestrequiredAsync function returning an attestation token from your backend. Called only after opt-in, or to resume one.
storagelocalStorageWhere the decision is remembered. Never stores an identifier. null keeps it in memory.
flushIntervalMs5000How often queued events are sent.
maxBatchSize20Events per request. The server maximum is 50.
maxQueueSize200Further events are dropped and reported through onError.
maxRetries3Retries for network, 429 and 5xx failures. The batch keeps its idempotency key.
retryBaseMs500Base delay for exponential backoff, capped at 30 seconds.
fetchglobal fetchInject your own for logging or tests.
onStateChangeCalled with the new state whenever it changes.
onErrorCalled with a RelayError for dropped events and failed batches.

Client methods

MethodReturnsDescription
init()Promise<RelayState>Restores an earlier decision. Makes no request unless the user opted in before.
presentConsent({ container? })Promise<choice>Shows the built-in dialog. Resolves to accepted, declined or dismissed.
getConsent()Promise<ConsentPresentation>Fetches consent copy and fields for a custom UI.
accept(presentation)Promise<ConsentReceipt>Records an opt-in from a custom UI.
decline(presentation?)voidRecords a decline. Sends nothing.
startSession()ContributionSession | nullStarts a sequenced session. null unless active.
flush()Promise<void>Sends queued events now.
withdraw()Promise<void>Stops collection, clears the queue and revokes tokens.
requestDeletion()Promise<{ deletionRequestId, notice }>Withdraws, then deletes stored contributions.
getState()RelayStateThe current state.
getReceipt()ConsentReceipt | nullThe active consent receipt, if any.
queuedCount()numberEvents waiting to be sent.
destroy()voidStops timers and listeners.

Sessions

session.track(type, payload) queues an event with the session's sessionId and the next sequence. It returns false and stores nothing if the user is not opted in.

Delivery

Events are batched by size or interval. Each batch carries an idempotency key that retries reuse, so a batch is never stored twice. Network, 429 and 5xx failures retry with exponential backoff and honour Retry-After. An expired token triggers one re-attestation and a retry of the same batch.

createRelayServer(options)

For your backend. Takes secretKey, endpoint and an optional fetch.

MethodDescription
attest({ subject })Mints a single-use attestation for a signed-in user. Returns { attestationToken }.
withdraw({ subject })Withdraws a user on their behalf.
requestDeletion({ subject })Withdraws and deletes a user's contributions.