Skip to Academy content
Guest progress is stored only in this browser on this device.
Course outline
Step 3 of 3
Revision 2026-08-12-interactive
12 min

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.

Server relay session flow

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

  1. Authenticate and authorize the caller on your backend.
  2. Derive stable customer and session identifiers from trusted server-side state.
  3. Call createTalkitServerRelayGrant with the Website credentials.
  4. Return only the short-lived grant to the browser.
server.ts
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

chat.ts
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.

Practice and knowledge checks

Apply the lesson and complete the required knowledge checks or manual tasks.

Practical task
Required

Confirm the website channel

Open your workspace and confirm that the website channel is enabled. This confirmation is stored locally and is not verified against your workspace.

Your progress

This step completes when all required exercises pass.

Complete the required exercises above to finish this step.