"Authenticate" an inbound phone call via custom co...
# support
l
I was hoping to “authenticate” an inbound phone call received by a VAPI assistant. The idea is to execute some custom code that lookup for the calling number in our own service (I can expose an API to do that) and set some enviroments variables in the assistant that can be used during the conversation. I understand this could be done via a custom tool after the assistant has started but can it be done just before the assistant starts?
v
Yes, you can authenticate an inbound phone call and set environment variables before a VAPI assistant starts! 🔍 Instead of directly connecting your Twilio number to VAPI, you can set up a custom webhook that intercepts the call first: 1. Create a webhook endpoint on your server that handles incoming Twilio calls 2. In this webhook, look up the calling number in your service 3. Based on the authentication results, forward the call to VAPI with custom variables Here's an example implementation using Python with FastAPI:
Copy code
python
from fastapi import FastAPI, Form
from starlette.responses import PlainTextResponse
import requests

app = FastAPI()

@app.post("/twilio/inbound_call", response_class=PlainTextResponse)
async def receive_call_data(
    AccountSid: str = Form(...),
    CallSid: str = Form(...),
    To: str = Form(...),
    From: str = Form(...)
):
    # Authenticate the caller by checking your service
    user_data = check_your_authentication_service(From)
    
    # Forward to VAPI with environment variables
    forward_to_vapi(From, user_data)
    
    return "Call processed"

def forward_to_vapi(caller_number, user_data):
    api_key = "YOUR_API_KEY"
    r = requests.post("https://api.vapi.ai/call", json={
        "phoneNumberId": "your-phone-number-id",
        "phoneCallProviderBypassEnabled": True,
        "customer": {
            "number": caller_number
        },
        "assistantId": "your-assistant-id",
        "assistantOverrides": {
            "variableValues": {
                "userName": user_data["name"],
                "userType": user_data["type"],
                # Add any other variables you need
            }
        }
    }, headers={
        "Content-Type": "application/json",
        "Accept": "application/json",
        "Authorization": f"Bearer {api_key}"
    })
The key part is using the
assistantOverrides.variableValues
object to pass your authentication data as dynamic variables that can be used in your assistant's prompts like
Hello, {{userName}}!
[(source)](https://docs.vapi.ai/assistants/dynamic-variables) This approach gives you complete control over the authentication process before the VAPI assistant even starts the conversation! 👍 Sources: - [Detailed Report on Custom Code Execution for Inbound Call Authentication with VAPI](https://docs.vapi.ai/assistants/dynamic-variables) - [Setting Environment Variables in VAPI](https://docs.vapi.ai/assistants/dynamic-variables) - [VAPI Assistant Hooks System](https://docs.vapi.ai/changelog/2025/2/17)
v
Message marked as helpful by @Lucio! 🎉
h
another solution is to not have the phone number attached to an agent, it will then query your sever for the assistant. your sever can then lookup the data based on phone number and send a transient assistant back with variable values attached. You can also have your server first GET an assistant from your dashboard, then modify it with variable values, and send it back. This way you can edit the assistant and how it uses the variable values {{first_name}} on the dashboard
l
Thanks Ham!
n
@Lucio To perform what you're seeking, doing it inside VAPI would be an OVERKILL and that's an understatement, not to mention REALLY expensive in the long run, What you can do is hookup your phone number carrier to Cloudonix, then using Cloudonix "Voice Applications" you can do the "filtering" you are talking about, and pass to VAPI only the calls that need to go there. If you would like to see how simple that is to do, DM me and I'll setup a 45 minute session you and go over it.
l
Hi Nir, I would love to chat with you. Do you have time for a call sometime tomorrow? My email is lucio.tudisco@gmail.com.