> ## Documentation Index
> Fetch the complete documentation index at: https://docs.simli.com/llms.txt
> Use this file to discover all available pages before exploring further.

# JavaScript SDK

> Show a Simli avatar in the browser with simli-client

[simli-client](https://github.com/simliai/simli-client) connects a web page to Simli. You send it the audio the avatar should speak, and it plays the avatar's lip-synced video and voice in a `<video>` and an `<audio>` element.

> [!TIP]
>
> Building a voice agent? [LiveKit Agents](https://docs.simli.com/api-reference/livekit.md) and [Pipecat](https://docs.simli.com/api-reference/pipecat.md) run the agent and Simli on the server. Use this SDK when the avatar's audio is already in the browser.

## What you need

* A Simli API key, from your [Simli dashboard](https://app.simli.com)
* A backend route that creates session tokens. Your API key stays there and never goes in the browser.

## Install

```bash theme={null}
npm install simli-client
```

## 1. Add video and audio elements

With React, create the elements and keep refs to them:

```tsx theme={null}
import { useRef } from "react";

function YourComponent() {
	const videoRef = useRef<HTMLVideoElement>(null);
	const audioRef = useRef<HTMLAudioElement>(null);

	return (
		<div>
			<video ref={videoRef} autoPlay playsInline></video>
			<audio ref={audioRef} autoPlay></audio>
		</div>
	);
}
```

Without React, look the elements up in the DOM:

```typescript theme={null}
const videoElement = document.getElementById("video") as HTMLVideoElement;
const audioElement = document.getElementById("audio") as HTMLAudioElement;
```

## 2. Create a session token on your server

Each session starts with a session token. Creating one takes your API key, so do it on your server and send only the token to the browser. This route is for Next.js (App Router). On other backends, POST the same config to [`POST /compose/token`](https://docs.simli.com/api-reference/compose-session-token) with your key in the `x-simli-api-key` header.

```typescript app/api/simli-token/route.ts theme={null}
import { generateSimliSessionToken } from "simli-client";

// Runs on your server, so SIMLI_API_KEY never reaches the browser.
export async function POST() {
	const { session_token } = await generateSimliSessionToken({
		apiKey: process.env.SIMLI_API_KEY!,
		config: {
			faceId: "14c60d62-6ebf-424d-b626-1f4f91c33b04", // Simli's default Trinity-2 face
			maxSessionLength: 600, // seconds
			maxIdleTime: 180, // seconds
		},
	});
	return Response.json({ session_token });
}
```

To use another avatar, replace the `faceId` with one from your [dashboard](https://app.simli.com) or the [default faces](https://docs.simli.com/api-reference/preset-faces.md). For the video size, the avatar's position, the starting camera and backgrounds, see [Trinity-2](https://docs.simli.com/api-reference/javascript/trinity-2.md).

> [!WARNING]
>
> Anyone who can reach your token route can start a session on your account. Put it behind your own sign-in or rate limiting before you go to production.

## 3. Connect

In the browser, get a token from your route, create a `SimliClient` and call `start()`:

```typescript theme={null}
import { SimliClient, LogLevel } from "simli-client";

async function startSimli(videoElement: HTMLVideoElement, audioElement: HTMLAudioElement): Promise<SimliClient> {
	const res = await fetch("/api/simli-token", { method: "POST" });
	if (!res.ok) throw new Error(`Couldn't create a Simli session token: ${res.status}`);
	const { session_token } = await res.json();

	const simliClient = new SimliClient(session_token, videoElement, audioElement, null, LogLevel.INFO);
	simliClient.on("stop", () => console.log("Session ended"));
	await simliClient.start(); // resolves once the avatar's first video frame is on screen
	return simliClient;
}
```

Create the client just before you call `start()`, because the constructor already opens the connection, and register your event handlers before `start()`. A `SimliClient` runs one session. To start another, get a new token and create a new client.

## 4. Send audio

Send the avatar's speech as raw PCM16 audio: 16 kHz, mono. Chunks of about 6000 bytes work well.

```typescript theme={null}
// audioData: a Uint8Array of PCM16, 16 kHz, mono audio
simliClient.sendAudioData(audioData);
```

To stop the avatar mid-sentence, for example when the user interrupts, drop the audio it hasn't spoken yet:

```typescript theme={null}
simliClient.ClearBuffer();
```

To stream from a live source instead, pass it to `listenToMediastreamTrack(track)` (a `MediaStreamTrack`, such as a microphone or a WebRTC track from your voice bot) or `listenToAudioElement(audioElement)` (an `<audio>` element). The SDK converts the audio and sends it for you.

### Choosing `handleSilence`

`handleSilence` in the session token config (default `true`) decides what Simli does when your audio runs out:

* **`true`**: Simli fills the gaps with silence, so the avatar keeps moving naturally between sentences and picks up again when you send more. Keep it on when you send audio in bursts with `sendAudioData`, such as one TTS response at a time.
* **`false`**: Simli plays only the audio you send, and the video freezes when you stop sending. Turn it off when you stream continuous audio with `listenToMediastreamTrack` or `listenToAudioElement`. Those already send their own silence, and Simli's gap filling would cut into it and cause audio artifacts.

## 5. Stop

```typescript theme={null}
await simliClient.stop();
```

## Errors and retries

`start()` resolves once the avatar's first video frame is on screen. If it fails, what happens depends on why:

* **Simli refuses the session**, for example because the session token has expired, the face ID is unknown, your account is out of minutes, or you hit a rate limit. The `error` event fires with Simli's message, and `start()` rejects at once with the same message, a string such as `"ERROR: INVALID_FACE_ID"`. It doesn't retry, since a retry would fail the same way.
* **The network fails**: the connection can't be opened or drops, or an attempt takes more than 15 seconds. These are the only failures `start()` retries, up to 10 times, with exponential backoff: it waits 1, 2, 4 and 8 seconds before the first four retries, then 16 seconds before each one after that, each wait plus a little random jitter. Then it rejects with `Error("Too Many Retry Attempts Failed to connect")`. Your event handlers stay registered across retries.
* **Anything else goes wrong before the first frame**, such as a message from Simli the SDK doesn't recognise. `start()` rejects at once with that message as is, and the SDK logs it as `Unknown error from Simli: <message>`. Once the session has started, an unrecognised message doesn't end it: it fires the `unknown` event and is logged as a warning.

On your server, `generateSimliSessionToken` throws the API's response body if it answers with an error, for example for an invalid face ID, no minutes left or an unsupported video size (`422`).

```typescript theme={null}
try {
	simliClient = await startSimli(videoRef.current!, audioRef.current!);
} catch (reason) {
	console.error("Simli couldn't start:", reason);
}
```

## Peer-to-peer mode (optional)

The default transport needs nothing from you. If you'd rather connect the browser straight to Simli over WebRTC, there's a lower-level peer-to-peer mode. It can save a little latency, but strict firewalls can block it, and it needs ICE servers. Fetching them takes your API key, so add a second route on your server:

```typescript app/api/simli-ice/route.ts theme={null}
import { generateIceServers } from "simli-client";

export async function GET() {
	// Falls back to a public STUN server if Simli's can't be fetched
	return Response.json(await generateIceServers(process.env.SIMLI_API_KEY!));
}
```

Then pass the ICE servers and `"p2p"` to `SimliClient`:

```typescript theme={null}
const iceServers: RTCIceServer[] = await (await fetch("/api/simli-ice")).json();
const simliClient = new SimliClient(session_token, videoElement, audioElement, iceServers, LogLevel.INFO, "p2p");
await simliClient.start();
```

If three peer-to-peer attempts in a row fail, `start()` makes its remaining retries with the default transport.

## Next steps

> [card] [Trinity-2](https://docs.simli.com/api-reference/javascript/trinity-2.md)
>
> Video size, the avatar's position, the camera, backgrounds and foregrounds.

> [card] [API reference](https://docs.simli.com/api-reference/javascript/reference.md)
>
> Every function, method, event and type.

## Troubleshooting

1. Check the reason `start()` rejected with, or the `error` event's message.
2. Check that your API key is correct and active, and that your face ID is valid.
3. Check that your audio is PCM16, 16 kHz, mono.
4. Check that the page has permission to use the microphone, if you stream it.

Reach out to our team on [Discord](https://discord.gg/yQx49zNF4d), or [fork simli-client](https://github.com/simliai/simli-client) for your own use case.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.