What the browser calls
The browser calls only your own token endpoint and then, with the token your backend returned, the Osvi AI chat history load and the live WebSocket. The token is valid for one session and the user stream only (about 5 minutes); the SDK refreshes it throughgetToken(). It never holds your API-Token.
Session flow
- The browser calls your backend’s token endpoint.
- For a normal start, your backend calls Rails
GET /v1/chat/session/active?customer_id=…&agent_uuid=…for the authenticated customer and agent. - If Rails returns 404, create a session with
POST /v1/chat/sessionusing your server-held API-Token. When the SDK callsgetToken({ newSession: true }), skip the active-session lookup and create a fresh session even if one already exists. - Request
POST /v1/chat/session/{id}/ws_token. TakesessionIdfrom the active-session or create-session response; the token response suppliestoken,ws_url, andexpires_at, which your backend returns astoken,wsUrl, andexpiresAt. - The browser SDK connects to the returned
wsUrl.
{ sessionId, token, wsUrl, expiresAt }. It may receive { newSession: true } when the visitor selects New chat; your backend must then create a fresh session instead of reusing the active one.
Node backend sketch
rails client in these sketches is an authenticated server-side HTTP client that returns the Rails status and JSON envelope. Keep its API-Token on the server; never expose it to browser code.
Python backend sketch
rails client represents an authenticated server-side HTTP client. Preserve the Rails status and JSON envelope handling in your implementation.
Welcome carousel images also load for logged-in chats when Anonymous usage is off.
Identity and rotation
A server-mode client belongs to one identity. Destroy it and create a new client when the user logs in, logs out, or switches accounts. The SDK stores no token, session id, or visitor id in browser storage in server mode. It asksgetToken() again on startup, token rotation (about every 4.5 minutes while connected), reconnect after an unauthorized close, and when a new session is needed.
Phone-shaped customer ids are normalized to +91…. Do not use the public visitor format (web: followed by 16–64 URL-safe characters) as a server-mode customer_id; Rails rejects it with 422 invalid_customer_id because it would be addressable on the public rail.