Approval workflows
Let users approve or reject requests inside the chat.
Many apps let users discuss a request and approve or reject it. This guide puts the decision directly in the chat, so the reviewer can act on the request right where they're discussing it. In this example, Jamie asks their manager Alex to approve a €120 design workshop.
When your backend receives Jamie's workshop request, send a message on Jamie's behalf to the chat with Alex, storing the request details in the message's custom data:
POST https://api.talkjs.com/v1/<APP_ID>/conversations/{conversationId}/messages
Authorization: Bearer <ADMIN_JWT>
Content-Type: application/json
[
{
"sender": "jamie",
"type": "UserMessage",
"text": "Can I book the €120 design workshop on 12 December?",
"custom": {
"kind": "approval",
"requestId": "request_1042",
"approverId": "alex",
"status": "pending"
}
}
]TalkJS returns the new message ID, for example [{"id": "msg_abc123"}]. Save that ID and the conversation ID with the approval request in your database. Your backend will use them to update the card once Alex decides.
Jamie and Alex both see the workshop request, but only Alex can approve it. We'll use the TalkJS theme system to give each of them the right view of the same message.
Here's what Alex sees while the request is pending:

Create an ApprovalCard component that shows Jamie's question. While the request is pending, it compares the viewer's TalkJS user ID with approverId: Alex gets Approve and Reject buttons, while everyone else sees "Awaiting a decision". Clicking a button calls your onDecision callback and disables the buttons until the request finishes. Once Alex decides, everyone sees the result.
import { useState } from 'react';
export function ApprovalCard({ approval, text, currentUserId, onDecision }) {
const { requestId, approverId, status } = approval;
const [saving, setSaving] = useState(false);
const [error, setError] = useState('');
const canDecide = currentUserId === approverId;
const statusLabel = { approved: 'Approved', rejected: 'Rejected' }[status];
async function decide(decision) {
setSaving(true);
setError('');
try {
await onDecision(requestId, decision);
} catch {
setError("Couldn't update the request. Please try again.");
} finally {
setSaving(false);
}
}
return (
<article className="approval-card" aria-label="Approval request">
<p className="approval-card-label">Approval request</p>
<p className="approval-card-text">{text}</p>
{status === 'pending' && canDecide && (
<>
<div className="approval-card-actions">
<button
className="approval-card-approve"
disabled={saving}
onClick={() => decide('approved')}
>
Approve
</button>
<button disabled={saving} onClick={() => decide('rejected')}>
Reject
</button>
</div>
{saving && <p role="status">Saving…</p>}
{error && <p role="alert">{error}</p>}
</>
)}
{status === 'pending' && !canDecide && (
<p className="approval-card-status">Awaiting a decision</p>
)}
{status !== 'pending' && (
<p className="approval-card-status" role="status">
{statusLabel || 'Status unavailable'}
</p>
)}
</article>
);
}To give the card the appearance shown above, save these styles in approval-card.css:
Card styles
.approval-card {
align-self: flex-start;
width: min(360px, 100%);
box-sizing: border-box;
margin: 8px 0;
padding: 20px;
border: 1px solid #d6deeb;
border-radius: 12px;
background: white;
color: #1e293b;
font: 14px/1.5 system-ui, sans-serif;
}
.t-theme-message .t-message-body:has(.approval-card) {
padding: 0;
background: transparent;
}
.approval-card-label {
margin: 0 0 8px;
color: #64748b;
font-size: 12px;
font-weight: 600;
}
.approval-card-text { margin: 0 0 16px; }
.approval-card-actions { display: flex; gap: 8px; }
.approval-card-status { margin: 0; font-weight: 600; }
.approval-card button {
padding: 8px 16px;
border: 1px solid #cbd5e1;
border-radius: 6px;
background: white;
color: #334155;
font: inherit;
cursor: pointer;
}
.approval-card .approval-card-approve {
border-color: #2563eb;
background: #2563eb;
color: white;
}
.approval-card button:disabled { opacity: 0.6; cursor: wait; }Load approval-card.css right after the TalkJS stylesheet:
import '@talkjs/react-components/default.css';
import './approval-card.css';TalkJS uses the TextBlock theme component to display the text inside a message. Replace it with a wrapper that renders ApprovalCard when message.custom.kind === 'approval' and falls back to defaultTheme.TextBlock for normal messages, keeping Jamie's avatar and timestamp intact.
import { defaultTheme } from '@talkjs/react-components';
import { ApprovalCard } from './ApprovalCard.jsx';
export function ApprovalTextBlock(props) {
if (props.message.custom.kind !== 'approval') {
return <defaultTheme.TextBlock {...props} />;
}
return (
<ApprovalCard
approval={props.message.custom}
text={props.message.plaintext}
currentUserId={props.common.currentUser.id}
onDecision={props.common.themeCustom.onDecision}
/>
);
}Now pass ApprovalTextBlock in your chatbox's theme option, and pass an onDecision function in themeCustom that sends Alex's choice to your backend using your app's usual authenticated request mechanism:
import { Chatbox } from '@talkjs/react-components';
import { ApprovalTextBlock } from './ApprovalTextBlock.jsx';
async function onDecision(requestId, decision) {
const response = await fetch(
`/api/approval-requests/${encodeURIComponent(requestId)}/decision`,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ decision }),
}
);
if (!response.ok) {
throw new Error('Could not update the approval request.');
}
}
export function ApprovalChat({ appId, userId, conversationId }) {
return (
<Chatbox
appId={appId}
userId={userId}
conversationId={conversationId}
theme={{ TextBlock: ApprovalTextBlock }}
themeCustom={{ onDecision }}
/>
);
}When your backend receives Alex's choice, look up the request, verify that the signed-in user is allowed to decide it, and save the decision in your database. Then use the stored conversation and message IDs to edit Jamie's message with an Admin JWT:
PUT https://api.talkjs.com/v1/<APP_ID>/conversations/{conversationId}/messages/{messageId}
Authorization: Bearer <ADMIN_JWT>
Content-Type: application/json
{
"custom": {
"status": "approved"
}
}For a rejection, set "status" to "rejected". Once TalkJS updates the message, return success to the browser. If the TalkJS call fails after saving to your database, retry it on your backend (or return an error and keep the endpoint idempotent) so the card stays in sync with your database.
TalkJS pushes the updated message to both people in real time. Alex's buttons and Jamie's "Awaiting a decision" both update to show the decision, including when either of them reopens the chat later.

An approval message works well when Jamie occasionally asks for approval in an ongoing chat with Alex. If your app opens a new conversation for each request, put the card in the chat header so it stays pinned as the discussion grows. In that version, the request details and status live on the conversation itself.

When creating the conversation on your backend, set the approval details in its custom data:
PUT https://api.talkjs.com/v1/<APP_ID>/conversations/{conversationId}
Authorization: Bearer <ADMIN_JWT>
Content-Type: application/json
{
"participants": ["jamie", "alex"],
"subject": "Design workshop",
"custom": {
"kind": "approval",
"requestId": "request_1042",
"approverId": "alex",
"status": "pending",
"description": "Can I book the €120 design workshop on 12 December?"
}
}Save the conversation ID with the workshop request. You can reuse ApprovalCard as-is; instead of overriding TextBlock, create a ChatHeader component that reads common.conversation.custom and renders the card below the default header:
import { defaultTheme } from '@talkjs/react-components';
import { ApprovalCard } from './ApprovalCard.jsx';
export function ApprovalHeader(props) {
const { common } = props;
const approval = common.conversation.custom;
if (approval.kind !== 'approval') {
return <defaultTheme.ChatHeader {...props} />;
}
return (
<>
<defaultTheme.ChatHeader {...props} />
<div className="approval-header">
<ApprovalCard
approval={approval}
text={approval.description}
currentUserId={common.currentUser.id}
onDecision={common.themeCustom.onDecision}
/>
</div>
</>
);
}Add .approval-header { padding: 0 12px; } to approval-card.css, set the chatbox's theme to { ChatHeader: ApprovalHeader }, and keep the same themeCustom.onDecision callback.
After your backend saves Alex's choice, update the conversation's custom data using the stored conversation ID:
PUT https://api.talkjs.com/v1/<APP_ID>/conversations/{conversationId}
Authorization: Bearer <ADMIN_JWT>
Content-Type: application/json
{
"custom": {
"status": "approved"
}
}For a rejection, set "status" to "rejected". Jamie and Alex will see the decision pinned at the top of the chat, with their discussion underneath.