@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):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/startwithotherUserId; your backend returnstoken,conversation_id, andother_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, passtoken,conversation_id, and the other participant’s display name to the chat screen.
2.4 Chat screen – SDK setup, connect, join, messages
Use one token and one conversationId per chat screen (from your backend or inbox).2.5 Typing indicators
Send typing (e.g. on input change / focus, stop on blur or after idle):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_URLandAPI_URL(same env as backend). - Frontend: get token from your backend; pass token +
conversation_idinto the chat screen. - Frontend:
client.connect()thenclient.conversations.join(cid); thenfetchMessagesandconvo.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).