Websocket API
Enhance your app with our audio-in, audio-out API, enabling seamless, natural conversations with your PlayAI agent. Transform your user experience with the power of voice.
To use our WebSocket, you will need beforehand:
- A Play.ai account
- An API key to authenticate with the Play.ai API
- An agent ID of a Play.ai Agent (created via our Web UI or our Create Agent endpoint)
To fully leverage our WebSocket API, the steps are:
- Connect to our
wss://api.play.ai/v1/talk/<your_agent_id>
URL - Send a
{"type":"setup","apiKey":"yourKey"}
message as first message - Send audio input as base64 encoded string in
{"type":"audioIn","data":"base64Data"}
messages - Receive audio output in
{"type":"audioStream","data":"base64Data"}
messages
Establishing a Connection
To initiate a conversation, establish a websocket connection to our talk
URL, including the agentId
as a path parameter:
For example, assuming Agent-XP5tVPa8GDWym6j
is the ID of an agent
you have created via our Web UI or through our Create Agent endpoint,
the WebSocket URL should look like:
Initial Setup Message
Before you can start sending and receiving audio data, you must first send a setup
message to authenticate
and configure your session.
WebSocket basic connection, setup and message flow
The only required field in the setup message is the apiKey
. This assumes you are comfortable with the default
values for audio input and audio output formats. In this scenario, your first setup message could be as simple as:
Code example:
Setup Options
The setup message configures important details of the session, including the format/encoding of the audio that you intend to send us and the format that you expect to receive.
The following fields are available for configuration:
Property | Accepted values | Description | Default value |
---|---|---|---|
type (required) | "setup" | Specifies that the message is a setup command. | - |
apiKey (required) | string | Your API Key. | - |
outputFormat (optional) |
| The format of audio you want our agent to output in the
| "mp3" |
outputSampleRate (optional) | number | The sample rate of the audio you want our agent to output in the audioStream messages | 44100 |
inputEncoding (optional) | For non-headerless formats:
For headerless formats:
| The encoding of the audio you intend to send in the If your are sending audio formats that use media containers (that is, audio that contain headers, such as If, on the other hand, you will send us audio in headerless formats, you have to specify the format you will
be sending. In this case, specify it by, e.g., setting | "media-container" |
inputSampleRate (optional) | number | The sample rate of the audio you intend to send. Required if you are specifying an inputEncoding different
than "media-container" . Optional, otherwise | - |
customGreeting (optional) | string | Your agent will say this message to start every conversation. This overrides the agent’s greeting. | - |
prompt (optional) | string | Give instructions to your AI about how it should behave and interact with others in conversation. This is appended to the agent’s prompt. | "" |
continueConversation (optional) | string | If you want to continue a conversation from a previous session, pass the | - |
audioIn
: Sending Audio Input
After the setup, you can send audio input in the form of an audioIn
message.
The audio must be sent as a base64 encoded string in the data
field. The message format is:
inputEncoding
and inputSampleRate
you configured in the setup options.Sample Code for Sending Audio
Assuming myWs
is a WebSocket connected to our /v1/talk
endpoint, the sample code below would
send audio directly from the browser:
audioStream
: Receiving Audio Output
Audio output from the server will be received in an audioStream
message. The message format is:
The audio you receive will match the outputFormat
and outputSampleRate
you configured in the setup options.
Sample Code for Receiving Audio
Voice Activity Detection: voiceActivityStart
and voiceActivityEnd
During the conversation, you will receive voiceActivityStart
and voiceActivityEnd
messages indicating the detection
of speech activity in the audio input. These messages help in understanding when the user starts and stops speaking.
When our service detects that the user started to speak, it will emit a voiceActivityStart
event.
Such a message will have the format:
It is up to you to decide how to react to this event.
We highly recommend you stop playing whatever audio is being played, since the voiceActivityStart
generally indicates
the user wanted to interrupt the agent.
Similarly, when our service detects that the user stopped speaking, it emits a voiceActivityEnd
event:
newAudioStream
: Handling New Audio Streams
A newAudioStream
message indicates the start the audio of a new response.
It is recommended to clear your player buffer and start playing the new stream content upon receiving this message.
This message contains no additional fields.
Error Handling
Errors from the server are sent as error
message type, a numeric code and a message in the following format:
The table below provides a quick reference to the various error codes and their corresponding messages for the Agent Websocket API.
Error Code | Error Message |
---|---|
1001 | Invalid authorization token. |
1002 | Invalid agent id. |
1003 | Invalid authorization credentials. |
1005 | Not enough credits. |
4400 | Invalid parameters. Indicates the message sent to the server failed to match the expected format. Double check the logic and try again. |
4401 | Unauthorized. Invalid authorization token or invalid authorization credentials for specified agent. |
4429 | You have reached the maximum number of concurrent connections allowed by your current plan. Please consider upgrading your plan or reducing the number of active connections to continue. |
4500 | Generic error code for internal errors, such as failures to generate responses. Generally, the user is not at fault when these happen. An appropriate reaction is to wait a few moments and try again. If the problem persists, contacting support is advised. |
This documentation covers the essential aspects of interacting with the PlayAI Websocket API for agent conversations. Ensure that your implementation handles the specified message types and follows the outlined protocols for a seamless integration.