API · Sessions
How sessions work
A session is one live avatar on one screen: one visitor, one conversation, from start to stop.
The life of a session
- •Start. Your server calls
POST /v1/sessionswith an agent. The avatar is ready in a second or two when one is warm. - •Show. The visitor opens
player_url, in an iframe on your page or as a link. In render mode your server also connects tows_url. - •Talk. In a conversation the avatar talks with the visitor on its own. In render mode it speaks what you send it.
- •Stop. You call
POST /v1/sessions/{id}/stopwhen the conversation is over. The page disconnects and the session's minutes are counted. If the visitor leaves first, we end it for you (below).
Two modes
| Mode | Who talks | Price |
|---|---|---|
conversation | Your Selvia AI agent: it listens to the visitor and answers from its knowledge. | $0.03 / min |
render | Your own agent. You stream speech (or text we voice) to the avatar. See Render mode. | $0.025 / min |
How a session ends
A session runs until one of these happens. Its status then shows the reason.
| end_reason | What happened |
|---|---|
api_stop | You stopped it with the API. |
session_limit | A free account reached its 5 minutes for one session. |
viewer_closed | The visitor closed or reloaded the player page. Ended at once. |
viewer_video_failed | The visitor's connection dropped for about 15 seconds. |
idle_timeout | Nobody opened the player page within about 90 seconds. |
embed_ended | A visitor ended a conversation in the website widget. |
Stop when the visitor leaves
When the visitor closes or reloads the player page, the session ends at once. When their connection drops, it ends about 15 seconds later; a quick switch between networks (Wi-Fi to mobile data) inside that time keeps the conversation going. To stop it yourself as the page goes away, have the page tell your server and let your server call stop, so your key never leaves your server. navigator.sendBeacon on pagehide still gets through while the page closes.
Your page
// sessionId: the session_id your server got from POST /v1/sessions.
// /avatar/stop is a route on YOUR server: it calls POST /v1/sessions/{id}/stop with your key.
addEventListener("pagehide", () => {
navigator.sendBeacon("/avatar/stop", JSON.stringify({ session_id: sessionId }));
});One visitor, one session. Before you start a new session for the same visitor (a reload, a second click), stop their previous one. GET /v1/sessions lists every session still running.
One page per session
player_url works only for its own session. When the session ends the page disconnects, and opening it again shows nothing: a page left open can never watch the next visitor's session. Start a new session for each visitor.
List live sessions
/v1/sessionsEvery live session of your account: the ones you started with the API, website-widget visitors and console calls.
curl https://talk.selviaai.com/v1/sessions \
-H "Authorization: Bearer $SELVIA_API_KEY"Check a session
/v1/sessions/{session_id}status is active while it runs and ended afterwards. In render mode your socket also closes with code 4001 when the session ends.
Response
{
"session_id": "1b913015-776e-4700-b17f-f1a9cdc8338f",
"status": "ended",
"mode": "render",
"source": "api",
"agent_id": 7,
"started_at": "2026-10-17T10:55:15Z",
"ended_at": "2026-10-17T10:58:19Z",
"end_reason": "api_stop"
}Stop a session
/v1/sessions/{session_id}/stopEnds any of your sessions, whatever started it. Stopping one that already ended answers the same way, so it is safe to retry.
Capacity
Each agent has a number of conversations it may hold at once (its concurrency_limit, set on the agent). Past it, a new session answers 409 concurrency_limit. That almost always means a session you started earlier is still running: don't retry in a loop, stop it and start again. When an avatar has to start first, it answers 202 with status: "starting" and retry_after: call again then (usually ready within 90 seconds). When every avatar is in a session and none can start, it answers 503 no_capacity: try again shortly. See Errors.