Skip to main content

Realtime API

A session is a live, two-way conversation with an identity. POST /v1/sessions creates it: it sets up a LiveKit room, brings in Lookalike's agent, and returns the session with what your app needs to join under transport: a server url and an access token.

For a file rendered ahead of playback, generate a video instead.

Prerequisite

You need an active identity. See Create an Identity.

Start a session​

curl -sS -X POST https://api.lookalike.com/v1/sessions \
-H "Authorization: Bearer $LOOKALIKE_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "identity_id": "id_3f9a2b7c4d1e8a05f6b3" }'
{
"id": "sess_4b9e02f1c7a85d3e6f10",
"object": "session",
"agent_mode": "managed",
"status": "pending",
"end_reason": null,
"error": null,
"identity_id": "id_3f9a2b7c4d1e8a05f6b3",
"modality": "video",
"avatar_id": "avt_1a2b3c4d5e6f7a8b9c0d",
"voice_id": "voc_9f8e7d6c5b4a39281706",
"avatar_model": "animatron-2",
"transport": {
"type": "livekit",
"url": "wss://your-livekit-server",
"room_name": "sess_4b9e02f1c7a85d3e6f10",
"token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
},
"external_agent": null,
"limits": { "max_duration_seconds": 3600 },
"usage": { "items": [], "amount_usd": 0 },
"created_at": "2026-11-11T12:00:00Z",
"started_at": null,
"ended_at": null
}
Field
identity_idThe identity to talk to. Required.
agent_modemanaged (default) or hybrid. Optional. See Bring your own agent. For external, see Bring your own agent and room.
modalitytext, audio or video (default). Optional.
languageauto, en, es, pl or uk. Overrides the identity's language for this session. Optional.
transport{ "type": "livekit" }, the only value and the default. Optional.
limitsmax_duration_seconds: the session ends when it is reached. Default 3600, at most 7200. Optional.
videoFor video: aspect_ratio (9:16 renders portrait, 16:9 landscape) and resolution (accepted, not applied yet). Optional.

The avatar and voice come from the identity. To change them, update the identity or send another identity_id. The identity's instructions are not applied to a session yet: the agent speaks as a general assistant. To run your own prompt and tools today, use an external session.

Modality​

  • video — the avatar, with its voice. It publishes audio and video tracks.
  • audio — voice only.
  • text — typed chat, no audio tracks. Send the visitor's messages on LiveKit's lk.chat text stream; replies arrive on lk.transcription.

Conversation language​

The session's language wins; without it, the identity's stored language is used. A session override never changes the identity.

A pinned language is strict: the agent answers only in it, even when the visitor switches. auto greets in English and then follows the visitor.

Every language except en, auto included, needs an identity with a multilingual voice for audio and video. Without one the call returns 422 (voice_required) rather than reading the words with an English-only voice.

Errors​

StatusMeaning
401Missing or invalid API key.
402Your balance can't cover a video session. detail.code is insufficient_balance, allowance_exhausted or spending_cap_reached. Checked before anything is allocated.
404No such identity, or it isn't your organization's.
409identity_not_ready: a video session on an identity with no avatar ready.
422The request is invalid, for example limit_exceeded or voice_required.
429concurrency_limit: your organization has its maximum of open sessions (default 10). Retry after the Retry-After header (30 s).
503High demand. Retry shortly.

Error bodies carry detail: { code, message }.

Join the room​

Connect with any LiveKit client SDK using transport.url and transport.token. The token is minted server-side and already encodes the room, so your browser never sees your API key. It is returned only on create; later reads of the session have token: null.

import { Room, RoomEvent, Track } from 'livekit-client';

// livekitUrl and token are transport.url and transport.token from the POST /v1/sessions response
const room = new Room();

room.on(RoomEvent.TrackSubscribed, (track, publication, participant) => {
if (track.kind === Track.Kind.Video) {
// attach the avatar video track to a view element
document.getElementById('avatar').appendChild(track.attach());
} else if (track.kind === Track.Kind.Audio) {
// play the agent audio track
document.body.appendChild(track.attach());
}
});

await room.connect(livekitUrl, token);

// enable the local mic so the agent can hear the user
await room.localParticipant.setMicrophoneEnabled(true);

Status and usage​

status moves pending (created, the agent hasn't joined yet) → running (the agent joined) → ended. Once ended, end_reason says why:

end_reason
apiYou called POST /v1/sessions/{id}/end.
client_leftEveryone left the room.
max_durationlimits.max_duration_seconds was reached.
errorSomething failed; see error: { code, message }.
insufficient_balance, allowance_exhausted, spending_cap_reachedBilling stopped the session.

usage.items lists what the session was charged, as { meter, quantity, amount_usd }; usage.amount_usd is the total. A video session bills video_second for the seconds its avatar delivered. Text and audio sessions are free today.

End the session​

A session ends when everyone leaves the room. To end it from your backend, without waiting for the client:

curl -sS -X POST https://api.lookalike.com/v1/sessions/sess_4b9e02f1c7a85d3e6f10/end \
-H "Authorization: Bearer $LOOKALIKE_API_KEY"

The room closes, every participant is disconnected and billing stops. The response is the session, ended with end_reason: "api". Ending an ended session returns it unchanged, so it is safe to call after the client has left.

List and fetch​

  • GET /v1/sessions lists your sessions, newest first. Filters: agent_mode, identity_id, status, since; paging with cursor and limit. Returns { data, next_cursor }.
  • GET /v1/sessions/{id} fetches one session.

Bring your own agent (hybrid)​

A hybrid session puts your own agent in the room alongside ours. Lookalike's agent stays the voice; yours can exchange messages with it over the data channel. Send the managed fields plus external_agent:

curl -sS -X POST https://api.lookalike.com/v1/sessions \
-H "Authorization: Bearer $LOOKALIKE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"agent_mode": "hybrid",
"identity_id": "id_3f9a2b7c4d1e8a05f6b3",
"external_agent": { "participant_identity": "my-agent" }
}'

The response's external_agent.token lets your agent join the same room at transport.url as an agent participant named participant_identity. Like transport.token, it is returned only on create.

Bring your own agent and room (external)​

An external session puts our avatar in your LiveKit room, in front of your own agent. Lookalike starts its renderer, which joins your room as the avatar participant and publishes the avatar's video and audio on behalf of your agent (LiveKit's avatar pattern). Your agent owns the conversation and speaks with its own voice. There is no socket to hold and no worker URL.

Mint a token for the avatar participant with your LiveKit API key: any identity other than your agent's (the plugins use lookalike-avatar-agent), kind agent, a join grant for exactly one room, the attribute lk.publish_on_behalf set to your agent's participant identity, and an expiry within one hour.

from datetime import timedelta
from livekit import api

token = (
api.AccessToken(LIVEKIT_API_KEY, LIVEKIT_API_SECRET)
.with_identity("lookalike-avatar-agent")
.with_kind("agent")
.with_grants(api.VideoGrants(room_join=True, room=room))
.with_attributes({"lk.publish_on_behalf": agent_identity})
.with_ttl(timedelta(minutes=10))
.to_jwt()
)

Always set the lifetime yourself: LiveKit tokens last six hours unless you say otherwise, and a token that expires more than an hour after your call is refused with 422 (invalid_livekit_token). Lookalike never sees your LiveKit secret; it reads the token's claims and hands the token to the renderer.

transport.url must be your own LiveKit project, in exactly this form, or the call returns 422 (invalid_livekit_url):

  • the scheme is wss, as in wss://my-project.livekit.cloud;
  • the port is 443, which is the default, so leave it out;
  • there is no path, query string or credentials;
  • the host resolves only to public addresses;
  • it is not Lookalike's own LiveKit server.

Then start the session with that token and your project's URL:

curl -sS -X POST https://api.lookalike.com/v1/sessions \
-H "Authorization: Bearer $LOOKALIKE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"agent_mode": "external",
"identity_id": "id_3f9a2b7c4d1e8a05f6b3",
"transport": { "type": "livekit", "url": "wss://your-project.livekit.cloud", "token": "eyJ..." }
}'
Field
agent_modeexternal. Required.
identity_id, avatar_image_url or avatar_imageThe face. Exactly one is required.
transporttype: "livekit", url and token. Required. url is wss://<host> of your LiveKit project on port 443, with no path, query or credentials, resolving to public addresses.
avatar_modelanimatron-2, the only value. Optional.
limitsmax_duration_seconds, default and at most 7200 (two hours). Optional.
videoaspect_ratio: 9:16 portrait or 16:9 landscape. Optional.

With identity_id only the identity's face is used; its voice and instructions don't apply. avatar_image_url is an https URL on port 443 of a public host. It is fetched once, following up to 3 redirects. The image must be JPEG, PNG or WEBP, at most 20 MB and 40 megapixels. It becomes one of your avatars, free; the same image is the same avatar next time. To upload the image instead, send avatar_image as multipart, with the other fields as form fields and transport, limits and video as JSON strings:

curl -sS -X POST https://api.lookalike.com/v1/sessions \
-H "Authorization: Bearer $LOOKALIKE_API_KEY" \
-F "agent_mode=external" \
-F "avatar_image=@face.png" \
-F 'transport={"type":"livekit","url":"wss://your-project.livekit.cloud","token":"eyJ..."}'

The response is the session with agent_mode: "external", modality: null, voice_id: null, identity_id (null for an image), avatar_id, and transport with your url, the room_name from your token, and token: null: your token is never echoed. status becomes running once the avatar has joined your room and ended when it leaves. end_reason is agent_left (your agent or room went away), api, max_duration, insufficient_balance, or error. On error, error.code is one of start_timeout (the avatar didn't join within 5 minutes), renderer_lost, avatar_unavailable, agent_absent, room_connect_failed, avatar_load_failed, invalid_livekit_token, invalid_livekit_url, billing_unavailable, renderer_error, session_closed or unsupported_protocol. There is no transcript: your agent owns the conversation.

POST /v1/sessions/{id}/end ends it: the avatar leaves within about 15 seconds, and the seconds delivered until then are charged. An external session bills video_second at $0.002 a second, after your plan's included external seconds; see Billing.

StatusMeaning
402Your balance can't cover the 30-second minimum. Nothing starts.
403shopify_billed: organizations billed through Shopify can't start external sessions.
403terms_not_accepted: someone in your organization must first accept the avatar terms on the Avatar sessions page of the Console.
404No such identity, or it isn't your organization's.
409identity_not_ready: the identity has no avatar image.
422invalid_avatar_source (not exactly one face), invalid_livekit_url, invalid_livekit_token, invalid_avatar_image_url, avatar_image_unreachable, avatar_image_too_large, unsupported_avatar_image, avatar_model_unavailable or limit_exceeded.
429concurrency_limit: external sessions count toward the same open-session limit.
503avatar_unavailable: external sessions are not open in this environment yet, or the renderer couldn't be started. Nothing is charged.

End-to-end example​

import os

import httpx
from livekit import rtc

BASE_URL = "https://api.lookalike.com"
API_KEY = os.environ["LOOKALIKE_API_KEY"]


async def start_session() -> rtc.Room:
"""Start a session and join its room with the returned token."""
async with httpx.AsyncClient() as client:
resp = await client.post(
f"{BASE_URL}/v1/sessions",
headers={"Authorization": f"Bearer {API_KEY}"},
json={"identity_id": "id_3f9a2b7c4d1e8a05f6b3"},
)
resp.raise_for_status()
session = resp.json()

room = rtc.Room()
await room.connect(session["transport"]["url"], session["transport"]["token"])
return room

Notes​

  • Avatar audio. In a video session the avatar publishes both audio and video. Don't also play the agent's raw voice track, or you'll hear it twice.
  • One token per session. Start a new session for each conversation.
  • Not yet available: transcript, control and events routes, previous_session_id, per-session instructions, greeting and dynamic_variables, embed tokens, and Idempotency-Key.