Vapi Server SDK
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
-
vapi.core.api_error.ApiError: HTTP status code (4xx or 5xx) received
cause The Vapi API returned an error response (e.g., bad request, unauthorized, server error).fixCatch `ApiError` exceptions and inspect `e.status_code` and `e.body` for specific error details. Verify API token, request parameters, and review Vapi service status. -
Call failed due to 'assistant-request-failed' or 'assistant-request-returned-error'
cause Your server URL, configured to dynamically provide an assistant, failed to respond or returned an error.fixEnsure your server is running, publicly accessible (e.g., via ngrok for local development), and returns a valid assistant configuration. Check server logs for errors. -
call.start.error-subscription-insufficient-credits
cause Your Vapi account has insufficient credits to initiate the call.fixAdd more credits to your Vapi account or enable auto-reload for your subscription. -
401-incorrect-api-key, 403-model-access-denied, 429-exceeded-quota for LLM/Voice Provider
cause Issues with API keys, permissions, or rate limits for integrated LLM or voice providers (e.g., OpenAI, ElevenLabs).fixVerify the API key for the specific provider is correct, ensure it has access to the requested models, and check if you've exceeded any rate limits or quotas with that provider. -
High loading time and unnecessary warnings at startup in Docker (related to pydantic models)
cause Reported issue where pydantic model rendering causes performance overhead and warnings in containerized environments.fixThis is an open issue (#22) on the GitHub repository. Monitor the official repository for updates or workarounds. Optimize environment setup to minimize pydantic re-evaluation if possible, though a direct fix is pending from the library maintainers.
Warnings
- breaking Several legacy API endpoints have been removed as part of Vapi's API modernization, including `/logs`, `/workflow/{id}`, `/test-suite`, and `/knowledge-base` related paths.
- breaking The `knowledgeBaseId` property has been removed from all model configurations due to a knowledge base architecture change.
- deprecated The `AssemblyAITranscriber.wordFinalizationMaxWaitTime` and `FallbackAssemblyAITranscriber.wordFinalizationMaxWaitTime` properties are deprecated.
- gotcha The SDK is programmatically generated. Direct code contributions (outside of README) will be overwritten in future releases.
- gotcha Requests have a default timeout of 60 seconds.
Install
-
pip install vapi-server-sdk
Imports
- Vapi
from vapi_server_sdk import VapiClient
from vapi import Vapi
- AsyncVapi
from vapi import AsyncVapi
- ApiError
from vapi.core.api_error import ApiError
Quickstart
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())