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

# Daily Integration

> Connect your LemonSlice-managed avatar to a [Daily](https://www.daily.co) room you can embed anywhere.

> [!NOTE]
>
> Check out our [hosted Daily starter project](https://github.com/lemonsliceai/lemonslice-examples/tree/main/01-hosted-daily-app) to see how you can connect your LemonSlice-managed avatar to a [Daily](https://www.daily.co) room in a simple web app.

## Getting Started

Follow these steps to create a Daily room that your LemonSlice avatar joins, and embed it into your application.

#### 1. Get your API key

Retrieve your API key from the LemonSlice [developers page](https://lemonslice.com/developers).

All API requests must include this header:

```
X-API-Key: YOUR_API_KEY
```

#### 2. Create a room

Use the `POST /liveai/rooms` endpoint to create a Daily room configured with a LemonSlice avatar. The avatar will automatically join the room once its startup is complete.

```bash BASH theme={null}
curl -X POST https://lemonslice.com/api/liveai/rooms \
  -H "X-API-Key: YOUR_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{ "agent_id": "YOUR_AGENT_ID" }'
```

#### 3. Join the room from your frontend

Use [Daily’s JavaScript SDK](https://docs.daily.co/reference/daily-js) to join the room with the URL and token returned by the API.

```javascript JAVASCRIPT theme={null}
import DailyIframe from "@daily-co/daily-js";

const callObject = DailyIframe.createCallObject();

await callObject.join({
  url: room_url,
  token: token
});
```

Once joined, the LemonSlice avatar will connect and begin responding in the room.

#### 4. Listen for LemonSlice events

LemonSlice uses Daily’s `app-message` channel to push events about the avatar, image updates, and errors.

##### Example event handler

```javascript JAVASCRIPT theme={null}
useDailyEvent("app-message", (ev) => {
  if (ev?.data?.type === "bot_ready") {
    console.log("Avatar is ready!");
  }
  if (ev?.data?.type === "daily_error") {
    console.log("An error occurred. ", ev?.data?.err_msg);
  }
});
```

#### You're ready to build

With a room created, the avatar connected, and event hooks wired up, you're ready to:

* Build interactive video AI apps
* Customize avatar behavior
* Trigger image changes and respond to events

Explore the rest of the API docs for more capabilities.

## Receiving Events

Hook into the Daily event handler to get LemonSlice-specific events.

| Event Type | Description |
| :- | :- |
| `bot_ready` | Fired when the LemonSlice avatar has successfully joined the Daily room and is ready to send and receive audio and video. |
| `idle_timeout` | Emitted when the avatar session stops due to inactivity. The idle timeout threshold is defined by the avatar’s configuration. |
| `user_transcription` | Emitted when the user's speech has been finalized. Includes the field `transcription`. This event also captures messages sent by the user via text. |
| `agent_transcription` | Emitted when the avatar's spoken response has been finalized. Includes the field `transcription`. |
| `image_change_requested` | A new avatar look has been requested, typically after an `/imagine` request is made. |
| `image_created` | A new avatar look has been created and is available for use. |
| `image_change_complete` | The avatar’s look has finished updating and the new look is now active. |
| `image_change_error` | The avatar failed to update its look. Includes an `error` field with additional details. |
| `daily_error` | A Daily-related error occurred. Includes `error` and `fatal` fields. |
| `video_generation_error` | A failure occurred while generating a video segment or rendering avatar output. |

#### Full event handler

```javascript JAVASCRIPT theme={null}
useDailyEvent(
  "app-message",
  useCallback((ev: DailyEventObjectAppMessage) => {
    if (ev?.data?.type === "bot_ready") {
      console.log("Avatar has joined the room")
    }
    if (ev?.data?.type === "idle_timeout") {
      console.log("Avatar has timed out")
    }
    if (ev?.data?.type === "user_transcription") {
      console.log("Latest user transcription: ", ev?.data?.transcription);
    }
    if (ev?.data?.type === "agent_transcription") {
      console.log("Latest avatar transcription: ", ev?.data?.transcription);
    }
    if (ev?.data?.type === "image_change_requested") {
      console.log("Request to edit avatar image has been received")
    }
    if (ev?.data?.type === "image_created") {
      console.log("New base image for avatar created")
    }
    if (ev?.data?.type === "image_change_complete") {
      console.log("Updated avatar image is ready")
    }
    if (ev?.data?.type === "image_change_error") {
      console.log("Failed to update avatar image", ev?.data?.error)
    }
    if (ev?.data?.type === "daily_error") {
      console.log("An error occurred. ", ev?.data?.error, ev?.data?.fatal);
    }
    if (ev?.data?.type === "video_generation_error") {
      console.log("A video generation error occurred.");
    } 
  }, [])
);
```

## Control Events

Send events through Daily to control the LemonSlice avatar.

| Control Event | Description |
| :- | :- |
| `chat-msg` | Send a message to the avatar for it to respond to. Also supports triggering avatar image changes via `/imagine` (see [Avatar image changes with `/imagine`](#imagine)). |
| `force-end` | Immediately stop the realtime avatar. The avatar will leave the room and billing will stop. |

#### Sending Events

```javascript JAVASCRIPT theme={null}
sendAppMessage(
  {
    event: "chat-msg",
    message: "Your message here",
    name: "User",
  },
  "*",
);

sendAppMessage(
  { event: "force-end" },
  "*",
);
```

#### Avatar image changes with `/imagine`

The chat-msg control event also supports avatar image updates using a special `/imagine` prefix.\
When a message begins with `/imagine`, LemonSlice interprets the remainder of the message as an image prompt and updates the avatar accordingly.

##### **Example**

```javascript JAVASCRIPT theme={null}
sendAppMessage(
  {
    event: "chat-msg",
    message: "/imagine being in front of a fireplace",
    name: "User",
  },
  "*",
);
```

##### **How it works**

* Messages starting with `/imagine` **do not trigger a spoken response**
* Instead, the avatar image is regenerated based on the prompt
* The avatar retains its core identity and proportions, while updating the visual context
* Once the image update completes, the avatar continues normal interaction
  \[!NOTE] **Avatar setup requirement:**`Allow users to modify appearance` must be enabled in the avatar configuration. This setting is *enabled by default for all newly created avatars.*

## Additional resources

> [card] [Starter project](https://github.com/lemonsliceai/lemonslice-examples/tree/main/01-hosted-daily-app)

> [card] [REST Endpoints](https://lemonslice.com/docs/api-reference/create-room.md)

> [card] [Daily Documentation](https://docs.daily.co)


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