Web Embed API
API for embedding a play.ai agent on your website
This document provides detailed information about the key components of the play.ai web embed API: the events array, the onEvent handler, and the openEmbed function.
Installation
openEmbed
Function
The openEmbed
function initializes and opens the play.ai web embed on the webpage. It is imported from the @play-ai/web-embed
package as follows
It has the following signature:
Parameters
webEmbedId
: A string representing your unique web embed ID provided by play.ai.options
: An object containing:events?
: The array of custom events your application can handle.onEvent?
: The event handler function.customGreeting?
: A custom greeting that replaces the default greeting.prompt?
: A prompt to give the agent in addition to the default prompt. Use this to give context that is page-specific or user-specific, e.g. “The form fields on the current page are X, Y, and Z”.
Return type
setMinimized(minimize?: boolean)
: A function that allows you to toggle the minimized state of the web embed. Pass intrue
to minimize andfalse
to maximize the web embed. Toggle the minimize state by passing inundefined
.
Example
In this example, the openEmbed function is called inside a useEffect hook to initialize the web embed when the component mounts.
Events Array
The events array defines the custom events that your application can handle. Each event in the array is an object with the following structure:
Properties
name
: A string that uniquely identifies the event.when
: A string describing the condition that triggers this event.data
: An object describing the data that should be passed to the event handler. Each key in this object represents the name of the data field, and its value is an object with:type
: The data type of the field (currently supportsstring
,number
,boolean
, andenum
).description?
: A brief description of what this data field represents.values?
: An array of strings representing the possible values for the field if the type isenum
.
Example
onEvent Handler
The onEvent handler is a function that processes events triggered by the play.ai web embed. It has the following signature:
Parameters
event
: An object containing:name
: The name of the triggered event (matching an event name from the events array).data
: An object containing the data associated with the event (matching the data structure from the events array).
Example
In this example, the handler logs all events and updates the text state when a “change-text” event is received.
Putting It All Together
Here’s how these components work together:
- You define your custom events in the events array.
- You implement the onEvent handler to process these events.
- You call the openEmbed function, passing your web embed ID, the events array, the onEvent handler, and optionally customGreeting and prompt.
- When a user interacts with the AI agent, it may trigger one of your defined events.
- The onEvent handler receives the triggered event and processes it according to your implementation.
This structure allows for flexible, event-driven interactions between your web application and the play.ai web embed.
Learn more about how to use the web embed API in this guide.