Tutorials

Send and Receive SMS and WhatsApp Messages with Node.js and Vonage Messages API

Learn how to build a Node.js Express application to send and receive SMS and WhatsApp messages using the Vonage Messages API. Complete guide with webhooks, security, and deployment.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 2, 2026 β€’ 14 min read

Developer Quick-Start: Learn how to build a Node.js Express application to send and receive SMS and WhatsApp messages using the Vonage Messages API. Complete guide with webhooks, security, and deployment. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.

Build a Node.js application using Express to send and receive SMS and WhatsApp messages via the Vonage Messages API. This guide covers project setup, configuration, webhook handling, security implementation, and deployment preparation.

By the end of this tutorial, you will have a functional application capable of:

  • Sending SMS messages programmatically.
  • Sending WhatsApp messages using the Vonage WhatsApp Sandbox for testing.
  • Receiving incoming SMS and WhatsApp messages via webhooks.
  • Verifying incoming webhook requests for security.
  • Handling basic errors and logging essential information.

This enables your application to communicate with users via SMS and WhatsApp for notifications, alerts, two-factor authentication (2FA), customer support, and more – all through a unified API interface.

Technologies used

  • Node.js: JavaScript runtime environment for building server-side applications.
  • Express: Minimal and flexible Node.js web application framework.
  • Vonage Messages API: Unified API for sending and receiving messages across multiple channels (SMS, MMS, WhatsApp, Facebook Messenger, Viber).
  • Vonage Node SDK: Simplifies interaction with Vonage APIs in Node.js. This guide uses these SDK modules:
  • @vonage/server-sdk: Core client initialization and authentication.
  • @vonage/messages: Constructing and sending messages via the Messages API.
  • @vonage/jwt: Verifying signatures of incoming webhooks.
  • ngrok: Tool to expose local development servers to the internet for webhook testing.
  • dotenv: Module to load environment variables from a .env file.

Prerequisites

Before you start, ensure you have:

  1. Vonage API Account: Sign up for a free Vonage API account. You'll receive free credit to start building.
  2. Node.js: Install Node.js version 18 or higher from nodejs.org.
  3. ngrok: Install ngrok and create a free account at ngrok.com.
  4. WhatsApp Account: Personal WhatsApp account on your smartphone for testing the WhatsApp Sandbox integration.
  5. Vonage Phone Number: Purchase a virtual phone number from your Vonage dashboard to send and receive SMS. Navigate to Numbers > Buy numbers.

System architecture

Your application interacts with the Vonage platform in two ways:

  1. Sending Messages: Your Node.js app uses the Vonage SDK to call the Vonage Messages API, specifying the channel (SMS or WhatsApp), recipient, sender ID (your Vonage number or WhatsApp Sandbox number), and message content. Vonage delivers the message via the respective network.
  2. Receiving Messages: When a user sends a message to your Vonage number (SMS) or the WhatsApp Sandbox number, Vonage sends an HTTP POST request (webhook) containing the message details to a preconfigured URL endpoint hosted by your Node.js application. During development, ngrok forwards these requests from the public internet to your local machine.
graph TD
    subgraph Your Application
        NodeApp[Node.js/Express App]
        EnvFile[.env File] --> NodeApp
    end

    subgraph Vonage Platform
        MessagesAPI[Messages API]
        VonageNumber[Vonage SMS Number]
        WhatsAppSandbox[WhatsApp Sandbox]
        VonageApp[Vonage Application Config]
        Webhooks[Webhook Service]
    end

    subgraph External Networks
        SMSNetwork[SMS Network]
        WhatsAppNetwork[WhatsApp Network]
    end

    UserSMS[User (SMS)]
    UserWhatsApp[User (WhatsApp)]

    NodeApp -- Send Request (SDK) --> MessagesAPI
    MessagesAPI -- Deliver SMS --> SMSNetwork --> UserSMS
    MessagesAPI -- Deliver WhatsApp --> WhatsAppNetwork --> UserWhatsApp

    UserSMS -- Sends SMS --> SMSNetwork -- Inbound SMS --> VonageNumber
    UserWhatsApp -- Sends WhatsApp --> WhatsAppNetwork -- Inbound WhatsApp --> WhatsAppSandbox

    VonageNumber -- Associated with --> VonageApp
    WhatsAppSandbox -- Triggers Webhook --> Webhooks
    VonageApp -- Webhook URLs --> Webhooks

    Webhooks -- POST Request --> Ngrok[ngrok Tunnel] --> NodeApp

    style UserSMS fill:#f9f,stroke:#333,stroke-width:2px
    style UserWhatsApp fill:#9cf,stroke:#333,stroke-width:2px

Note: If this Mermaid diagram doesn't render in your Markdown processor, generate a static image as a fallback and include appropriate alt text.

1. Setting up the project

Initialize your Node.js project and install the necessary dependencies.

  1. Create Project Directory: Open your terminal and create a new directory for your project, then navigate into it.
   mkdir vonage-node-messaging
   cd vonage-node-messaging
  1. Initialize Node.js Project: Initialize a new Node.js project using npm. The -y flag accepts default settings.
   npm init -y

This creates a package.json file.

  1. Install Dependencies: Install Express for the web server, the Vonage SDKs for API interaction, and dotenv for managing environment variables.
   npm install express @vonage/server-sdk @vonage/messages @vonage/jwt dotenv
  1. Create Project Structure: Create a simple structure for your source code.
   mkdir src
   touch src/server.js
   touch .env
   touch .gitignore
  1. Configure .gitignore: Add node_modules and .env to your .gitignore file to prevent committing them to version control. Never commit sensitive credentials.

.gitignore

   node_modules/
   .env
  1. Set Up Environment Variables (.env): Open the .env file and add the following variables. You'll fill these in during the Vonage setup section.

.env

   # .env

   # Vonage API Credentials (found on Vonage Dashboard homepage)
   VONAGE_API_KEY=YOUR_API_KEY
   VONAGE_API_SECRET=YOUR_API_SECRET

   # Vonage Application Details (generated when creating a Vonage Application)
   VONAGE_APPLICATION_ID=YOUR_APPLICATION_ID
   VONAGE_PRIVATE_KEY=./private.key # Path to your downloaded private key file (for local dev)

   # Vonage Numbers (your purchased Vonage number and the WhatsApp Sandbox number)
   VONAGE_SMS_FROM_NUMBER=YOUR_VONAGE_SMS_NUMBER
   # Verify the Sandbox number from your Vonage Dashboard (Messages API Sandbox page)
   VONAGE_WHATSAPP_SANDBOX_NUMBER=14157386102

   # Webhook Security (found in Vonage Dashboard > Settings)
   VONAGE_SIGNATURE_SECRET=YOUR_SIGNATURE_SECRET

   # Server Configuration
   PORT=8000 # Port for the Express server
  • VONAGE\_API\_KEY, VONAGE\_API\_SECRET: Your main account credentials.
  • VONAGE\_APPLICATION\_ID: Identifies your specific Vonage application configuration.
  • VONAGE\_PRIVATE\_KEY: Path to the private key file used for authenticating requests related to your Vonage application (required for Messages API). For deployment, handle this differently (see Section 8).
  • VONAGE\_SMS\_FROM\_NUMBER: Your purchased Vonage number in E.164 format (e.g., 12015550123).
  • VONAGE\_WHATSAPP\_SANDBOX\_NUMBER: The number provided by Vonage for the WhatsApp Sandbox. Verify this on your dashboard.
  • VONAGE\_SIGNATURE\_SECRET: Used to verify that incoming webhooks originated from Vonage.
  • PORT: The local port your Express server listens on.

2. Vonage account and application setup

Configure your Vonage account and create a Vonage Application to handle messaging.

  1. Log in to Vonage Dashboard: Access your Vonage API Dashboard.
  2. Find API Key and Secret: On the dashboard homepage, locate your API key and API secret. Copy these values into the corresponding VONAGE\_API\_KEY and VONAGE\_API\_SECRET variables in your .env file.
  3. Create a Vonage Application:
  • Navigate to Applications > Create a new application.
  • Give your application a name (e.g., "Node Messaging App").
  • Click Generate public and private key. Important: A private.key file will download. Save this file in your project's root directory (same level as your .env file). Ensure the VONAGE\_PRIVATE\_KEY path in your .env file points to this file (e.g., ./private.key).
  • Enable Messages under Capabilities. Configure the webhook URLs later after starting ngrok. Leave the Inbound URL and Status URL fields blank for now.
  • Click Generate new application.
  • On the next screen, copy the Application ID and paste it into the VONAGE\_APPLICATION\_ID variable in your .env file.
  1. Link Your Vonage SMS Number:
  • Go back to your Application's settings page.
  • Scroll down to Link virtual numbers.
  • Find the Vonage phone number you purchased earlier and click Link. This associates incoming SMS messages on this number with your application's webhooks.
  • Copy this number in E.164 format (e.g., 12015550123) into the VONAGE\_SMS\_FROM\_NUMBER variable in your .env file.
  1. Find Signature Secret:
  • Navigate to Settings in the main dashboard menu.
  • Under API settings, find your Signature secret. Copy this value and paste it into the VONAGE\_SIGNATURE\_SECRET variable in your .env file.
  • Important: Ensure Default SMS Setting is set to Use the Messages API. Save changes if needed.
  1. Set Up WhatsApp Sandbox:
  • Navigate to Messages API Sandbox in the sidebar (under Build & Manage).
  • You'll see QR codes and instructions to link your personal WhatsApp number to the sandbox. Scan the QR code with your phone's WhatsApp app or send the specified message to the Vonage Sandbox number (+14157386102).
  • Follow the prompts in WhatsApp to allowlist your number for testing.
  • Verify the VONAGE\_WHATSAPP\_SANDBOX\_NUMBER in your .env file matches the number shown on the Sandbox page.
  • Add webhook URLs to the Sandbox configuration later.

3. Setting up the Express server

Now let's write the basic Express server code in src/server.js.

require('dotenv').config();
const express = require('express');
const { Vonage } = require('@vonage/server-sdk');
const { WhatsAppText } = require('@vonage/messages');
const { verifySignature } = require('@vonage/jwt');
const fs = require('fs');

const app = express();

app.use(express.json());
app.use(express.urlencoded({ extended: true }));

const requiredEnv = [
  'VONAGE_API_KEY', 'VONAGE_API_SECRET', 'VONAGE_APPLICATION_ID',
  'VONAGE_PRIVATE_KEY', 'VONAGE_SMS_FROM_NUMBER',
  'VONAGE_WHATSAPP_SANDBOX_NUMBER', 'VONAGE_SIGNATURE_SECRET', 'PORT'
];
const missingEnv = requiredEnv.filter(key => !process.env[key]);
if (missingEnv.length > 0) {
  console.error(`Error: Missing required environment variables: ${missingEnv.join(', ')}`);
  console.error('Please check your .env file or environment configuration.');
  process.exit(1);
}

const PORT = process.env.PORT || 8000;
const VONAGE_SIGNATURE_SECRET = process.env.VONAGE_SIGNATURE_SECRET;

let privateKeyValue;
if (process.env.VONAGE_PRIVATE_KEY_CONTENT) {

    privateKeyValue = process.env.VONAGE_PRIVATE_KEY_CONTENT.replace(/\\n/g, '\n');
} else if (fs.existsSync(process.env.VONAGE_PRIVATE_KEY)) {

    privateKeyValue = fs.readFileSync(process.env.VONAGE_PRIVATE_KEY);
} else {
    console.error('Error: VONAGE_PRIVATE_KEY path is invalid or VONAGE_PRIVATE_KEY_CONTENT env var is not set.');
    process.exit(1);
}

const vonage = new Vonage({
  apiKey: process.env.VONAGE_API_KEY,
  apiSecret: process.env.VONAGE_API_SECRET,
  applicationId: process.env.VONAGE_APPLICATION_ID,
  privateKey: privateKeyValue
}, {


  apiHost: 'https://messages-sandbox.nexmo.com'
});

console.log('Vonage client initialized.');

const verifyVonageSignature = (req, res, next) => {
  try {
    const authHeader = req.headers.authorization;
    if (!authHeader || !authHeader.startsWith('Bearer ')) {
      console.warn('Webhook Error: Missing or invalid Authorization header.');
      return res.status(401).send('Unauthorized: Missing or invalid token');
    }
    const token = authHeader.split(' ')[1];




    if (verifySignature(token, VONAGE_SIGNATURE_SECRET, req.body)) {
      console.log('Webhook Signature Verified Successfully.');
      next();
    } else {
      console.warn('Webhook Error: Invalid signature.');
      res.status(401).send('Unauthorized: Invalid signature');
    }
  } catch (error) {
    console.error('Webhook Signature Verification Error:', error);
    res.status(500).send('Internal Server Error during signature verification');
  }
};

app.post('/api/send-message', (req, res) => {
  res.status(501).send({ message: 'Send message endpoint not implemented yet.' });
});

app.post('/webhooks/inbound', verifyVonageSignature, (req, res) => {
  console.log('Received Inbound Webhook (Stub)');
  res.status(200).send('OK');
});

app.post('/webhooks/status', verifyVonageSignature, (req, res) => {
  console.log('Received Status Webhook (Stub)');
  res.status(200).send('OK');
});

app.use((err, req, res, next) => {
  console.error("Unhandled Error:", err.stack || err);
  res.status(500).send('Something broke!');
});

app.listen(PORT, () => {
  console.log(`Server is running on http://localhost:${PORT}`);
  console.log('Ensure ngrok is running and webhooks are configured correctly.');
});

Explanation:

  1. Load Env Vars: require('dotenv').config() loads variables from .env.
  2. Initialize Express: Sets up the Express application and middleware for parsing request bodies.
  3. Env Var Check: Verifies that all necessary environment variables are present.
  4. Initialize Vonage Client: Creates a Vonage instance. It now attempts to read the private key content either directly from an environment variable VONAGE\_PRIVATE\_KEY\_CONTENT (for deployment) or from the file path specified in VONAGE\_PRIVATE\_KEY (for local development). The apiHost is set specifically for the WhatsApp Sandbox. Remove or change this for production WhatsApp.
  5. Webhook Security Middleware (verifyVonageSignature): Intercepts webhook requests, extracts the JWT token, verifies it using verifySignature, and passes control or sends an error response. Crucially, webhooks must be secured.
  6. Route Placeholders: Defines basic routes, applying verifyVonageSignature only to webhook routes.
  7. Basic Error Handler: A simple catch-all middleware.
  8. Start Server: Starts the Express server.

4. Sending messages

Let's implement the functions to send SMS and WhatsApp messages.

  1. Add Sending Functions: Add these functions inside src/server.js before the route definitions.
const sendSmsMessage = async (toNumber, text) => {
  const fromNumber = process.env.VONAGE_SMS_FROM_NUMBER;
  console.log(`Attempting to send SMS from ${fromNumber} to ${toNumber}`);
  try {
    const response = await vonage.messages.send({
      message_type: "text",
      text: text,
      to: toNumber,
      from: fromNumber,
      channel: "sms"
    });

    console.log(`SMS sent successfully to ${toNumber}. Message UUID: ${response.messageUuid}`);
    return response.messageUuid;
  } catch (error) {
    console.error(`Error sending SMS to ${toNumber}:`, error.response ? error.response.data : error.message);

    throw new Error(`Failed to send SMS: ${error.message}`);
  }
};

const sendWhatsAppMessage = async (toNumber, text) => {

  const fromNumber = process.env.VONAGE_WHATSAPP_SANDBOX_NUMBER;
  console.log(`Attempting to send WhatsApp message from ${fromNumber} to ${toNumber}`);
  try {

    const response = await vonage.messages.send(
      new WhatsAppText({
        text: text,
        to: toNumber,
        from: fromNumber,
      })
    );

    console.log(`WhatsApp message sent successfully to ${toNumber}. Message UUID: ${response.messageUuid}`);
    return response.messageUuid;
  } catch (error) {
    console.error(`Error sending WhatsApp message to ${toNumber}:`, error.response ? error.response.data : error.message);

    throw new Error(`Failed to send WhatsApp message: ${error.message}`);
  }
};

app.post('/api/send-message', async (req, res) => {
  const { to, type, text } = req.body;


  if (!to || !type || !text) {
    return res.status(400).send({ error: 'Missing required fields: to, type, text' });
  }
  if (type !== 'sms' && type !== 'whatsapp') {
    return res.status(400).send({ error: 'Invalid type. Must be "sms" or "whatsapp"' });
  }


  try {
    let messageUuid;
    if (type === 'sms') {
      messageUuid = await sendSmsMessage(to, text);
    } else {

      console.warn(`Sending WhatsApp to ${to}. Ensure this number is allowlisted in the Sandbox.`);
      messageUuid = await sendWhatsAppMessage(to, text);
    }
    res.status(200).send({ message: `${type.toUpperCase()} sent successfully`, messageUuid });
  } catch (error) {
    console.error(`Failed to send message via API endpoint: ${error.message}`);

    res.status(500).send({ error: `Failed to send ${type.toUpperCase()} message.`, details: error.message });
  }
});

Explanation:

  • sendSmsMessage: Sends SMS using vonage.messages.send with channel: "sms".
  • sendWhatsAppMessage: Sends WhatsApp using the WhatsAppText constructor and the Sandbox number.
  • API Endpoint (/api/send-message): Handles POST requests to send messages, validates input, and calls the appropriate sending function.

5. Receiving messages (webhooks)

To receive messages, we need to expose our local server to the internet using ngrok and configure Vonage to send webhooks to that public URL.

  1. Start ngrok: Open a new terminal window and start ngrok, pointing it to the port your Express server is listening on (e.g., 8000).
   ngrok http 8000

Copy the https:// Forwarding URL (e.g., https://<unique-subdomain>.ngrok-free.app). This is your public webhook base URL.

  1. Configure Vonage Webhooks:
  • Application Webhooks (for SMS):
  • Go back to your Vonage Application settings in the dashboard.
  • Under Messages capabilities, paste your ngrok Forwarding URL into the Inbound URL field, appending /webhooks/inbound.
  • Paste your ngrok Forwarding URL into the Status URL field, appending /webhooks/status.
  • Click Save changes.
  • WhatsApp Sandbox Webhooks:
  • Navigate to the Messages API Sandbox page in the dashboard.
  • Click on the Webhooks tab.
  • Paste your ngrok URL + /webhooks/inbound into the Inbound URL field.
  • Paste your ngrok URL + /webhooks/status into the Status URL field.
  • Click Save webhooks.
  1. Implement Webhook Handlers: Update the placeholder webhook routes in src/server.js to actually process the incoming data.
app.post('/webhooks/inbound', verifyVonageSignature, (req, res) => {
  console.log('--- INBOUND WEBHOOK RECEIVED ---');
  console.log('Timestamp:', new Date().toISOString());

  console.log('Request Body:', JSON.stringify(req.body, null, 2));

  const { channel, message_type, from, to, text, message_uuid } = req.body;


  if (channel === 'sms') {
    console.log(`Received SMS from ${from} to ${to}`);
    if (message_type === 'text' && text) {
      console.log(`Message Text: "${text}"`);



    }
  } else if (channel === 'whatsapp') {
    console.log(`Received WhatsApp message from ${from} to ${to}`);
    if (message_type === 'text' && text) {
      console.log(`Message Text: "${text}"`);



    }



  } else {
    console.log(`Received message on unhandled channel: ${channel}`);
  }


  res.status(200).send('OK');
});

app.post('/webhooks/status', verifyVonageSignature, (req, res) => {
  console.log('--- STATUS WEBHOOK RECEIVED ---');
  console.log('Timestamp:', new Date().toISOString());

  console.log('Request Body:', JSON.stringify(req.body, null, 2));

  const { message_uuid, status, timestamp, error, usage } = req.body;

  console.log(`Status update for Message UUID: ${message_uuid}`);
  console.log(`Status: ${status} at ${timestamp}`);

  if (error) {
    console.error(`Error Code: ${error.code}, Reason: ${error.reason}`);



  }

  if (usage) {
    console.log(`Message Cost: ${usage.price} ${usage.currency}`);

  }


  switch (status) {
      case 'delivered':
          console.log('Message successfully delivered.');
          break;
      case 'read':
          console.log('WhatsApp message read by recipient.');
          break;
      case 'rejected':
      case 'undeliverable':
          console.warn(`Message failed with status: ${status}`);

          break;
      default:
          console.log(`Received status: ${status}`);
  }


  res.status(200).send('OK');
});

Explanation:

  • Inbound Webhook (/webhooks/inbound): Logs the full incoming payload using JSON.stringify for better structure. Differentiates processing based on channel. Responds with 200 OK promptly.
  • Status Webhook (/webhooks/status): Logs the status update payload using JSON.stringify. Provides details on message delivery, errors, and usage. Responds with 200 OK promptly.

6. Error handling and logging refinement

While we have basic try...catch and logging, let's refine it slightly. For production, consider dedicated logging libraries (pino, winston) and error tracking services (Sentry, Rollbar).

  • Structured Logging: Using JSON.stringify in the webhook handlers (as implemented above) is a step towards structured logging, making logs easier to parse. You can enhance this further by adopting a standard log format across your application.
  console.log(JSON.stringify({
    level: 'info',
    timestamp: new Date().toISOString(),
    message: 'Received inbound SMS',
    details: { from: from, to: to, messageId: message_uuid }
  }));

  console.error(JSON.stringify({
      level: 'error',
      timestamp: new Date().toISOString(),
      message: 'Failed to send SMS',
      error: { message: error.message, stack: error.stack },
      context: { recipient: toNumber, function: 'sendSmsMessage' }
  }));
  • Specific Error Handling: Catch specific Vonage API errors if possible by inspecting error.response.data or error.message for details provided by Vonage.
  • Webhook Retries: Remember Vonage retries webhooks if they don't receive a 200 OK. Your webhook handler should be idempotent (safe to run multiple times with the same input) or store processed message\_uuids to prevent duplicate actions.
  • Retry Mechanisms (Sending): For transient network errors when sending messages, consider implementing a simple retry strategy with exponential backoff (e.g., using libraries like async-retry).

7. Verification and testing

Now, let's test the complete flow.

  1. Ensure Services are Running:
  • Your Node.js server (node src/server.js in one terminal).
  • ngrok (ngrok http <PORT> in another terminal).
  1. Test Sending SMS:
  • Use curl or Postman. Replace <YOUR\_PERSONAL\_PHONE\_NUMBER> (E.164 format).
   curl -X POST http://localhost:8000/api/send-message \
        -H "Content-Type: application/json" \
        -d '{
              "to": "<YOUR_PERSONAL_PHONE_NUMBER>",
              "type": "sms",
              "text": "Hello from Vonage Node App! (SMS)"
            }'
  • Check: SMS received, server logs success, status webhooks arrive.
  1. Test Sending WhatsApp:
  • Ensure your number is allowlisted in the Sandbox. Replace <YOUR\_PERSONAL\_WHATSAPP\_NUMBER> (E.164 format).
   curl -X POST http://localhost:8000/api/send-message \
        -H "Content-Type: application/json" \
        -d '{
              "to": "<YOUR_PERSONAL_WHATSAPP_NUMBER>",
              "type": "whatsapp",
              "text": "Hello from Vonage Node App! (WhatsApp Sandbox)"
            }'
  • Check: WhatsApp message received, server logs success, status webhooks arrive.
  1. Test Receiving SMS:
  • From your personal phone, send an SMS to your VONAGE\_SMS\_FROM\_NUMBER.
  • Check: ngrok shows POST /webhooks/inbound, server logs signature verification, inbound webhook details (channel: sms), and message text.
  1. Test Receiving WhatsApp:
  • From your personal WhatsApp, send a message to the VONAGE\_WHATSAPP\_SANDBOX\_NUMBER (+14157386102).
  • Check: ngrok shows POST /webhooks/inbound, server logs signature verification, inbound webhook details (channel: whatsapp), and message text.

8. Deployment and CI/CD considerations

Deploying this application requires replacing ngrok with a permanent public URL and managing environment variables securely.

  1. Choose a Hosting Platform: Options include Heroku, Vercel, AWS, Google Cloud, Azure, etc.
  2. Environment Variables: Configure your environment variables (VONAGE\_API\_KEY, VONAGE\_API\_SECRET, etc.) directly on the hosting platform. Do not commit your .env file.
  • Private Key Handling: Instead of using a file path for VONAGE\_PRIVATE\_KEY, set the content of your private.key file as an environment variable (e.g., VONAGE\_PRIVATE\_KEY\_CONTENT). Many platforms support multi-line environment variables. Ensure your src/server.js reads this variable, as shown in the updated initialization code in Section 3. You might need to handle newline characters (\\n) correctly (e.g., replace literal \\n with actual newlines if your platform escapes them).
  • Example Code Modification (already done in Section 3): The Vonage client initialization in src/server.js now checks for process.env.VONAGE\_PRIVATE\_KEY\_CONTENT first before falling back to the file path.
  1. Public URL: Your hosting platform will provide a public URL (e.g., https://your-app-name.herokuapp.com).
  2. Update Vonage Webhooks: Replace the ngrok URLs in your Vonage Application and Messages API Sandbox settings with your permanent public URL (e.g., https://your-app-name.herokuapp.com/webhooks/inbound and /status).
  3. Procfile (for Heroku/similar): You might need a Procfile:

Procfile

   web: node src/server.js
  1. CI/CD Pipeline: Set up a pipeline (GitHub Actions, GitLab CI, etc.) for automated testing, building, and deployment, managing environment variables securely.

9. Troubleshooting and caveats

  • Invalid Credentials: Double-check API keys, secret, Application ID, and private key content/path.
  • Webhook Failures (401 Unauthorized): Incorrect VONAGE\_SIGNATURE\_SECRET or issues with the verifySignature implementation.
  • Webhook Failures (No Response / Timeouts): Server not responding 200 OK quickly, or the server crashed. Check server logs. Vonage expects a response within a few seconds.
  • WhatsApp Sandbox Limitations: Only works with allowlisted numbers. Message templates are required for production WhatsApp outside the 24-hour customer care window. The apiHost needs to be changed for production WhatsApp.
  • Rate Limits: Be aware of Vonage API rate limits. Implement appropriate error handling and backoff strategies.
  • Number Formatting: Ensure all phone numbers are in E.164 format (e.g., +12015550123).
  • SMS Sender ID: SMS sender ID behavior varies by country. Your Vonage number might be overwritten in some regions.
  • Idempotency: Ensure webhook handlers are idempotent to handle potential retries from Vonage safely.

Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)

If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.

curl -X POST https://api.sendsmsnokyc.com/messages \
  -H "x-api-key: $SMSROUTE_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "to": ["+14155551234"],
    "channel": ["sms"],
    "body": "Your verification code is 482910"
  }'
  • Instant onboarding: No KYC paperwork or sales queues β€” grab an API key and fund with crypto (BTC/USDT) or card.
  • Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
  • Transparent global pricing: Compare exact per-country termination rates on our live pricing page.

Ship Global SMS in One API Call

Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.

Explore SMSRoute Developers β†’