This tutorial shows how to initiate an outbound phone call from Node.js using the Twilio REST API with a TwiML URL that controls what Twilio says or plays when the call connects. It targets Node.js developers adding automated voice notifications, appointment reminders, or call flows to their applications. You will trigger a real outbound call and serve the TwiML instructions from an Express route on the same server.
Prerequisites and Setup
Install the required packages with npm install twilio express dotenv and expose your local server with ngrok using npx ngrok http 3000. The url parameter in client.calls.create() must point to a publicly accessible HTTPS endpoint that returns TwiML XML; use the ngrok HTTPS URL followed by your route path during development. Twilio fetches the TwiML URL as an anonymous HTTP client once the call connects, so the endpoint must not require any authentication headers. Store your Account SID, Auth Token, and phone number as environment variables in a .env file.
npm install twilio express dotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
The Complete Code
The Express route at /voice returns a TwiML response with a Say verb that reads a message using Twilio's Alice voice when the call connects. The client.calls.create() method dials the to number and, once answered, fetches the TwiML at the provided url to determine what to play or say. The returned CallInstance has a sid starting with CA that you can use to monitor, update, or cancel the call programmatically. Logging the SID immediately lets you look up call details in the Twilio Console without waiting for the call to complete.
require('dotenv').config();
const express = require('express');
const twilio = require('twilio');
const app = express();
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
app.post('/voice', (req, res) => {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say({ voice: 'alice' },
'Hello. This is an automated message from Telphi Consulting.'
);
res.type('text/xml');
res.send(twiml.toString());
});
async function makeCall(toNumber, twimlUrl) {
const call = await client.calls.create({
to: toNumber,
from: process.env.TWILIO_PHONE_NUMBER,
url: twimlUrl,
});
console.log('Call SID:', call.sid, 'Status:', call.status);
return call.sid;
}
app.listen(3000, async () => {
console.log('Server running on port 3000');
// Replace with your ngrok URL before running
await makeCall('+15551234567', 'https://xxxx.ngrok.io/voice');
});
Testing and Debugging
Start the Express server, confirm your ngrok tunnel is forwarding to port 3000, paste the ngrok URL into the makeCall() call, and run the script. The call SID should log immediately while the outbound call rings the destination number. If the destination rings but you hear nothing, check the ngrok terminal for a 200 response to the /voice POST and verify the TwiML XML is well-formed. Error code 13224 in the Twilio Console means Twilio could not fetch your TwiML URL; confirm the server is running and the ngrok tunnel has not expired.
Taking It Further
Add a statusCallback URL to calls.create() so Twilio POSTs events such as initiated, ringing, answered, and completed to your server for real-time call tracking. Pass machineDetection: 'Enable' to activate answering machine detection so your TwiML can branch on whether a human or voicemail answered the call. Use the timeout parameter to limit how many seconds Twilio rings before treating the call as unanswered. For personalised voice messages, pass call metadata as query parameters on the TwiML URL and read them in the Express route to construct a dynamic Say response.
Contact Telphi Consulting to design automated outbound call campaigns with dynamic TwiML, AMD, and call analytics.
Ready to Transform Your Business Communications?
Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.
Comments (0)
Join the discussion and share your thoughts (AI-moderated for quality)
Be the first to comment
No comments yet. Share your thoughts below.