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

# Web Embed Tutorial

> Learn how to embed AI agents in your web applications

## Introduction

The PlayAI web embed allows you to integrate AI-powered interactions into your web application. This guide will walk you through the process of setting up and using the PlayAI web embed in your React application.

## Installation

First, install the `@play-ai/agent-web-sdk` package:

```bash theme={null}
npm install @play-ai/agent-web-sdk
```

## Basic Usage

Here's a step-by-step guide to implement the play.ai web embed in your React application:

<Steps>
  <Step title="Create an agent">
    If you haven't already, create an agent on [https://play.ai](https://play.ai).
  </Step>

  <Step title="Import the necessary dependencies">
    ```javascript theme={null}
    import { useEffect, useState } from 'react';
    import { open as openEmbed } from '@play-ai/agent-web-sdk';
    ```
  </Step>

  <Step title="Define your web embed ID">
    ```javascript theme={null}
    const webEmbedId = 'YOUR_WEB_EMBED_ID';
    ```

    Replace `YOUR_WEB_EMBED_ID` with the actual ID provided by play.ai, which can be found in the Agent editor.

    <Frame caption="Find your web embed ID on the last page of the Agent editor under 'Deploy Web'">
      <img src="https://mintcdn.com/playhtinc/oLfqv-LO9FzqCeTi/media/embedding-an-agent-on-your-website/web-embed-id.png?fit=max&auto=format&n=oLfqv-LO9FzqCeTi&q=85&s=be5a8d9bddec7bacfc99e32ce5deab7c" alt="Web Embed ID" width="1058" height="1278" data-path="media/embedding-an-agent-on-your-website/web-embed-id.png" />
    </Frame>
  </Step>

  <Step title="Define custom events (optional)">
    ```javascript theme={null}
    const events = [
      {
        name: "change-text",
        when: "The user says what they want to change the text on the screen to",
        data: {
          text: { type: "string", description: "The text to change to" },
        },
      },
    ] as const;
    ```

    Custom events are optional, but they allow you to define custom behavior for your agent, to allow it to execute javascript and interact with the page. Learn more about custom events <a target="_blank" href="/documentation/agent/web-embed#events-array">here</a>.

    This example defines a single event called "change-text" that will be triggered when the user specifies the text they want to display.
  </Step>

  <Step title="Implement the custom event handler (optional)">
    ```javascript theme={null}
    const onEvent = (event: any) => {
      console.log("onEvent: ", event);
      if (event.name === "change-text") {
        setText(event.data.text);
      }
    };
    ```

    If you define custom events, you must implement an event handler to handle the events. Learn more about the custom event handler <a target="_blank" href="/documentation/agent/web-embed#onevent-handler">here</a>.

    This handler logs the event and updates the text state when a "change-text" event is received.
  </Step>

  <Step title="Initialize the web embed">
    ```javascript theme={null}
    useEffect(() => {
      openEmbed(webEmbedId, { events, onEvent });
    }, []);
    ```

    This `useEffect` hook initializes the web embed when the component mounts. See all the parameters that can be passed to `openEmbed()` <a target="_blank" href="/documentation/agent/web-embed#openembed-function">here</a>.
  </Step>

  <Step title="Render the component">
    ```jsx theme={null}
    return (
      <>
        <div className="flex justify-center items-center h-[70vh]">
          <div className="font-medium text-2xl">{text}</div>
        </div>
      </>
    );
    ```

    This example renders the current text in the center of the page.
  </Step>
</Steps>

## Full Example

Here's the complete example of a React component using the play.ai web embed.

```jsx theme={null}
"use client";
import { useEffect, useState } from "react";
import { open as openEmbed } from "@play-ai/agent-web-sdk";

const webEmbedId = "YOUR_WEB_EMBED_ID";

export default function Home() {
  const [text, setText] = useState("Change this text");

  const events = [
    {
      name: "change-text",
      when: "The user says what they want to change the text on the screen to",
      data: {
        text: { type: "string", description: "The text to change to" },
      },
    },
  ] as const;

  const onEvent = (event: any) => {
    console.log("onEvent: ", event);
    if (event.name === "change-text") {
      setText(event.data.text);
    }
  };

  useEffect(() => {
    openEmbed(webEmbedId, { events, onEvent });
  }, []);

  return (
    <>
      <div className="flex justify-center items-center h-[70vh]">
        <div className="font-medium text-2xl">{text}</div>
      </div>
    </>
  );
}
```

View the live example [here](https://play.ai/embed/demo/change-text).

View the code [here](https://github.com/playht/web-embed-examples/blob/main/change-text/nextjs/app/page.tsx).

## Customization

You can customize the behavior of your AI agent by modifying the agent greeting and prompt. In this example, the agent is instructed to change the text on the page and end the call immediately after doing so.

<Frame caption="The greeting and prompt for the Change Text demo agent">
  <img src="https://mintcdn.com/playhtinc/oLfqv-LO9FzqCeTi/media/embedding-an-agent-on-your-website/greeting-and-prompt.png?fit=max&auto=format&n=oLfqv-LO9FzqCeTi&q=85&s=69ee109bc59968ded416a43dea02c4c4" alt="Web Embed ID" width="1044" height="1008" data-path="media/embedding-an-agent-on-your-website/greeting-and-prompt.png" />
</Frame>

## Other examples

* [View other examples](/documentation/tutorials/agent/web-embed-examples)
