Vapi Server SDK

1.10.0 · active · verified Thu Apr 16

The Vapi Python SDK provides convenient access to the Vapi API, enabling Python developers to easily integrate Vapi's conversational AI capabilities into their applications. It facilitates the creation and deployment of AI-powered virtual assistants, particularly for server-side call management like automated outbound campaigns, inbound call routing, and webhook processing. The library is actively maintained, with frequent releases often multiple times a month, reflecting continuous development and improvements.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate both the synchronous `Vapi` and asynchronous `AsyncVapi` clients using your Vapi API key, preferably loaded from an environment variable. It illustrates basic API interaction for creating a call and includes error handling for `ApiError` responses. Replace placeholder API calls with actual methods from the Vapi API documentation, such as `client.calls.create()` with relevant `assistant_id` and `phone_number_id`.

import os
import asyncio
from vapi import Vapi, AsyncVapi
from vapi.core.api_error import ApiError

# Synchronous client example
def create_sync_call():
    client = Vapi(
        token=os.environ.get('VAPI_API_KEY', 'YOUR_VAPI_PRIVATE_API_KEY')
    )
    try:
        # Example: create an outbound call (replace with actual assistant_id and phone_number_id)
        print("Attempting to create a synchronous call...")
        # This method is illustrative; actual API calls will vary based on Vapi's current API.
        # Refer to Vapi's official documentation for up-to-date API methods and parameters.
        # Example: call = client.calls.create(assistant_id="asst_YOUR_ID", phone_number_id="pn_YOUR_ID", customer_number="+15551234567")
        # For demonstration, we'll simulate a success.
        # In a real scenario, you would call a Vapi API method, e.g., client.calls.create(...)
        print("Synchronous call creation simulated successfully.")
    except ApiError as e:
        print(f"Synchronous API Error: Status {e.status_code}, Body: {e.body}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

# Asynchronous client example
async def create_async_call():
    client = AsyncVapi(
        token=os.environ.get('VAPI_API_KEY', 'YOUR_VAPI_PRIVATE_API_KEY')
    )
    try:
        print("Attempting to create an asynchronous call...")
        # Similar to sync client, actual API calls depend on Vapi's current API.
        # Example: call = await client.calls.create(assistant_id="asst_YOUR_ID", phone_number_id="pn_YOUR_ID", customer_number="+15551234567")
        # For demonstration, we'll simulate a success.
        await asyncio.sleep(0.1) # Simulate async operation
        print("Asynchronous call creation simulated successfully.")
    except ApiError as e:
        print(f"Asynchronous API Error: Status {e.status_code}, Body: {e.body}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == "__main__":
    print("--- Synchronous Vapi Client Example ---")
    create_sync_call()

    print("\n--- Asynchronous Vapi Client Example ---")
    asyncio.run(create_async_call())

view raw JSON →