Skip to main content

The useWebChat hook is the headless client for Web Chat. It loads conversation history, sends messages, streams live turns over the socket, and exposes pending tool approvals.

Use it inside NovuProvider. See Web Chat, Messages, and State.

Hook parameters#

Pass agentId (and optional conversationId and agentHash), or pass conversation. Do not mix the two.

PropertyTypeDescription
agentIdstringPublic agent identifier from the dashboard. Required unless you pass conversation.
conversationIdstringResume this conversation and load history on mount. Omit this prop to start a new chat. The first sendMessage creates the conversation. See Start or resume.
agentHashstringHMAC-SHA256 of agentId with the environment secret. Required when Security HMAC is on for the Web Chat integration.
conversationAgentConversationRuntimeShare an existing runtime across hook instances. Do not pass agentId, conversationId, or agentHash with this prop. Get the runtime from loadWebChat(novu) then novu.webChat.conversation().
onSuccess(data: LoadConversationResult) => voidFires after a history fetch succeeds.
onError(error: NovuError | WebChatPlanLimitError) => voidFires when a load, send, retry, or action response fails. Also fires when reconnect recovery fails.
onMessage(message: AgentMessage) => voidFires once per new message id. History pages are silent. The first event of a turn can create an empty assistant message before text arrives. A send that never reaches the server does not fire. The message status becomes failed instead.
onActionRequested(action: AgentPendingAction) => voidFires once per pending action, including actions still pending on mount. Paging backwards is silent.
onEvent(envelope: AgentEventEnvelope) => voidRaw live envelopes for this conversation. Duplicate envelopes that the store drops do not fire.

Return value#

PropertyTypeDescription
messagesAgentMessage[]Timeline for the current conversation.
pendingActionsAgentPendingAction[]Tool approvals and MCP connect items that are still pending. Derived from messages.
conversationIdstring | undefinedServer conversation id after create or resume. Undefined until the first successful send on a new chat.
errorNovuError | WebChatPlanLimitError | undefinedLast error from load, send, retry, respondToAction, sendAction, or a failed agent run.
isLoadingbooleanTrue while the Web Chat module loads. For an existing conversation, it stays true until the first history fetch completes.
isRunningbooleanTrue while the agent turn is in progress. Same as run.isRunning.
typingAgentConversationTyping | undefinedEphemeral typing indicator. Same as run.typing. Absent when the agent is not typing.
run{ isRunning: boolean; typing?: AgentConversationTyping }Current agent-run snapshot.
conversationStatus'active' | 'resolved'Conversation lifecycle. The agent sets resolved with ctx.resolve(). Not a loading flag. See State.
pagination{ status, hasMore, fetchMore }Older history pages. Not a top-level hasMore or fetchMore. See Pagination.
isRecoveringbooleanTrue while reconnect recovery is in progress.
catchUpErrorNovuError | undefinedSet when reconnect recovery fails. Separate from send and fetch error.
refetch() => Promise<void>Reload the newest history page. No-op when there is no conversation id.
sendMessage(input: SendMessageInput) => Promise<{ data?: SendMessageResult; error?: NovuError | WebChatPlanLimitError }>Send a user message. input is a string, or { text, metadata }. Creates a conversation when conversationId is omitted.
retryMessage(messageId: string) => Promise<{ data?: SendMessageResult; error?: NovuError | WebChatPlanLimitError }>Resend a message whose status is failed. Reuses the original idempotency key. Does not create a second message. See Retry.
respondToAction(args: { approvalId: string; decision: AgentToolApprovalDecision }) => Promise<{ data?: RespondToActionResult; error?: NovuError | WebChatPlanLimitError }>Resolve a pending approval. Pass approvalId from pendingActions or message parts. Typical decisions: 'approved' or 'denied'.
sendAction(args: { actionId: string; sourceMessageId: string; value?: string }) => Promise<{ data?: SendActionResult; error?: NovuError | WebChatPlanLimitError }>Click a Card button. Pass id / value from the button and message.id as sourceMessageId. Do not use this for tool approval. See Cards.
startNewConversation() => voidClear the current conversation and start a fresh one without remounting the component. The next sendMessage creates the new server conversation. No-op when you pass conversationId or a shared conversation runtime. See Start or resume.

Pagination#

pagination is the older-history control. It is not a top-level hasMore or fetchMore.

PropertyTypeDescription
status'idle' | 'loading' | 'error'Status of the older-history request.
hasMorebooleanTrue when an older page is available.
fetchMore() => Promise<{ data?: { messages: AgentMessage[]; hasMore: boolean }; error?: NovuError }>Load the next older page. Overlapping calls do not run.

See Older messages.

Recovery#

The hook reconnects and catches up on its own. Use these fields for a banner.

PropertyTypeDescription
isRecoveringbooleanTrue while recovery is in progress.
catchUpErrorNovuError | undefinedSet when recovery fails. conversationStatus does not become an error state.

See Reconnect.

Message type#

PropertyTypeDescription
idstringMessage id. Optimistic local ids are replaced after the server ack.
roleAgentMessageRoleSender role (user or assistant).
partsAgentMessagePart[]Ordered content. See part types below.
createdAtstringISO timestamp.
status'sending' | 'sent' | 'failed'Delivery status for the local send. If the status is failed, call retryMessage(id).

Part types#

typeWhen you see it
textVisible message text. state is streaming or done.
thinkingChain-of-thought / plan text while the agent reasons.
toolTool call and result. See Tool calls.
approvalGated tool waiting on the subscriber. Use pendingActions + respondToAction. See Tool approval.
mcp-connectionMCP server connect card. Open authorizeUrl in the browser. See MCP connect.
cardStructured Card tree (card object). Render it in your UI. Button clicks call sendAction. Agents send Cards with Interactive cards. See Cards.
dataCustom payload (name + data). The UI decides how to render it. See Custom data.
fileFile attachment metadata.
sourceCitation (url or document).

Pending actions#

pendingActions is derived from pending approval and mcp-connection parts.

typeWhat to do
approvalCall respondToAction({ approvalId: action.approvalId, decision: 'approved' | 'denied' }).
mcp-connectionOpen authorizeUrl in the browser. Do not invent action ids.

Plan limit error#

WebChatPlanLimitError is set on the hook error field when the organization is over a plan limit. The HTTP status is 402.

PropertyTypeDescription
name'WebChatPlanLimitError'Discriminator.
reason'agents' | 'channels' | 'conversations'Which limit blocked the request.
messagestringHuman-readable error.

JavaScript SDK#

The same runtime lives on novu.webChat.conversation() in @novu/js. Call loadWebChat(novu) first in JavaScript. The React hook loads Web Chat for you. Do not call novu.webChat.subscribe() from React UI code.