# Embed SDK quickstart

## Embed the player (`@avatarstudio/embed`)

### Publish an experience

[Publish a revision](/documentation/en/guides/admin/experiences) in Avatar Studio. Copy the `slug` from the
`/s/{slug}` link and add your site's origin, such as `https://example.com`, to the
publication's `allowed_origins`.
### Prepare the integration

For TypeScript or React, add `@avatarstudio/embed` to your app’s NeuroStudio workspace dependencies.
Use the address of the Studio where you published your experience and replace the example slug with your own.
### Add the player and start a conversation

Embed the player directly with an iframe and the [host protocol](/documentation/en/guides/embed/bridge).

```html title="index.html"
<iframe id="avatar" title="Avatar Studio" allow="microphone; autoplay"
  style="width: 100%; height: 480px; border: 0"></iframe>
<button id="avatar-talk" type="button" disabled>Start conversation</button>

<script type="module">
  const studio = "https://avatarstudio.dev.digitallegends.ru";
  const slug = "your-publication-slug";
  const frame = document.querySelector("#avatar");
  const talk = document.querySelector("#avatar-talk");
  const url = new URL("/embed/" + encodeURIComponent(slug), studio);
  url.searchParams.set("hostOrigin", location.origin);

  function send(command) {
    frame.contentWindow.postMessage({
      protocol: "avatarstudio",
      version: 1,
      type: "host." + command,
      sessionId: slug,
      payload: {},
    }, studio);
  }

  window.addEventListener("message", ({ origin, source, data }) => {
    if (origin !== studio || source !== frame.contentWindow) return;
    if (data?.protocol !== "avatarstudio" || data.version !== 1 || data.sessionId !== slug) return;
    if (data.type === "ready") talk.disabled = false;
  });

  frame.addEventListener("load", () => send("configure"));
  talk.addEventListener("click", () => send("startListening"));
  frame.src = url.href;
</script>
```
Add the mount point and button to your page. Load the TypeScript module with your app’s bundler.

~~~html title="index.html"
<div id="avatar-host" style="width: 100%; height: 480px"></div>
<button id="avatar-talk" type="button" disabled>Start conversation</button>
<script type="module" src="/src/avatar.ts"></script>
~~~

```ts title="avatar.ts"
import { mount } from "@avatarstudio/embed";

const talk = document.querySelector<HTMLButtonElement>("#avatar-talk")!;
const embed = mount({
  platformUrl: "https://avatarstudio.dev.digitallegends.ru",
  token: "your-publication-slug",
  container: "#avatar-host",
});

const offReady = embed.on("ready", () => { talk.disabled = false; });
const start = () => embed.startListening();
talk.addEventListener("click", start);
const offTranscript = embed.on("transcript", (payload) => console.log(payload));

// Call this when removing the player.
export function disposeAvatar() {
  talk.removeEventListener("click", start);
  talk.disabled = true;
  offReady();
  offTranscript();
  embed.destroy();
}
```
Render `Avatar` with your `platformUrl` and publication `slug`.

```tsx title="Avatar.tsx"
import { mount, type AvatarStudioEmbed } from "@avatarstudio/embed";
import { useEffect, useRef, useState } from "react";

export function Avatar({ platformUrl, slug }: { platformUrl: string; slug: string }) {
  const host = useRef<HTMLDivElement>(null);
  const [readyPlayer, setReadyPlayer] = useState<AvatarStudioEmbed | null>(null);

  useEffect(() => {
    const container = host.current;
    if (!container) return;

    const embed = mount({ platformUrl, token: slug, container });
    const offReady = embed.on("ready", () => setReadyPlayer(embed));
    return () => {
      offReady();
      embed.destroy();
      setReadyPlayer((current) => current === embed ? null : current);
    };
  }, [platformUrl, slug]);

  return (
    <>
      <div ref={host} style={{ height: 480 }} />
      <button type="button" disabled={!readyPlayer} onClick={() => readyPlayer?.startListening()}>
        Start conversation
      </button>
    </>
  );
}
```

The button becomes available when the player is ready. The browser may ask for microphone permission when you press it.

See the generated [Embed SDK reference](/documentation/en/guides/embed/sdk-reference) for mount options and player methods.

Use `stopListening()` to turn off the microphone, `sendText(text)` to send a message,
and `interrupt()` or `reset()` to interrupt the avatar or start a fresh conversation.
See [client tools](/documentation/en/guides/embed/tool-calls) for tool handlers and the
[embed protocol](/documentation/en/guides/embed/bridge) for managing kiosk visits and viewport changes.

## Manage sessions directly (`@avatarstudio/client`)

Use the HTTP client when you run your own player or need to manage the LiveKit
session lifecycle directly.

```ts title="session.ts"
import { createSession, getSessionState, closeSession } from "@avatarstudio/client";

const platformUrl = "https://avatars.digitallegends.ru";
const session = await createSession({
  deploymentId: "your-publication-slug",
  platformUrl,
});

// Connect to LiveKit using session.url and session.token.
if (session.closeToken) {
  const state = await getSessionState({ closeToken: session.closeToken, platformUrl });
  console.log("Queued:", state.queued, state.position);
  await closeSession({ closeToken: session.closeToken, platformUrl });
}
```
