ApolloMessagingClient is the browser-safe client for end-user messaging. It
authenticates with your publishable key, exchanging it for a short-lived access
token internally — the agent comes from the key, never from request bodies.
async, fully typed, and throw typed
errors on failure. Request and response
models are exported under the Apollo namespace.
Send a message
Omitthread_id to start a new conversation, or pass it to continue one.
cards. Each card holds the same entity in two
self-contained representations, so you pick what fits your stack: a
ready-to-render JSX string for React, or structured JSON for any other
framework — see Cards for the full shape.
title, capability, and instance are optional and may be null — read them
defensively.
Pass structured data the caller should not have to state — the signed-in user,
the account in view, what your systems already know — as
agent_variables: a flat
Record<string, unknown> whose keys are the context variables the agent’s
program declares. Attach an image for the agent to reason over with
image_url:
runtime_version — an advanced build pin
for the turn; normally omit it.
Attach files
Attach a file in three steps: create an upload,PUT the file’s bytes to it,
then pass the upload’s file_url in the message’s file_urls.
streamMessage takes file_urls the same way. rerun doesn’t take files, and
runtime v1 agents refuse a send that carries them. See
Attach files for the underlying
endpoint.
Requires SDK 3.4.1 or later.
Stream a message
streamMessage is the streaming transport: one HTTP call per turn that
returns the reply as an async-iterable stream of Server-Sent Events — token
deltas while the agent works, cards as they are ready, then the complete
message, then ready-made follow-up suggestions. It takes the same request body and auth as
sendMessage, leaves you no connection to keep alive, and resumes after a drop
without re-running the turn:
suggestions frame
after the terminal message — render them as tappable chips, no extra call
needed. Its data carries followup_suggestions alongside the thread_id and
interaction_id it belongs to. (Runtime v1 agents
don’t emit this frame; their followup_suggestions ride on the message itself.)
A turn that surfaces entities can also emit cards frames before the terminal
message, so you can show cards while the reply is still streaming. Each frame
carries the turn’s complete card list so far and replaces the previous one. The
terminal message’s cards replace them all, and an empty cards frame before
an error or the end of the stream withdraws them. The frame is optional — a
turn without cards emits none — and it requires SDK 3.4.2 or later.
(Runtime v1 agents never emit it; their cards ride on
the message.)
To resume a dropped stream without re-running the turn, reconnect with the
last seq you saw:
Rerun an interaction
Regenerate a previous interaction — with the original or edited text — on a new thread branched from the original. Bothinteraction_id and text are
required:
Read a conversation
A thread lives on the runtime that created it, so the transcript read names that runtime — pass the runtime version your agent runs on asruntime_version (sending resolves it automatically; see
Threads and runtime versions):
1.x family selects runtime v2 — it doesn’t have to
match the thread’s exact build. Threads of
runtime v1 agents take no selector:
listMessages(threadId) reads them as is.
Traces
Every interaction’s reasoning trace — what the agent understood, which sources it relied on, what it decided. The shape follows the runtime the agent runs on, andtrace.runtime says which one you got:
Apollo.Trace is a discriminated union — Apollo.Trace.V1
(Apollo.RuntimeV1Trace with runtime: "v1") or Apollo.Trace.V2
(Apollo.RuntimeV2Trace with runtime: "v2") — so the if narrows it. The
runtime v2 sections are typed too (Apollo.RuntimeTraceFact,
Apollo.RuntimeTracePassage, and the sources, citations_used, decided,
checks and answer item types under Apollo.RuntimeV2Trace).
Requires SDK 3.4.0 or later. Earlier versions type
Trace as the runtime
v1 shape only and don’t know include_trace or the thread_id argument.include_trace: true on sendMessage
/ streamMessage / rerun; the reply carries the same object as
message.trace_info. Off by default — a trace can be large. On a stream it
rides the final message event:
threadTrace takes page[size], page[after] /
page[before], and sort_by / sort_order in its second argument and
returns { results, meta, links }:
thread_id throws
Apollo.BadRequestError (the message says to pass it, or to use
include_trace on the send); a thread that belongs to another agent than the
key’s throws Apollo.NotFoundError. Both carry the standard
error envelope in error.body.
Watch a conversation live
A reply to a send is not the only message a conversation receives. A person on your team can answer in it (see Human handover), and the agent can speak again on its own — after an outside approval, for example. To show those messages as they arrive, open a WebSocket withconnect() and
watch the conversation. Every message it saves is then pushed to you.
Watching is receive-only. Keep sending turns with sendMessage or
streamMessage.
Follow these rules:
- Wait for
watching, then read the transcript. The feed carries what the conversation saves from then on, not its history. - Show each
data.idonce. The reply to your ownsendMessageorstreamMessageis pushed too, and a message can be pushed again after a reconnect. - Show what the end user typed yourself. A frame carries the turn’s outcome, not the end user’s message that started it. A transcript read returns both.
- Keep
sequenceorder.sequencenumbers the conversation’s turns in the order they were saved. A frame that skips a number means a turn was missed. Read the transcript again. - After a reconnect, watch again and read the transcript again. The socket
reconnects on its own (30 attempts by default), so sending
watchfrom theopenhandler covers it. - Watch one conversation per socket. A new
watchreplaces the socket’s previous one.
data.code:
The socket closes with code
1013 when the server could not keep the watch
whole. Let it reconnect, then watch and read the transcript again. Code 1008
is an authentication failure, and 1011 is a server error.
Requires SDK 3.4.2 or later. The wire protocol is in
Watch a thread live.
Human handover
An agent can hand a conversation to a person on your team. While the person holds it, the end user’s messages go to that person instead of the agent, and the person’s answers arrive in the same conversation.message.disposition
says what happened to each message of such a conversation. It is null on
every other message.
Two cases matter in a chat UI:
sender.name is set only when sender.type is human. The disposition
fields:
Only
kind is always present, and the SDK types every value as a plain
string. Compare against the values above and keep a default branch.
Transcripts (listMessages, and threads.getThreadMessages on the management
client) carry the same disposition and sender.name, so a conversation
renders the same way live and on reload.
Your team’s side — the person’s messages, taking a conversation over, returning
it to the agent — is sent from your backend, not through the SDK. See
Human handover on the events webhook.
Requires SDK 3.4.2 or later. On
runtime v1 agents,
disposition is always null.Welcome message and follow-up suggestions
context, generateFollowupSuggestions returns the agent’s
configured static suggestions.
Client context
After the first exchange (or an explicitgetContext() call), the client knows
which agent and organization its key resolves to:
Channels (WhatsApp and SMS)
Start an outbound conversation on a channel withchannels.initiateThread.
Pass 'sms' or 'whatsapp' as the channel.
phone_number is the recipient (who receives the opener). sender_id is
not that phone number. It is the MongoDB ObjectId of a sender record you
already connected in the Playground.
If you connected a specific phone number through the Playground (SMS or
WhatsApp), copy that sender’s ObjectId and pass it as sender_id. The SDK
then starts the conversation from that connected number. Without
sender_id, the platform default sender is used instead.
The response includes from when a specific sender was used, so you can
confirm which connected number the opener was sent from.
text is
used for SMS and ignored by WhatsApp.
Method reference
Every method also accepts a final
requestOptions argument
(timeoutInSeconds, maxRetries, abortSignal, extra headers) — see
Best practices.