Pizza Website Example 🍕

Let’s build a pizza ordering assistant for our website.

In this example, we’ll be using the Web SDK to create an assistant that can take a pizza order. Since all the Client SDKs have equivalent functionality, you can use this example as a guide for any Vapi client.

We want to add a button to the page to start a call, update our UI with the call status, and display what the user’s saying while they say it. When the user mentions a topping, we should add it to the pizza. When they’re done, we should redirect them to checkout.

1

Create an assistant

We’ll start by taking a look at the Assistant API reference and define our assistant:

1{
2 "model": {
3 "provider": "openai",
4 "model": "gpt-4",
5 "messages": [
6 {
7 "role": "system",
8 "content": "You're a pizza ordering assistant. The user will ask for toppings, you'll add them. When they're done, you'll redirect them to checkout."
9 }
10 ],
11 "functions": [
12 {
13 "name": "addTopping",
14 "description": "Used to add a topping to the pizza.",
15 "parameters": {
16 "type": "object",
17 "properties": {
18 "topping": {
19 "type": "string",
20 "description": "The name of the topping. For example, 'pepperoni'."
21 }
22 }
23 }
24 },
25 {
26 "name": "goToCheckout",
27 "description": "Redirects the user to checkout and order their pizza.",
28 "parameters": {"type": "object", "properties": {}}
29 }
30 ]
31 },
32 "firstMessage": "Hi, I'm the pizza ordering assistant. What toppings would you like?",
33}

Let’s break this down:

  • model - We’re using the OpenAI GPT-4 model, which is better at function calling.
  • messages - We’re defining the assistant’s instructions for how to run the call.
  • functions - We’re providing a addTopping function with a topping parameter. The assistant can call this during the conversation to add a topping. We’re also adding goToCheckout, with an empty parameters object. The assistant can call this to redirect the user to checkout.
  • firstMessage - This is the first message the assistant will say when the user starts the call.

We’ll then make a POST request to the Create Assistant endpoint to create the assistant.

2

Set up the Web SDK

We’ll follow the README for the Web SDK to get it installed.

We’ll then get our Public Key from the Vapi Dashboard and initialize the SDK:

1import Vapi from '@vapi-ai/web';
2
3const vapi = new Vapi('your-web-token');
3

Add the call buttons

We’ll add a button to the page that starts the call when clicked:

1<button id="start-call">Start Call</button>
2<button id="stop-call">Stop Call</button>
1const startCallButton = document.getElementById('start-call');
2
3startCallButton.addEventListener('click', async () => {
4 await vapi.start('your-assistant-id');
5});
6
7const stopCallButton = document.getElementById('stop-call');
8
9stopCallButton.addEventListener('click', async () => {
10 await vapi.stop();
11});
4

Handle call status events

1vapi.on('call-start', () => {
2 // Update UI to show that the call has started
3});
4
5vapi.on('call-end', () => {
6 // Update UI to show that the call has ended
7});
5

Handle speaking events

1vapi.on('speech-start', () => {
2 // Update UI to show that the assistant is speaking
3});
4
5vapi.on('speech-end', () => {
6// Update UI to show that the assistant is done speaking
7});
6

Handle transcription events

All messages send to the Server URL, including transcript and function-call messages, are also sent to the client as message events. We’ll need to check the type of the message to see what type it is.

1vapi.on("message", (msg) => {
2 if (msg.type !== "transcript") return;
3
4 if (msg.transcriptType === "partial") {
5 // Update UI to show the live partial transcript
6 }
7
8 if (msg.transcriptType === "final") {
9 // Update UI to show the final transcript
10 }
11});
7

Handle function call events

1vapi.on('message', (msg) => {
2 if (msg.type !== "function-call") return;
3
4if (msg.functionCall.name === "addTopping") {
5const topping = msg.functionCall.parameters.topping;
6// Add the topping to the pizza
7}
8
9if (msg.functionCall.name === "goToCheckout") {
10// Redirect the user to checkout
11}
12});
8

Order your pizza!

You should now have a working pizza ordering assistant! 🍕