Mahimai Raja
04/10/2025, 4:34 AMVapi Support Bot
04/10/2025, 4:36 AMbash
pip install flask
Then create a basic server:
python
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/webhook", methods=["POST"])
def handle_webhook():
# Get the JSON data from the request
data = request.get_json()
# Extract the message from the data
message = data.get("message", {})
# Get the message type
message_type = message.get("type")
# Handle different message types
if message_type == "function-call":
return handle_function_call(message)
elif message_type == "assistant-request":
return handle_assistant_request(message)
elif message_type == "hang":
return handle_hang(message)
elif message_type == "end-of-call-report":
return handle_end_of_call_report(message)
else:
# Default response
return jsonify({"status": "received"})
if __name__ == "__main__":
app.run(debug=True, port=5000)
## 2. Handle function calls
According to the [server events documentation](https://docs.vapi.ai/server-url/events), function calls have a specific structure. Here's how to handle them:
```python
def handle_function_call(message):
# Extract the call object and function call details
call = message.get("call", {})
tool_calls = message.get("toolCalls", [])
results = []
for tool_call in tool_calls:
tool_call_id = tool_call.get("id")
function = tool_call.get("function", {})
function_name = function.get("name")Vapi Support Bot
04/10/2025, 4:36 AM## 3. Handle assistant requests
When Vapi needs to get dynamic configuration for an assistant, it sends an assistant-request:
```python
def handle_assistant_request(message):
# Extract the call object
call = message.get("call", {})
# You can use the call information to customize the assistant configuration
# For example, you might look up the caller in your database
# Return the assistant configuration
return jsonify({
"assistant": {
"model": {
"provider": "openai",
"model": "gpt-4",
"messages": [
{
"role": "system",
"content": "You're a helpful assistant."
}
]
}
}
})
## 4. Handle end-of-call reports
End-of-call reports contain information about the completed call:
```python
def handle_end_of_call_report(message):
# Extract the call object and report details
call = message.get("call", {})
summary = message.get("summary")
analysis = message.get("analysis")
structured_data = message.get("structured_data") # Note: might be camelCase in some logs
# Process the end-of-call report
# For example, you might store it in your database
# Return a success responseVapi Support Bot
04/10/2025, 4:36 AM## 5. Handle hang notifications
When a call ends, Vapi sends a hang notification:
```python
def handle_hang(message):
# Extract the call object
call = message.get("call", {})
# Process the hang notification
# For example, you might update the call status in your database
# Return a success response
return jsonify({"status": "success"})
## 6. Expose your server to the internet
For Vapi to reach your server, it needs to be accessible from the internet. During development, you can use a tool like ngrok:
bash
ngrok http 5000
This will give you a public URL that you can use as your Server URL in the Vapi Dashboard.
## 7. Configure your Server URL in Vapi
Finally, you need to configure your Server URL in the Vapi Dashboard. You can do this at the account level or at the assistant level, as mentioned in the [documentation](https://docs.vapi.ai/tools/default-tools).
Remember to handle errors appropriately and validate the incoming data to ensure your server is robust.
Is there any specific part of the server implementation you'd like me to explain in more detail?
Sources:
- [Vapi Server URL Events](https://docs.vapi.ai/server-url/events)
- [Using Your Server with Vapi](https://docs.vapi.ai/customization/custom-llm/using-your-server)
- [Vapi Server Message API Reference](https://docs.vapi.ai/api-reference/webhooks/server-message)
- [Vapi Tools Documentation](https://docs.vapi.ai/tools/default-tools)Kings_big💫
04/10/2025, 5:58 AMVapi Support Bot
04/10/2025, 7:09 AMMahimai Raja
04/10/2025, 7:09 AMKings Big
04/11/2025, 3:26 PM