This guide shows how to integrate the Vocantly SDK (@vocantly/sdk) in your app using a backend-for-token pattern: the backend creates conversations and issues SDK tokens; the frontend uses the token only (no API keys on the client).

Architecture overview

  • Backend: Uses Vocantly API keys to create external conversations and issue short-lived SDK tokens. Never exposes API keys to the frontend.
  • Frontend: Instantiates the SDK with token + wsUrl + apiUrl; connects, joins conversations, and sends/receives messages. No API keys on the client.

1. Backend

1.1 Environment variables

1.2 Vocantly service (Node.js example)

Your backend calls the Vocantly REST API with API keys to create conversations and issue tokens.

1.3 API routes

Get token (e.g. for Inbox / conversation list):
Start or get a 1:1 conversation and return token + conversation_id:

2. Frontend

2.1 Install the SDK

2.2 Config (WS + API URLs)

Point the SDK at the same environment as your backend (e.g. dev).

2.3 Getting a token and opening a chat

  • From user list (start new chat): Call your POST /api/chat/start with otherUserId; your backend returns token, conversation_id, and other_user. Navigate to the chat screen and pass these in state (or params).
  • From inbox (existing conversation): Call your GET /api/chat/token, then use the SDK to list conversations. When the user opens a conversation, pass token, conversation_id, and the other participant’s display name to the chat screen.
Example (React Router) – starting a chat from a user list:

2.4 Chat screen – SDK setup, connect, join, messages

Use one token and one conversationId per chat screen (from your backend or inbox).
Connect and join:
Fetch message history (after join):
Listen for new messages – and dedupe: The SDK may emit the same message more than once (e.g. optimistic + server echo, or network replay). To avoid duplicate UI entries and React key warnings, only append a message if its id is not already in the list:
Send a message:
Cleanup on unmount:

2.5 Typing indicators

Send typing (e.g. on input change / focus, stop on blur or after idle):
Receive typing:

2.6 Inbox – list conversations (token only)

No API keys on the frontend. Get a token from your backend, then use the SDK to list conversations:

3. Important details

4. Checklist

  • Backend: env vars set (VOCANTLY_APP_ID, VOCANTLY_PK, VOCANTLY_SK).
  • Backend: endpoint to issue token (e.g. GET /api/chat/token).
  • Backend: endpoint to create/start conversation and return token + conversation_id (e.g. POST /api/chat/start).
  • Frontend: config with WS_URL and API_URL (same env as backend).
  • Frontend: get token from your backend; pass token + conversation_id into the chat screen.
  • Frontend: client.connect() then client.conversations.join(cid); then fetchMessages and convo.on('message').
  • Frontend: in convo.on('message'), dedupe by message id before appending (avoid duplicate keys / bubbles if SDK emits same message twice).
  • Frontend: cleanup on unmount (leave, disconnect).
This pattern keeps your app secure (keys on backend only) and gives a clear path from backend (conversations + tokens) to frontend (SDK with token only).