Skip to main content
This guide builds a chat experience like the chat widget, but in your own interface. If the widget’s look and layout options are enough, install the widget instead. It takes minutes. By the end, your chat will:
  • Send a customer’s question and show what the agent is doing while it works
  • Show the answer with its citations
  • Take follow-up questions in the same conversation
  • Let the customer rate an answer
  • Hand the conversation to your helpdesk when the customer needs a person

How the pieces fit

Your API token can read every conversation in your organization, so it stays on your server. The browser talks only to your server. Your server checks who the customer is, adds the token, and passes the request on to Decimal. Your server also decides which conversations each customer may see. Decimal knows the conversation, not your customer, so record which of your users started each conversation_id and refuse any other user’s.

1. Create a token

Open Settings → API Keys and create a token with these scopes: Leave out chat:escalate if you won’t offer a handoff. Store the token as a server environment variable, such as DECIMAL_API_KEY. To offer a handoff, also connect your helpdesk.

2. Proxy messages through your server

Send messages with stream: true. The answer then comes back over the same connection as server-sent events, so you don’t have to poll. This Next.js route handler forwards a message and streams the events back to the browser. getSignedInUser, ownsConversation, and saveConversation stand for your own auth and storage.
app/api/support-chat/route.ts
The first event is always message.started, which carries the new conversation_id. The handler saves it against your user before the answer arrives. Add matching routes for the other calls your chat makes. Each one checks ownsConversation first:

Tell the agent who’s asking

The user object holds the customer’s name and email. The agent uses the name to address them. It’s saved on the conversation, so follow-ups can leave it out. The agent treats user as context, not proof of identity. Policies that need a signed-in visitor don’t run through the Chat API, and customers who ask about those topics get the agent’s normal answer instead.

3. Read the stream in the browser

EventSource can only send GET requests, so read the stream with fetch:
Blocks with no data: line are keep-alives, sent every 15 seconds, and are skipped. A retry that reuses an idempotency key gets the original 202 JSON body instead of a stream. The code above turns that into a message.started event. You then poll for the answer.

Handle each event

The answer arrives whole in message.completed. There’s no word-by-word text. Use the tool events to show progress while the customer waits.
Tool names depend on what the agent can use. The tools it can call change with your Policies, connected MCP servers, and custom tools. Map the names you want to label and fall back to a generic one:
input holds the tool’s arguments, such as a search query. Show them only if they’d make sense to your customer.

If you can’t stream

Leave out stream. Sending returns 202 with conversation_id and message_id. Poll GET /v0/chat/{conversation_id} until status is completed or failed. Every poll counts toward your rate limits, and all your customers share your token’s 120 requests a minute. Polling every 2 seconds uses 30 of those per waiting customer, so streaming scales much further. If you poll, wait at least 2 seconds between requests and honor Retry-After on a 429.

4. Show the answer

An answer is a ConversationMessage:
  • content is Markdown. Render it with a Markdown library that sanitizes HTML, because answers can quote what customers typed. Citations are already inline as superscript links, such as [¹](https://...).
  • sources lists each knowledge article (kb) or code file (code) the answer drew on. Show them as a list under the answer. url and snippet aren’t always present.
  • message_id identifies this answer. Keep it to rate the answer.
  • outcome says how the agent resolved the message. Use it to offer a person.

5. Take follow-up questions

Send the next message with the same conversation_id. The agent keeps the earlier messages as context. A conversation answers one message at a time. Disable your send button from message.started until message.completed or message.failed. A message sent while the conversation is still working gets 409 with code conflict. To restore a chat after a page reload, look up the customer’s saved conversation_id and load it with GET /v0/chat/{conversation_id}. messages holds the whole conversation, oldest first, in the same shape as above. If status is still processing, show the typing indicator and poll until it finishes.

6. Let customers rate answers

Add a helpful / not helpful control under each answer and send the rating to POST /v0/chat/{conversation_id}/feedback:
rating is helpful or not_helpful. comment is optional, up to 2,000 characters. Leave out message_id to rate the latest answer. Rating the same answer again replaces the earlier rating, so customers can change their mind. You can rate answers only once the conversation is completed.

7. Hand off to a person

Show a Talk to a person button on every conversation. Bring it forward when the latest answer’s outcome says the customer needs one: When the customer asks for a person, call POST /v0/chat/{conversation_id}/escalate with your helpdesk and their name and email. Decimal opens a ticket with the transcript and a summary:
Then tell the customer how your team will reply, such as by email. Messages sent after this aren’t added to the ticket, so close the chat or point the customer to the ticket. A conversation can be escalated once, and only when it’s completed. A second call gets 409 with code conflict. See the Escalation API for each helpdesk’s fields.

8. Handle errors

Errors share one shape. Branch on error.code, and retry when error.retryable is true. Create one Idempotency-Key per message the customer sends, such as with crypto.randomUUID(), and reuse it only to retry that same send.

Before you launch

  • The token lives only on your server, with only the scopes you need.
  • Your server checks that each conversation_id belongs to the signed-in customer.
  • Answers render as sanitized Markdown, with sources shown as citations.
  • The send button is disabled while a message is running.
  • A dropped stream recovers by polling, and message.failed offers a retry.
  • Talk to a person escalates once, and the customer knows how your team will reply.
  • Test a few real questions and review them under API conversations.