Documentation

Learn how to use the Vapi.ai SDK and this demo application

Introduction to Vapi.ai

Learn about Vapi.ai and how it can help you build voice AI agents

Vapi.ai is a platform that allows developers to create voice AI agents that can have natural conversations with users. It provides tools for building, testing, and deploying voice assistants.

With Vapi.ai, you can:

  • Create voice agents that can understand and respond to user queries
  • Integrate with existing systems and APIs
  • Analyze conversation data to improve your agents
  • Deploy your agents to various platforms

Installation

How to install and set up the Vapi.ai SDK

To get started with Vapi.ai, you'll need to install the SDK:

npm install @vapi.ai/sdk

Then, you'll need to set up your API key:

import { VapiService } from '@vapi.ai/sdk';

const vapi = new VapiService('YOUR_API_KEY');

Quick Start

Create your first voice AI agent

Here's a simple example of how to create a voice AI agent:

import { VapiService } from '@vapi.ai/sdk';

// Initialize the Vapi service
const vapi = new VapiService(process.env.VAPI_API_KEY);

// Create a new assistant
async function createAssistant() {
const assistant = await vapi.createAssistant({
name: 'My First Assistant',
model: 'gpt-4',
voice: 'nova',
instructions: 'You are a helpful assistant that answers questions about our product.'
});

console.log('Assistant created:', assistant);
return assistant;
}

createAssistant();

VapiService

The main class for interacting with the Vapi.ai API

Constructor

new VapiService(apiKey: string)

Creates a new instance of the VapiService with the provided API key.

Methods

createAssistant

createAssistant(options: {
name: string;
model: string;
voice: string;
instructions?: string;
}): Promise<Assistant>

Creates a new assistant with the specified options.

getAssistants

getAssistants(): Promise<Assistant[]>

Gets a list of all assistants.

createCall

createCall(options: {
assistant_id: string;
to: string;
}): Promise<Call>

Creates a new call with the specified assistant and phone number.

getCallDetails

getCallDetails(callId: string): Promise<Call>

Gets details about a specific call.

getCallTranscript

getCallTranscript(callId: string): Promise<Transcript>

Gets the transcript of a specific call.

Types

TypeScript type definitions for the Vapi.ai SDK

Assistant

interface Assistant {
id: string;
name: string;
model: string;
voice: string;
instructions?: string;
created_at: string;
updated_at: string;
}

Call

interface Call {
id: string;
assistant_id: string;
to: string;
status: 'queued' | 'in-progress' | 'completed' | 'failed';
duration: number;
created_at: string;
updated_at: string;
}

Transcript

interface Transcript {
call_id: string;
messages: {
role: 'assistant' | 'user';
content: string;
timestamp: string;
}[];
}

Creating a Customer Service Agent

Example of how to create a customer service agent

This example shows how to create a customer service agent that can answer questions about a product:

import { VapiService } from '@vapi.ai/sdk';

// Initialize the Vapi service
const vapi = new VapiService(process.env.VAPI_API_KEY);

// Create a customer service assistant
async function createCustomerServiceAssistant() {
const assistant = await vapi.createAssistant({
name: 'Customer Service',
model: 'gpt-4',
voice: 'nova',
instructions: `
You are a customer service representative for Acme Inc.
You help customers with questions about our products.
Be friendly, helpful, and concise.
If you don't know the answer to a question, offer to connect the customer with a human representative.
`
});

return assistant;
}

// Make a call with the assistant
async function makeCall(assistantId, phoneNumber) {
const call = await vapi.createCall({
assistant_id: assistantId,
to: phoneNumber
});

console.log('Call initiated:', call);
return call;
}

Analyzing Call Data

Example of how to analyze call data

This example shows how to get and analyze call data:

import { VapiService } from '@vapi.ai/sdk';

// Initialize the Vapi service
const vapi = new VapiService(process.env.VAPI_API_KEY);

// Get call details
async function getCallInfo(callId) {
const call = await vapi.getCallDetails(callId);
console.log('Call details:', call);

// Get the transcript
const transcript = await vapi.getCallTranscript(callId);
console.log('Transcript:', transcript);

// Analyze the transcript
const userMessages = transcript.messages.filter(msg => msg.role === 'user');
const assistantMessages = transcript.messages.filter(msg => msg.role === 'assistant');

console.log(`User spoke ${userMessages.length} times`);
console.log(`Assistant spoke ${assistantMessages.length} times`);

// Calculate average message length
const avgUserMessageLength = userMessages.reduce((sum, msg) => sum + msg.content.length, 0) / userMessages.length;
const avgAssistantMessageLength = assistantMessages.reduce((sum, msg) => sum + msg.content.length, 0) / assistantMessages.length;

console.log(`Average user message length: ${avgUserMessageLength.toFixed(2)} characters`);
console.log(`Average assistant message length: ${avgAssistantMessageLength.toFixed(2)} characters`);

return { call, transcript, analysis: { userMessages, assistantMessages, avgUserMessageLength, avgAssistantMessageLength } };
}

Frequently Asked Questions

Common questions about Vapi.ai

What is Vapi.ai?

Vapi.ai is a platform that allows developers to create voice AI agents that can have natural conversations with users. It provides tools for building, testing, and deploying voice assistants.

What models does Vapi.ai support?

Vapi.ai supports various language models, including GPT-4, GPT-3.5, and others. You can specify the model when creating an assistant.

What voices are available?

Vapi.ai offers a variety of voices with different accents and styles. Some of the available voices include Nova, Echo, Onyx, and more.

How do I get an API key?

You can get an API key by signing up for an account on the Vapi.ai website and creating a new project.

Is there a free tier?

Yes, Vapi.ai offers a free tier with limited usage. You can upgrade to a paid plan for more features and higher usage limits.

Can I integrate Vapi.ai with my existing systems?

Yes, Vapi.ai provides APIs that allow you to integrate with your existing systems and services.