Connect Web Chat with server relay
Connect an authenticated website user to TalkIt without exposing Website integration credentials.
Use a Website integration in server-relay mode when your application has a trusted backend that authenticates its users.
What server relay means
Your backend relays authentication and session creation. It does not proxy chat messages: after receiving a grant, the browser connects directly to the TalkIt WebSocket URL.
Prerequisites
You need an active TalkIt agent with text mode enabled, a Website integration configured for server relay, its client ID and secret, and @talkit-llc/talkit-js in the backend and frontend projects.
Create an authenticated grant endpoint
- Authenticate and authorize the caller on your backend.
- Derive stable customer and session identifiers from trusted server-side state.
- Call
createTalkitServerRelayGrantwith the Website credentials. - Return only the short-lived grant to the browser.
import { createTalkitServerRelayGrant } from '@talkit-llc/talkit-js/server'
app.post('/api/talkit/text-token', requireAuthenticatedUser, async (req, res) => {
const grant = await createTalkitServerRelayGrant({
apiBaseUrl: process.env.TALKIT_BASE_URL!,
clientId: process.env.TALKIT_WEBSITE_CLIENT_ID!,
clientSecret: process.env.TALKIT_WEBSITE_CLIENT_SECRET!,
channel: 'text',
externalSubjectId: `customer:${req.user.id}`,
externalSessionId: `session:${req.session.id}`,
})
res.json(grant)
})
Do not accept externalSubjectId directly from the browser. Use an immutable identifier from your authenticated account model.
Connect the browser client
import { createTalkitChat } from '@talkit-llc/talkit-js'
async function getSessionToken() {
const response = await fetch('/api/talkit/text-token', {
method: 'POST',
credentials: 'include',
})
if (!response.ok)
throw new Error(`TalkIt grant request failed with ${response.status}.`)
return response.json()
}
const chat = createTalkitChat({
baseUrl: 'https://your-talkit-host.example',
getSessionToken,
})
await chat.connect()
chat.send('Hello from the website')
Request a fresh grant for every new connection or reconnect. Do not cache grants or log session tokens.
Verify the integration
Confirm that an authenticated user can connect, send a message, receive a response, and start a new session after reconnecting. Also confirm that an unauthenticated user cannot call your grant endpoint.
See the current API overview for the HTTP contract and the JavaScript SDK guide for client lifecycle details.