API · Connect your systems
Messages to the agent
Tell the avatar's AI something it didn't hear from the visitor: who they are, what they bought, what just happened in your system. At call start, or at any moment while the call runs.
The shape of a message
One message
{ "text": "Order A4512 has shipped. It arrives Friday.", "when": "next_turn", "exact": false, "scope": "turn", "expires_s": 120 }| Field | Values | Default at call start | Default during the call |
|---|---|---|---|
text | What the AI should know or do. 1 to 2,000 characters, in any language. | required | required |
scope | turn: for one reply only. call: for every reply until the call ends. Read the next section. | call | turn |
when | next_turn: used in the AI's next reply. next_pause: the avatar speaks up at the first quiet moment, without waiting for the visitor. | doesn't apply (nobody has spoken yet) | next_turn |
exact | true: said word for word, no AI. At call start it replaces the greeting (one at most). During the call only with next_pause. | false | false |
expires_s | 1 to 3,600 seconds. Not used in time: dropped. | none | none |
A message is not a line for the avatar to read out. The AI gets your text exactly as you wrote it, marked as coming from you (never as something the visitor said), and puts it in its own words, in the visitor's language. To have something said exactly as you wrote it, use exact.
How to write one
Write it the way you'd brief a member of your staff: say plainly what you want done. What the AI does with it depends on the scope.
| You write | scope: turn (one reply) | scope: call (rest of the call) |
|---|---|---|
| “Order A4512 has shipped. It arrives Friday.” | Told in the next reply, after the AI answers the visitor. | Known for the rest of the call and used when the visitor gets there. Not brought up out of the blue. |
| “Ask if she’d like a one-on-one call with our sales lead.” | Asked in the next reply. | Asked when it fits, not on every reply. |
| “Offer 20% off the first month.” | Offered in the next reply. An offer is made, never announced as already applied. | Offered when it fits. |
| “Ask once, when it fits, if they’d like a call with our sales lead.” | — | Asked once, at a good moment. |
| “Early in the call, ask if they’d like a call with our sales lead.” | — | Asked within the first few replies, once. Good in a call-start message. |
| News to tell now AND keep: send the same text twice in one request, once with scope turn and once with scope call. | Told in the next reply. | Still known later (“When did you say it arrives?” → “Friday”). |
- •A turn message always comes across in the next reply: news is told, a question is asked, an offer is made.
- •A call message is background the AI keeps. If you want it said, say so in the message, and when.
Turn or call: what the AI remembers
⚠ A turn-scope message is forgotten after one reply
scope: "turn" message for its next reply only. After that reply it has no recollection of the message. Only what it actually said in that reply stays in the conversation. If the visitor asks about it two replies later, the AI won't know, and may even say it doesn't. If the AI should keep knowing something, send it with scope: "call".The same news, sent both ways:
| scope: turn | scope: call | |
|---|---|---|
| You send | “Order A4512 has shipped; it arrives Friday.” | “Order A4512 has shipped; it arrives Friday.” |
| Next reply | “Good news: your order is on its way!” (may leave out the day) | Told when the visitor gets there, not necessarily in the next reply. To have it told at once too, send it twice: once as turn, once as call. |
| Three replies later: “When did you say it arrives?” | The AI only knows what it said. It said “on its way”, so it doesn't know the day. | “Friday.” The AI still has the message. |
| Cost | Read once | Read before every reply until the call ends |
Which to use
- •call for facts that stay true: who the visitor is, their plan, their cart, an order's status, today's opening hours. It is the default at call start for this reason.
- •turn for one-off nudges and news to react to once: “ask if she'd like gift wrap”, “mention the weekend sale once”. It is the default during the call, so a stream of updates doesn't pile up.
- •Mid-call news you want the AI to keep: send it with
scope: "call"explicitly too; to have it told at once as well, send the same text as a turn message in the same request.
⚠ A call-scope message can't be taken back
Keep call-scope messages short. Each one is read before every reply, and long ones make replies slower and less focused. All call-scope messages in force at once may hold 16,000 characters in all.
At call start
Add messages to POST /v1/sessions, at most 20. They default to scope: "call". One exact message replaces the agent's greeting for this session.
curl -X POST https://talk.selviaai.com/v1/sessions \
-H "Authorization: Bearer $SELVIA_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"agent_id": 7,
"external_user_id": "u_88",
"messages": [
{"text": "Hi Maria, welcome back to Lumen Shoes! How can I help today?", "exact": true},
{"text": "Maria is a Gold member: 10% off every order. Her cart: white trainers, size 38."},
{"text": "Early in the call, mention that the trainers are back in stock in her size.", "scope": "turn"}
]
}'- •The exact greeting is said word for word and never translated: write it in the language the visitor will speak, and keep it short, because the visitor waits for it to finish before they can talk.
- •A turn-scope message at start rides the first reply: the AI's opening, or the reply to the visitor's first words when the greeting is fixed.
- •If you only need values in the agent's instructions or your data handoffs, dynamic variables (
vars) still work. Messages are for telling the AI things directly.
From your website
A website using the embed widget passes start messages inside the signed token, as a messages claim next to vars and external_user_id. Because the token is signed on your server, the messages count as yours.
A signed token with messages
// On your server, per page view (Node.js, the jsonwebtoken package)
const token = jwt.sign(
{
external_user_id: "u_88",
messages: [
{ text: "Hi Maria, welcome back!", exact: true },
{ text: "Maria is a Gold member: 10% off every order." }
],
exp: Math.floor(Date.now() / 1000) + 3600
},
process.env.SIGNING_SECRET, // vsk_… the agent's secret for visitor details. Server only.
{ algorithm: "HS256" }
);The page can also add messages with JavaScript, no server needed: see From your web page.
During the call
/v1/sessions/{session_id}/context1 to 20 messages per request. The answer is 202 with one id per message. It works for every live conversation session of your account, however it was started: the API, the website widget, a direct link. For a widget call, your server learns the session_id from a call-start hook, or from GET /v1/sessions.
curl -X POST https://talk.selviaai.com/v1/sessions/SESSION_ID/context \
-H "Authorization: Bearer $SELVIA_API_KEY" \
-H "Content-Type: application/json" \
-d '{"messages": [{"text": "Payment for the trainers just went through. They ship tomorrow.", "when": "next_pause"}]}'next_turn or next_pause
| next_turn (default) | next_pause | |
|---|---|---|
| What happens | The AI uses it in its next reply, after the visitor next speaks. | At the first quiet moment (about a second with nobody talking), the avatar speaks up on its own, in 1 to 2 sentences. |
| If the visitor speaks first | — | It rides their turn instead, like next_turn. |
| Good for | News, a question or an offer that can wait until the visitor next speaks | News the visitor should hear now: a payment, a delivery, an answer your slow job just found |
- •A reply the AI is already writing doesn't pick up a message that arrives during it. The next one does.
- •The avatar never cuts the visitor off to deliver a message. If someone talks without pause, a
next_pausemessage waits, and rides their next turn. - •If nobody is connected (a page reloading), it waits for them. Use
expires_sfor news that is useless when late.
Word for word
exact: true with when: "next_pause" makes the avatar say your text exactly, at the next quiet moment, with no AI in between. The AI knows it was said and can answer follow-up questions about it.
- •Never translated. Write it in the language of the call.
- •Keep it to a few sentences (under about 500 characters). Long exact text sounds stilted.
- •The visitor can talk over it like any reply.
409 call_ended and nothing is stored, so a late message can never reach another visitor.From your web page
With the website widget (embed.js) on your page, SelviaAI.addContext(text, options) sends a message from the page itself. Call it before or during a call, before or after the widget has loaded. The options are the fields above: when, scope, exact, expires_s.
On your page
<script>
// Safe to call before or after the widget has loaded.
window.SelviaAI = window.SelviaAI || { q: [], identify: function (v) { this.q.push(v); },
addContext: function (t, o) { this.q.push(["addContext", t, o]); } };
// What the visitor is doing on your page:
SelviaAI.addContext("The visitor is looking at the white trainers, size 38.");
// Words to say exactly as written, at the next quiet moment:
SelviaAI.addContext("Your payment went through. Thank you!", { when: "next_pause", exact: true });
</script>| When you call it | What happens |
|---|---|
| Before the visitor starts a call | Kept in the page and sent with the call's start, so the call-start defaults apply (scope: "call"). An exact one replaces the greeting; if there are several, the newest. |
| While the avatar is starting | Sent as soon as she is on screen. |
| During the call | Sent at once, with the during-the-call defaults (scope: "turn", when: "next_turn"). |
| After the call has ended | Kept for the visitor's next call. |
addContext from their browser's developer tools, so a visitor could give your avatar instructions within their own call. Keep anything the avatar must never be talked into in its instructions, and check anything that unlocks a price, an account or an order on your server.- •A mistake (an unknown value,
exactwithoutwhen: "next_pause", text over 2,000 characters) shows in the browser console, and nothing is sent. The call returnsfalse. - •You can also pass one object:
SelviaAI.addContext({ text: "…", scope: "call" }). - •
expires_scounts from the moment you called it, including time spent waiting for a call. - •Up to 20 messages wait in the page; past that the oldest is dropped. During a call, up to 30 a minute.
- •
SelviaAI.reset()(on sign-out) also drops messages that haven't gone yet. - •The inline frame (an
<iframe>of the widget, noembed.js): post the same message to the frame,document.getElementById("selvia-embed").contentWindow.postMessage({ type: "selvia:context", messages: [{ text: "…" }] }, "https://talk.selviaai.com"). The same rules apply; only the page that frames it is heard. - •A direct link or a QR code has no page around the avatar, so it can't use
addContext.
Check that it got through
/v1/sessions/{session_id}/contextEvery message of the live call, with its status. Works while the call runs.
Answer
{
"messages": [
{ "id": "m_1795801496391_0000", "status": "delivered", "delivered_turn": 1414,
"text": "Payment for the trainers just went through. They ship tomorrow.",
"when": "next_pause", "exact": false, "scope": "turn",
"trust": "business", "source": "api",
"arrived_at": 1795801496.39, "expires_at": null }
]
}| Status | Means |
|---|---|
waiting | Not used yet. |
delivered | A turn-scope message used in a reply (delivered_turn). |
active | A call-scope message in force: read before every reply until the call ends. |
expired | Its expires_s ran out before it could be used. |
source tells you where it came from: api, token (a signed token), page (addContext on your web page), or hook:<name> / tool:<name> when your hook or tool answered with messages.
Things to get right
⚠ Never put secrets in a message
- •Write messages as plain facts or clear instructions, as you would brief a colleague: “She already paid; don't offer the discount again.”
- •Messages don't override the agent's own rules. A message that contradicts them makes the AI unpredictable: change the agent's setup instead.
- •Messages are not part of the transcript. What the avatar said because of them is.
- •Conversation sessions only. A render session answers
409 no_ai.
Limits and errors
| Limit | Value |
|---|---|
| Messages per request | 1 to 20 (at call start: up to 20) |
text | 2,000 characters each |
| Call-scope messages in force at once | 16,000 characters in all |
| Messages waiting in one call | 50 |
| Exact messages at call start | 1 (it replaces the greeting) |
| Code | Status | Means |
|---|---|---|
invalid_messages | 422 | A message breaks a rule: empty text, too long, exact without next_pause mid-call, two exact messages at start, an unknown field. The message says which. |
session_not_found | 404 | No live session with this id in your account. |
call_ended | 409 | The call ended. Nothing was delivered. |
no_ai | 409 | A render session: there is no AI of ours to tell. |
too_large | 413 | Call-scope messages would pass 16,000 characters. |
too_many_messages | 429 | 50 messages are already waiting in this call. Wait for them to be used. |
messages_unavailable | 503 | At start: messages can't be taken by the avatar host right now. No session was started; try again. |
context_unavailable | 501 | Mid-call: messages aren't available for this session's avatar host. |