How to Send Voice Notifications
A phone call is a high-priority method for delivering critical alerts. In this guide, you will learn how to automate voice notifications to a list of recipients, play a custom message, and capture user confirmation via keypad (DTMF) input.
Prerequisites
- A Vonage account
- Node.js installed on your machine.
- localtunnel installed globally (
npm install -g localtunnel).
Initialize your Project Folder
- Create the environment where your code and security keys will live.
- Open your terminal and create a new directory:
- Initialize the project and install the Vonage SDK and Express:
Create a Vonage Application
Generate your credentials via the Dashboard and save them to the folder you just created.
- Log in to the Vonage Dashboard.
- Go to Applications > Create a new application.
- Authentication: Click Generate public and private keys.
- A file named
private.keywill download. - Move this
private.keyfile from your Downloads folder into yourvoice-notificationsfolder.
- Capabilities: Enable Voice.
We will return here to Step 3 to add the Webhook URLs.
- Click Save Changes at the bottom.
Start your Public Tunnel
Vonage needs to send webhooks to your local machine. Use localtunnel to create a public URL:
Replace my-unique-voice-notification-app with a unique string of your choice.
Keep this terminal window open.
Go back to your Application in the Vonage Dashboard, click Edit and set the following webhooks in the Voice group of Capabilities section :
- Answer URL:
https://my-unique-voice-notification-app.loca.lt/answer(Method:GET) - Event URL:
https://my-unique-voice-notification-app.loca.lt/event(Method:POST)
Create the Webhook Server (server.js)
When Vonage answers a call, it fetches a Call Control Object (NCCO) from your answer_url. This JSON array defines the "script" the call follows.
Create a file named server.js and set up the webhook to provide the NCCO:
const express = require('express');
const app = express();
app.use(express.json());
app.get('/answer', (req, res) => {
// NCCO defines what the recipient hears
res.json([
{
action: 'talk',
text: 'This is a priority notification. Please press 1 to confirm receipt.'
},
{
action: 'input',
type: [ 'dtmf' ],
dtmf: { maxDigits: 1 }
}
]);
});
app.post('/event', (req, res) => {
// Capture the button press
if (req.body.dtmf && req.body.dtmf.digits === '1') {
console.log(`✅ Confirmation received from UUID: ${req.body.uuid}`);
}
if (status) {
console.log(`Call Status Update: ${status} (UUID: ${req.body.uuid})`);
}
res.status(204).send();
});
app.listen(3000, () => console.log('🚀 Webhook server listening on port 3000'));
Create the Broadcast Script (broadcast.js)
Create a file named broadcast.js. This script triggers the outbound calls.
const { Vonage } = require('@vonage/server-sdk');
// --- 1. CONFIGURATION ---
const API_KEY = "YOUR_API_KEY";
const API_SECRET = "YOUR_API_SECRET";
const APP_ID = "YOUR_APP_ID";
const VONAGE_NUMBER = "YOUR_VONAGE_NUMBER";
const TUNNEL_URL = "https://YOUR-SUBDOMAIN.loca.lt" // From Step 3
// --- 2. PLACEHOLDER PROTECTION ---
const config = { API_KEY, API_SECRET, APP_ID, VONAGE_NUMBER, TUNNEL_URL };
Object.keys(config).forEach(key => {
if (config[key].includes("YOUR")) {
console.error(`❌ ERROR: The ${key} placeholder has not been updated!`);
process.exit(1);
}
});
const vonage = new Vonage({
apiKey: API_KEY,
apiSecret: API_SECRET,
applicationId: APP_ID,
privateKey: './private.key'
});
const recipients = [
{ name: 'Alice', number: '15551234567' }
];
async function broadcast() {
for (const person of recipients) {
try {
const result = await vonage.voice.createOutboundCall({
to: [{ type: 'phone', number: person.number }],
from: { type: 'phone', number: VONAGE_NUMBER },
answer_url: [`${TUNNEL_URL}/answer`],
event_url: [`${TUNNEL_URL}/event`]
});
console.log(`☎️ Calling ${person.name}... (UUID: ${result.uuid})`);
} catch (err) {
console.error(`❌ Failed to call ${person.name}:`, err.message);
}
}
}
broadcast();
Replace YOUR_VONAGE_NUMBER with any of your Vonage numbers. If you have a new Vonage account with no numbers yet, you can use the test number 123456789 as the caller ID, and call the number you originally provided during sign-up. Please note that this feature is only available for demo or trial accounts until you add credit to your account.
Test your Notification
- Start your server: Run
node server.jsin your project folder. - Start the broadcast: In a separate terminal, run
node broadcast.js. Your phone will ring, play the message, and log your confirmation to the console when you press "1".
Next Steps
Customization: Change text-to-speech voice or use the stream action in your NCCO to play pre-recorded MP3 files instead of text-to-speech.
Voice AI: Listen to the recipient answer with speech-to-text or connect a voice bot with WebSocket.
Answering Machines: Use answering machine detection to leave a voicemail message.
Scalability: For large lists, implement a queue system to respect Vonage Rate Limits.