To build a voice AI agent that processes retail returns without human intervention, start by provisioning a Twilio phone number and linking it to a Vapi flow. Vapi orchestrates the call, routes inbound audio to a speech-to-text service (e.g., Deepgram or Whisper), and sends the transcript to a webhook you host in n8n. In the webhook, parse the customer's request, call the Shopify API to create a return, and decide whether a refund is eligible. For the response, request an ElevenLabs voice to synthesize a confirmation and stream the audio back through Vapi, which hands it to Twilio for playback. The entire sequence - call inbound → transcription → logic → TTS → playback - is deterministic, fully automated, and runs on standard HTTP endpoints.
How it works
- Inbound call - A customer dials the Twilio number; Twilio captures the audio and streams it to Vapi's IVR engine. Vapi routes the call to the next step based on the menu selected (e.g., "Return a product").
- Speech-to-Text - Vapi forwards the live audio to Deepgram (or Whisper) for real-time transcription. The STT service returns a JSON transcript with timestamps that Vapi uses to parse intent (e.g., product SKU, quantity, reason).
- Orchestration & Decision - Vapi, acting as the voice-agent orchestration layer, sends the parsed intent to an n8n workflow via a webhook. n8n queries the Shopify API to verify the order, check stock, and calculate refund eligibility. The workflow then decides whether the return can be processed automatically.
- Text-to-Speech response - If the return is approved, n8n triggers an ElevenLabs TTS call, sending the confirmation text ("Your refund of $29.99 will be issued within 5 business days") to Vapi. Vapi streams the generated audio back to Twilio, which plays it to the caller.
- Transaction finalisation - Vapi calls the Shopify API to update the order status and issue the refund. It logs the call transcript and TTS output to a database for audit and feeds the data into a reporting dashboard. The call ends with Twilio's "goodbye" message, completing a fully automated return workflow.
How to build it
1. Provision the telephony endpoint in Twilio
Log in to the Twilio Console → Phone Numbers → Buy a Number (choose a local US number).
Under Messaging & Voice, set Incoming Call webhook to https://<your-domain>/twilio/incoming.
* Save. The webhook will receive a POST with CallSid, From, and To and must reply with valid TwiML that redirects the call to Vapi's SIP gateway (see step 2).
2. Create a Vapi voice-agent that routes to ElevenLabs
Sign up at https://vapi.ai and open the Agents page.
Click New Agent → give it a name such as ReturnRefundBot.
In the Speech-to-Text section select Deepgram (or Whisper) - Vapi will handle the transcription.
In the Text-to-Speech dropdown choose ElevenLabs and select a voice (e.g., "Rachel").
Set the Agent URL* to https://<your-domain>/vapi/webhook. This is the endpoint that Vapi will call after each user turn.
3. Expose the Vapi webhook and connect Twilio to it
Create a small Express server (or any HTTP server) that accepts `POST /vapi/webhook`.
The body will contain transcript, intent, and a sessionId. Respond with a JSON payload that Vapi expects:
* In the Twilio webhook (/twilio/incoming) return TwiML that forwards the call to Vapi's SIP endpoint:
* Deploy the server to a reachable URL (HTTPS required for both Twilio and Vapi).
4. Add Shopify order-lookup logic in the Vapi webhook * Inside the webhook handler, when the user provides an order number, call the Shopify API:
* If the order is found and its fulfillment_status is not "fulfilled", build a confirmation prompt:
5. Trigger the refund via Shopify and close the call When the user says "yes", send a `POST` to the Shopify Refund* endpoint:
* If the refund succeeds, respond with:
* Vapi will synthesize the reply with ElevenLabs and, because endCall is true, will send a Hang-up signal back to Twilio, terminating the phone session.
6. Orchestrate the whole flow in n8n (optional but recommended for monitoring) Install n8n (Docker: `docker run -p 5678:5678 n8nio/n8n`). Create a workflow with the following nodes (exported JSON excerpt shown below):
- Webhook (/n8n/vapi) - receives Vapi payload.
- HTTP Request - calls Shopify order lookup.
- If - checks whether the user confirmed.
- HTTP Request - issues the refund.
- Respond to Webhook - sends the JSON reply back to Vapi.
Enable Execution Mode* "Manual" for testing, then switch to "Queue" for production.
```json { "nodes": [ { "parameters": { "path": "vapi", "method": "POST" }, "name": "Vapi Webhook", "type": "n8n-nodes-base.webhook", "typeVersion": 1, "position": [250, 300] }, { "parameters": { "url": "https://{{ $json.shopifyStore }}/admin/api/2024-01/orders/{{ $json.orderId }}.json", "method": "GET", "authentication": "headerAuth", "headerAuth": { "name": "X-Shopify-Access-Token", "value": "={{$env.SHOPIFY_TOKEN}}" } }, "name": "Get Order", "type": "n8n-nodes-base.httpRequest", "typeVersion": 1, "position": [500, 300] }, { "parameters": { "conditions": { "boolean": [ { "value1": "={{$json.reply.includes('yes')}}", "operation": "contains" } ]
What it costs to run
This estimate assumes a 2-minute average call duration, 128kbps audio, and successful connection rates above 95%.
| Component | Rate | 100 Calls | 1,000 Calls | 10,000 Calls |
|---|---|---|---|---|
| Twilio | ~$0.085/min | $17.00 | $170.00 | $1,700.00 |
| Vapi | ~$0.05/min | $10.00 | $100.00 | $1,000.00 |
| ElevenLabs | Variable | ~$2.00 | ~$20.00 | ~$200.00 |
| Total | - | ~$29.00 | ~$290.00 | ~$2,900.00 |
Twilio and Vapi rates represent standard US inbound voice and platform fees respectively. ElevenLabs costs vary significantly by voice tier and character volume; these figures assume mid-tier realistic voice models.
These figures exclude development time, potential n8n hosting costs if running a dedicated instance, and Shopify API rate limits which are generally free for standard usage. Actual monthly spend will fluctuate based on average handle time and failed call ratios. Always verify current provider pricing pages before budgeting, as rates adjust for volume tiers.
Where this breaks
1. Voice transcription errors from Deepgram Symptom: The bot mishears common return terms ("refund", "return", "exchange") and routes the call to an incorrect IVR node. Fix: Increase the confidence threshold in the Deepgram webhook and add a fallback "Did you say X?" prompt; optionally train a custom model on your return-related vocabulary.
2. ElevenLabs TTS latency under load Symptom: During peak hours the TTS response is delayed by 2-3 seconds, causing callers to perceive the system as unresponsive. Fix: Pre-generate and cache frequently used prompts (e.g., "Your return is being processed") and serve them from a local Redis store; also consider provisioning a higher tier ElevenLabs plan for lower latency.
3. Twilio webhook timeouts
Symptom: The TwiML webhook returns a 504 after the first call, and Twilio retries, resulting in duplicated return entries in Shopify.
Fix: Keep the webhook's execution time under 15 seconds, offload heavy logic to a background queue (e.g., n8n), and return an early 200 with a <Pause> to keep the call alive until processing completes.
4. Shopify API rate limits
Symptom: After a batch of 200 return calls, the bot receives 429 responses and fails to create return orders.
Fix: Implement exponential back-off in the Shopify integration, or batch the return requests and schedule them during off-peak hours; monitor the X-Shopify-Shop-Api-Request-Count header to stay within limits.
How do I connect Vapi's voice orchestration to a Twilio phone number?
Create a Twilio Voice webhook that points to your Vapi endpoint (e.g., https://yourdomain.com/vapi/webhook). In the Twilio console, assign that webhook URL to the incoming call handler of the purchased number. Vapi will receive the call SID, answer the call via Twilio's REST API, and take over the dialog flow.
Which service provides the text-to-speech voice for the refund bot? ElevenLabs supplies the natural-sounding TTS output. After Vapi generates the response text, it calls the ElevenLabs API (using your API key) to retrieve an audio stream, which Vapi streams back to Twilio for playback.
How can I retrieve order data from Shopify during the call? Expose a secure webhook in your automation platform (e.g., n8n) that queries the Shopify API with the caller's phone number or order reference. Vapi can invoke that webhook from the IVR flow, receive the JSON payload, and use the data to confirm the refund amount.
What latency should I expect from real-time language processing? Using Deepgram or Whisper for speech-to-text and ElevenLabs for TTS typically yields end-to-end latency of 1-2 seconds per turn when hosted in the same region. Keep the Vapi workflow lightweight and avoid unnecessary HTTP hops to stay within this range.
For a deeper technical reference, see n8n's documentation.