- 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
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 withstream: 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
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
Theuser 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:
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.
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 outstream. 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 aConversationMessage:
contentis 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://...).sourceslists each knowledge article (kb) or code file (code) the answer drew on. Show them as a list under the answer.urlandsnippetaren’t always present.message_ididentifies this answer. Keep it to rate the answer.outcomesays how the agent resolved the message. Use it to offer a person.
5. Take follow-up questions
Send the next message with the sameconversation_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 toPOST /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’soutcome 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:
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 onerror.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_idbelongs to the signed-in customer. - Answers render as sanitized Markdown, with
sourcesshown as citations. - The send button is disabled while a message is running.
- A dropped stream recovers by polling, and
message.failedoffers 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.