Web SDK
Integrate Vapi into your web application.
The Vapi Web SDK provides web developers a simple API for interacting with the realtime call functionality of Vapi.
Installation
Install the package:
or w/ npm:
Importing
Import the package:
Then, create a new instance of the Vapi class, passing your Public Key as a parameter to the constructor:
You can find your public key in the Vapi Dashboard.
Usage
.start()
You can start a web call by calling the .start()
function. The start
function can either accept:
- a string, representing an assistant ID
- an object, representing a set of assistant configs (see Create Assistant)
The start
function returns a promise that resolves to a call object. For example:
Passing an Assistant ID
If you already have an assistant that you created (either via the Dashboard or the API), you can start the call with the assistant’s ID:
Passing Assistant Configuration Inline
You can also specify configuration for your assistant inline.
This will not create a persistent assistant that is saved to your account, rather it will create an ephemeral assistant only used for this call specifically.
You can pass the assistant’s configuration in an object (see Create Assistant for a list of acceptable fields):
Overriding Assistant Configurations
To override assistant settings or set template variables, you can pass assistantOverrides
as the second argument.
For example, if the first message is “Hello {{name}}
”, set assistantOverrides
to the following to replace {{name}}
with John
:
.send()
During the call, you can send intermediate messages to the assistant (like background messages).
type
will always be"add-message"
- the
message
field will have 2 items,role
andcontent
.
Possible values for role are system
, user
, assistant
, tool
or
function
.
.stop()
You can stop the call session by calling the stop
method:
This will stop the recording and close the connection.
.isMuted()
Check if the user’s microphone is muted:
.setMuted(muted: boolean)
You can mute & unmute the user’s microphone with setMuted
:
say(message: string, endCallAfterSpoken?: boolean)
The say
method can be used to invoke speech and gracefully terminate the call if needed
Events
You can listen on the vapi
instance for events. These events allow you to react to changes in the state of the call or user speech.
speech-start
Occurs when your AI assistant has started speaking.
speech-end
Occurs when your AI assistant has finished speaking.
call-start
Occurs when the call has connected & begins.
call-end
Occurs when the call has disconnected & ended.
volume-level
Realtime volume level updates for the assistant. A floating-point number between 0
& 1
.
message
Various assistant messages can be sent back to the client during the call. These are the same messages that your server would receive.
At assistant creation time, you can specify on the clientMessages
field the set of messages you’d like the assistant to send back to the client.
Those messages will come back via the message
event:
error
Handle errors that occur during the call.