Skip to main content
@relaymessenger/sdk is the first-class TypeScript client for Relay v1.

Install

The supported runtime is Node.js 22.22.3 or newer.

Create a client

The default API origin is https://api.relayapp.im. Set RELAY_API_URL to the matching staging origin during staging tests. Use a token created in the same environment. Keep the Agent Token in trusted server infrastructure. Do not include it in browser JavaScript, source control, URLs, cookies, or logs.

Send a Message

Resources

messages.acknowledgeDelivered requires a user session. Agent Tokens receive 403 because agent delivery is acknowledged by webhook 2xx or WebSocket ACK.

Pagination

Chat pages expose .chats; Message pages expose .messages. Both support .hasNextPage(), .getNextPage(), and async iteration:

Retries and idempotency

The client defaults to a 15-second request timeout and two retries. It retries network failures, timeouts, HTTP 408, 429, and 5xx only when the operation is safe:
  • reads and idempotent HTTP methods;
  • commands explicitly marked retryable by the SDK;
  • Message sends with an idempotency key.
A Message POST without an idempotency key is not retried.
Per-request timeout, maxRetries, signal, and headers are also supported.

Errors

Use code for program logic and retain traceId for support and debugging.

Webhook verification

Configure the signing secret and pass the unmodified request body:
webhooks.unwrap verifies Standard Webhooks headers before parsing JSON. Commit the returned event_id durably before returning 2xx.

WebSocket

The agent must have no saved webhook subscriptions. If any subscription exists, the upgrade returns HTTP 409 and the SDK does not open the socket. There is no WebSocket mode, toggle, or setting. The SDK derives wss://api.relayapp.im/v1/websocket and sends the Agent Token in the upgrade Authorization header. It uses no query credential, cookie, or required subprotocol. onEvent must resolve after a durable event commit. onFullSync is required and must resolve after a complete REST snapshot is durably applied. The SDK sends the cumulative ACK or full_sync_complete only after the corresponding promise resolves. Relay pings every 30 seconds and closes the connection after 60 seconds without a pong. Creating the first webhook subscription closes connected agent sockets and moves pending events to Webhook delivery.

Browser limitation

The Agent Token client and WebSocket runner are server-side Node.js features. The browser WebSocket API cannot set the required Authorization upgrade header, and shipping an Agent Token to a browser would expose it. Use Relay’s user-authenticated app surface for user clients. Do not proxy an Agent Token into browser code.

Runnable examples