adverse-sapphire
adverse-sapphire3h ago

connect voice agent and twilio at scale

We are building a single Vapi voice agent that uses a custom LLM URL to dynamically respond to incoming calls. The goal is to identify the caller's phone number and use it to determine which backend LLM instance should handle the conversation. This allows us to support multiple use cases through a single voice agent on the Vapi dashboard. To scale this setup, we plan to connect multiple Twilio phone numbers (potentially up to 1000) to this single Vapi agent. Each number represents a different use case or client, and the voice agent will route the call dynamically based on the incoming number. We have a few questions: Is there a limit to how many Twilio numbers can be connected to a single Vapi voice agent? If 1000 numbers is not feasible, what is the recommended architecture to support this kind of routing? Is ther a way to connect Twilio call to vapi passing through our own backend?
8 Replies
Vapi
Vapi3h ago
To support dynamic routing for many Twilio numbers, configure each number (or the assistant) with a server URL that points to your backend. Your backend can then use the incoming caller's number to select the correct LLM instance. Here’s a sample Express.js endpoint to handle this:
app.post('/vapi-webhook', (req, res) => {
const caller = req.body.customer?.number;
// Lookup LLM instance based on caller
// Route call logic here
res.json({ assistantId: getLLMForCaller(caller) });
});
app.post('/vapi-webhook', (req, res) => {
const caller = req.body.customer?.number;
// Lookup LLM instance based on caller
// Route call logic here
res.json({ assistantId: getLLMForCaller(caller) });
});
Set this endpoint as your assistant or phone number server URL in Vapi to enable scalable, dynamic routing. Sources: - Setting server URLs - Import number from Twilio
⤵️ Mention @Vapi Support Bot for follow-ups 🐥
adverse-sapphire
adverse-sapphireOP3h ago
but how does the call from Twilio gets connected to the voice agent? We are able to get the dynamic LLM instance working. @Vapi Support Bot
Vapi
Vapi3h ago
To connect Twilio calls to your Vapi voice agent, set your Twilio number's webhook to your backend, then have your backend trigger a Vapi call. For example:
// Twilio webhook handler
app.post('/twilio-incoming', async (req, res) => {
const caller = req.body.From;
// Call Vapi API to start the voice agent
await axios.post('https://api.vapi.ai/call', {
phoneNumberId: YOUR_VAPI_PHONE_NUMBER_ID,
customer: { number: caller },
assistantId: getLLMForCaller(caller)
}, { headers: { Authorization: `Bearer ${VAPI_API_KEY}` } });
res.send('<Response></Response>'); // TwiML response
});
// Twilio webhook handler
app.post('/twilio-incoming', async (req, res) => {
const caller = req.body.From;
// Call Vapi API to start the voice agent
await axios.post('https://api.vapi.ai/call', {
phoneNumberId: YOUR_VAPI_PHONE_NUMBER_ID,
customer: { number: caller },
assistantId: getLLMForCaller(caller)
}, { headers: { Authorization: `Bearer ${VAPI_API_KEY}` } });
res.send('<Response></Response>'); // TwiML response
});
Set /twilio-incoming as your Twilio number's webhook. This lets you scale and route calls dynamically through your backend.
Reference: Call Handling with Vapi and Twilio Source: - Call Handling with Vapi and Twilio
adverse-sapphire
adverse-sapphireOP3h ago
@Vapi Support Bot Do i need to link all 1000 numbers on the vapi dashboard to get this id? phoneNumberId: YOUR_VAPI_PHONE_NUMBER_ID,
Vapi
Vapi3h ago
Yes, each Twilio number you want to use with Vapi must be imported and registered in the Vapi dashboard to obtain its unique phoneNumberId.
Once imported, you can use that phoneNumberId in your backend logic to trigger the Vapi agent for each incoming call:
await axios.post('https://api.vapi.ai/call', {
phoneNumberId: YOUR_VAPI_PHONE_NUMBER_ID, // unique for each imported number
customer: { number: caller },
assistantId: getLLMForCaller(caller)
}, { headers: { Authorization: `Bearer ${VAPI_API_KEY}` } });
await axios.post('https://api.vapi.ai/call', {
phoneNumberId: YOUR_VAPI_PHONE_NUMBER_ID, // unique for each imported number
customer: { number: caller },
assistantId: getLLMForCaller(caller)
}, { headers: { Authorization: `Bearer ${VAPI_API_KEY}` } });
For large-scale setups, consider automating the import process via the Vapi API to manage many numbers efficiently. Sources: - Phone Calling - Call Handling with Vapi and Twilio
adverse-sapphire
adverse-sapphireOP3h ago
@Vapi Support Bot is there a limit on how many numbers can be imported? we have 1000 of them
Vapi
Vapi3h ago
There is no documented hard limit on the number of Twilio numbers you can import into Vapi—only free Vapi numbers are limited to 10 per account. For large-scale imports (like 1000 numbers), automate the process using the Vapi API for efficiency. If you encounter any issues at this scale, it's best to reach out to Vapi support for guidance. Reference: Phone Calling documentation Source: - Phone Calling
Duckie
Duckie3h ago
Message marked as helpful by @VerveAI! 🎉 Message marked as helpful by @VerveAI! 🎉 Message marked as helpful by @VerveAI! 🎉

Did you find this page helpful?