This tutorial builds a complete voicemail system in Node.js using Express and Twilio TwiML that plays a professional greeting to callers, records their message, and sends an SMS notification to your team with the recording details. It targets developers who need programmable voicemail with transcription and notification support without hardware or a traditional phone system. You will build two Express routes: one for the greeting and recording flow, and one that receives the recording details after the audio is processed.
Prerequisites and Setup
Install the packages with npm install twilio express dotenv and configure your Twilio number's voice webhook to POST to your /voicemail/greeting route so every inbound call triggers the voicemail flow. Store the notification recipient's phone number as a separate environment variable so you can change it without modifying code. The recording flow uses two Express routes: the greeting route returns TwiML with the Record verb, and the action route receives the recording details after the call ends. Expose your server with npx ngrok http 3000 during development since all TwiML URLs must be publicly accessible.
npm install twilio express dotenv
# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_PHONE_NUMBER=+15551234567
NOTIFICATION_NUMBER=+15559998888
The Complete Code
The greeting route uses the Say verb to play a professional greeting and the record verb with a 120-second maximum duration, a 5-second silence timeout, transcription enabled, and an action URL pointing to the recording handler. The playBeep: true option on the Record verb plays a tone so callers know when to start speaking. The recording action route reads the caller's number, duration, and recording SID from the POST body, logs the details, and sends an SMS notification to your team. Responding with a brief thank-you message after the recording completes gives callers confirmation that their message was saved.
require('dotenv').config();
const express = require('express');
const twilio = require('twilio');
const app = express();
app.use(express.urlencoded({ extended: false }));
const client = twilio(
process.env.TWILIO_ACCOUNT_SID,
process.env.TWILIO_AUTH_TOKEN
);
const FROM = process.env.TWILIO_PHONE_NUMBER;
const NOTIFY = process.env.NOTIFICATION_NUMBER;
app.post('/voicemail/greeting', (req, res) => {
const twiml = new twilio.twiml.VoiceResponse();
twiml.say({ voice: 'alice' },
'You have reached Telphi Consulting. We are unable to take your call right now. ' +
'Please leave your name, number, and a brief message after the tone, ' +
'and we will return your call within one business day.'
);
twiml.record({
timeout: 5,
maxLength: 120,
transcribe: true,
action: '/voicemail/recorded',
finishOnKey: '#',
playBeep: true,
transcribeCallback: '/voicemail/transcription',
});
twiml.say({ voice: 'alice' }, 'We did not receive a message. Goodbye.');
res.type('text/xml');
res.send(twiml.toString());
});
app.post('/voicemail/recorded', async (req, res) => {
const caller = req.body.Caller || 'Unknown';
const duration = req.body.RecordingDuration || '0';
const recSid = req.body.RecordingSid || '';
console.log('New voicemail from', caller, duration + 's SID:', recSid);
try {
await client.messages.create({
body: 'New voicemail from ' + caller + ' (' + duration + 's). ' +
'Recording SID: ' + recSid + '. Check the Twilio Console to listen.',
from: FROM,
to: NOTIFY,
});
} catch (err) {
console.error('Notification failed:', err.message);
}
const twiml = new twilio.twiml.VoiceResponse();
twiml.say({ voice: 'alice' }, 'Your message has been recorded. Thank you and goodbye.');
res.type('text/xml');
res.send(twiml.toString());
});
app.post('/voicemail/transcription', (req, res) => {
const text = req.body.TranscriptionText || '';
const sid = req.body.RecordingSid || '';
console.log('Voicemail transcript [' + sid + ']:', text);
res.sendStatus(204);
});
app.listen(3000, () => console.log('Voicemail server on port 3000'));
Testing and Debugging
Call your Twilio number, listen to the greeting, leave a 10-second test message, and press # or hang up. Within 30 to 60 seconds you should receive the SMS notification with the recording SID. Check the Express terminal for the /voicemail/recorded POST to confirm Twilio called the action URL with the recording details after the call ended. If the SMS notification fails, check the terminal for a caught error and verify your notification number is verified under Verified Caller IDs if you are on a trial account.
Taking It Further
Store each voicemail's recording SID, caller, duration, and timestamp in a database and build an Express API that lists new voicemails sorted by date for a web dashboard. Add an audio playback endpoint that streams the recording MP3 from Twilio's URL through your server so the voicemail web UI does not expose Twilio credentials to the browser. Set up a cron job using node-cron that downloads recordings to S3 after 24 hours and then deletes them from Twilio using client.recordings(sid).remove() to avoid storage charges. Send email notifications in addition to SMS using Nodemailer with the recording URL and transcript in the email body for team members who prefer email over text.
Contact Telphi Consulting to build a production voicemail management system with transcription, notifications, and a web playback interface in Node.js.
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.