Tutorials

Developer Guide: Node.js & Express Two-Way SMS with Vonage Messages API

A comprehensive guide to building a Node.js/Express application for sending and receiving SMS messages using the Vonage Messages API, covering setup, webhooks, security, and deployment.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 6, 2026 β€’ 12 min read

Developer Quick-Start: A comprehensive guide to building a Node.js/Express application for sending and receiving SMS messages using the Vonage Messages API, covering setup, 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.

This guide provides a comprehensive walkthrough for building a Node.js application using the Express framework to both send outbound SMS messages and receive inbound SMS messages via webhooks, leveraging the Vonage Messages API.

We will cover everything from initial project setup and Vonage configuration to implementing core messaging logic, handling webhooks, ensuring security, and preparing for deployment. By the end, you'll have a functional foundation for two-way SMS communication.

Project Goal: To create a simple Node.js server that can:

  1. Send an SMS message to a specified phone number via the Vonage Messages API.
  2. Receive incoming SMS messages sent to a Vonage virtual number via a webhook endpoint.

Technology Stack:

  • Node.js: JavaScript runtime environment.
  • Express: Minimalist web framework for Node.js, used to create the webhook endpoint.
  • Vonage Messages API: Vonage's unified API for sending and receiving messages across various channels (we'll focus on SMS).
  • @vonage/server-sdk: The official Vonage Node.js SDK for interacting with the API.
  • ngrok: A tool to expose local servers to the internet for webhook testing during development.
  • dotenv: Module to load environment variables from a .env file.

System Architecture:

+-------------+      +-----------------+      +-----------------+      +-------------+
| Your Phone  |<---->| Vonage Platform |<---->| Your Node.js App|      | Developer   |
| (End User)  |      | (Messages API)  |      | (Express Server)|<---->|             |
+-------------+      +-----------------+      +-----------------+      +-------------+
       ^                     |                        ^
       | SMS                 | Webhook Call           | API Call
       |                     v                        |
       +---------------------+------------------------+
             (Send/Receive Flow)

Prerequisites:

  • Node.js and npm (or yarn): Installed on your development machine. (Download Node.js)
  • Vonage API Account: Sign up for free at Vonage. Note your API Key and Secret from the dashboard.
  • Vonage Virtual Number: Purchase an SMS-capable number through the Vonage dashboard (Numbers > Buy numbers).
  • ngrok: Installed and authenticated. (Download ngrok). A free account is sufficient.
  • Basic understanding of Node.js, Express, and REST APIs.

1. Project Setup

Let's initialize our 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-sms-app
   cd vonage-sms-app
  1. Initialize Node.js Project: This creates a package.json file.
   npm init -y
  1. Install Dependencies: We need the Vonage SDK, the Express framework, and dotenv for managing environment variables.
   npm install @vonage/server-sdk express dotenv
  1. Create Project Structure: Create the necessary files and directories.
   touch server.js send-sms.js .env .gitignore
  • server.js: Will contain our Express server code for handling inbound webhooks.
  • send-sms.js: A simple script to demonstrate sending an outbound SMS.
  • .env: Stores sensitive credentials like API keys (will be ignored by Git).
  • .gitignore: Specifies files/directories Git should ignore (like .env and node\_modules).
  1. Configure .gitignore: Add the following lines to your .gitignore file to prevent committing sensitive information and dependencies:
   # Dependencies
   node_modules

   # Environment variables
   .env

   # Vonage Private Key
   private.key

   # OS generated files
   .DS_Store
   Thumbs.db

2. Vonage Account and Application Setup

Before writing code, we need to configure Vonage correctly.

  1. API Credentials: Locate your API Key and API Secret on the main page of your Vonage API Dashboard. While the Messages API primarily uses the Application ID and Private Key for authentication in this guide, having the Key/Secret handy is useful.
  2. Choose API for SMS: Vonage offers two APIs for SMS (SMS API and Messages API). The Messages API is more modern and versatile. Ensure it's set as the default for your account:
  • Go to your Vonage API Dashboard.
  • Navigate to API Settings in the left-hand menu.
  • Scroll down to SMS Settings.
  • Under ""Default SMS Setting"", select Messages API.
  • Click Save changes.
  1. Create a Vonage Application: Applications act as containers for your communication configurations, including webhook URLs and authentication methods.
  • Navigate to Applications in the dashboard (Applications > Create a new application).
  • Enter an Application name (e.g., ""Node Express SMS App"").
  • Click Generate public and private key. Crucially, save the private.key file that downloads. We recommend initially saving it in your project's root directory (vonage-sms-app/private.key). Remember, this file is sensitive and should not be committed to Git (it's already in .gitignore).
  • Security Note: For production environments, avoid storing the key file directly within the project. Best practices include storing it securely outside the project directory and referencing its path via an environment variable, or even better, loading the content of the key directly from an environment variable (see Deployment section).
  • Enable the Messages capability.
  • You'll see fields for Inbound URL and Status URL. We'll fill these in later once we have our ngrok tunnel running. For now, you can enter temporary placeholders like https://example.com/webhooks/inbound and https://example.com/webhooks/status.
  • Click Generate new application.
  • Note the Application ID that is generated – you will need this.
  1. Link Your Vonage Number: Associate your purchased Vonage virtual number with the application you just created.
  • Navigate to Numbers > Your numbers.
  • Find the virtual number you want to use for receiving SMS.
  • Click the Manage button (or the pencil icon) next to the number.
  • In the Applications dropdown under Forward to, select the application you created (""Node Express SMS App"").
  • Click Save. Now, any SMS sent to this number will trigger events directed to the webhook URLs defined in your application.

3. Environment Variables

Store your sensitive credentials and configuration in the .env file. Never hardcode credentials directly in your source code.

Populate your .env file with the following, replacing the placeholder values:

.env

# .env

# Vonage Application Credentials (from Application setup)
VONAGE_APPLICATION_ID=YOUR_APPLICATION_ID
VONAGE_PRIVATE_KEY_PATH=./private.key # Path relative to project root

# Vonage Virtual Number (The one linked to your application, E.164 format recommended)
VONAGE_NUMBER=+14155550100 # Example: Use your actual number with country code

# Test Recipient Number (Your mobile number, for sending tests, E.164 format recommended)
YOUR_PHONE_NUMBER=+14155550199 # Example: Use your actual number with country code

# Optional: Vonage API Key/Secret (Needed if using other APIs or different auth)
# VONAGE_API_KEY=YOUR_API_KEY
# VONAGE_API_SECRET=YOUR_API_SECRET
  • Replace YOUR\_APPLICATION\_ID with the actual value from your Vonage application settings.
  • Ensure VONAGE\_PRIVATE\_KEY\_PATH points to the correct location of your downloaded private.key file (relative to where you run the node scripts).
  • Replace +14155550100 with your Vonage number in E.164 format (including the + and country code).
  • Replace +14155550199 with your personal mobile number, also in E.164 format, for testing.
  • The VONAGE\_API\_KEY and VONAGE\_API\_SECRET are commented out as they are not strictly required for the Messages API when using Application ID/Private Key authentication as shown in this guide, but you might uncomment and fill them if needed for other purposes.

4. Implementing SMS Sending (send-sms.js)

This script demonstrates how to send an outbound SMS using the Vonage SDK with Application ID and Private Key authentication.

require('dotenv').config();
const { Vonage } = require('@vonage/server-sdk');
const { SMS } = require('@vonage/messages');

const vonageAppId = process.env.VONAGE_APPLICATION_ID;
const vonagePrivateKeyPath = process.env.VONAGE_PRIVATE_KEY_PATH;
const vonageNumber = process.env.VONAGE_NUMBER;
const recipientNumber = process.env.YOUR_PHONE_NUMBER;
const messageText = `Hello from Vonage and Node.js! (Sent: ${new Date().toLocaleTimeString()})`;

if (!vonageAppId || !vonagePrivateKeyPath || !vonageNumber || !recipientNumber) {
  console.error(""Error: Missing required environment variables (VONAGE_APPLICATION_ID, VONAGE_PRIVATE_KEY_PATH, VONAGE_NUMBER, YOUR_PHONE_NUMBER). Check your .env file."");
  process.exit(1);
}

const vonage = new Vonage({
    applicationId: vonageAppId,
    privateKey: vonagePrivateKeyPath,
});

async function sendSms() {
    console.log(`Attempting to send SMS from ${vonageNumber} to ${recipientNumber}`);
    try {
        const resp = await vonage.messages.send(
            new SMS(
                {
                    to: recipientNumber,
                    from: vonageNumber,
                    text: messageText,
                }
            )
        );

        console.log('Message sent successfully!');
        console.log('Message UUID:', resp.messageUuid);

    } catch (err) {
        console.error('Error sending SMS:');
        if (err.response) {

            console.error('Status:', err.response.status);
            console.error('Data:', err.response.data);
        } else {

            console.error(err);
        }
        process.exitCode = 1;
    }
}

sendSms();

Explanation:

  1. require('dotenv').config(): Loads the variables from your .env file into process.env.
  2. Import Vonage and SMS: We import the main SDK class and the specific SMS class from the @vonage/messages sub-module.
  3. Configuration: Reads the necessary Application ID, private key path, and phone numbers from process.env. Includes basic validation.
  4. Initialize Vonage: Creates an instance of the Vonage client using only the applicationId and privateKey for authentication, which is standard for the Messages API.
  5. sendSms Function:
  • Uses an async function to work with the promise-based SDK methods.
  • Calls vonage.messages.send(), passing an instance of the SMS class.
  • The SMS constructor takes an object with to, from, and text properties.
  • Uses a try...catch block for robust error handling.
  • Logs the messageUuid for tracking.
  1. Execute: Calls the sendSms() function.

To Test Sending: Run the script from your terminal:

node send-sms.js

You should see output indicating success or failure, and shortly after, receive the SMS on the phone number specified in YOUR_PHONE_NUMBER.


5. Implementing SMS Receiving (Webhook Server - server.js)

This Express server listens for incoming POST requests from Vonage when an SMS is sent to your virtual number.

require('dotenv').config();
const express = require('express');
const { json, urlencoded } = express;

const app = express();
const port = process.env.PORT || 3000;

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

app.post('/webhooks/inbound', (req, res) => {
    console.log('--- Inbound SMS Received ---');
    console.log('Timestamp:', new Date().toISOString());
    console.log('Request Body:', JSON.stringify(req.body, null, 2));


    const { msisdn, to, text, messageId, 'message-timestamp': messageTimestamp } = req.body;

    if (msisdn && to && text) {
        console.log(`From: ${msisdn}`);
        console.log(`To: ${to}`);
        console.log(`Text: ${text}`);
        console.log(`Message ID: ${messageId}`);
        console.log(`Message Timestamp: ${messageTimestamp}`);






    } else {
        console.warn('Received incomplete or unexpected webhook payload.');
    }



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

app.post('/webhooks/status', (req, res) => {
    console.log('--- Message Status Update ---');
    console.log('Timestamp:', new Date().toISOString());
    console.log('Request Body:', JSON.stringify(req.body, null, 2));







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

app.get('/health', (req, res) => {
    res.status(200).json({ status: 'UP', timestamp: new Date().toISOString() });
});

app.listen(port, () => {
    console.log(`Server listening for webhooks at http://localhost:${port}`);
    console.log(`Inbound SMS webhook expected at POST /webhooks/inbound`);
    console.log(`Status webhook expected at POST /webhooks/status`);
});

Explanation:

  1. Imports & Middleware: Sets up Express and body parsing. A comment highlights where security middleware should go.
  2. /webhooks/inbound Endpoint (POST):
  • Logs the received req.body. Note: The specific fields (msisdn, to, text, etc.) should be verified against the current Vonage Messages API documentation, as they can occasionally change. The code includes common fields as an example.
  • Sends back 200 OK to acknowledge receipt.
  • Includes a placeholder for custom application logic.
  1. /webhooks/status Endpoint (POST):
  • Logs status updates for outbound messages. Note: The fields in the commented-out example (message\_uuid, status, timestamp) should also be verified against current Vonage documentation for status webhooks.
  • Sends back 200 OK.
  1. /health Endpoint (GET): Basic health check.
  2. app.listen: Starts the server.

6. Local Development with ngrok

Use ngrok to expose your local server to the internet for Vonage webhooks.

  1. Start Your Server:
   node server.js
  1. Start ngrok: In a separate terminal:
   ngrok http 3000
  1. Get ngrok URL: Copy the HTTPS Forwarding URL provided by ngrok (e.g., https://xxxxxxxx.ngrok.io).
  2. Update Vonage Application Webhooks:
  • Go to your Vonage Application settings (Applications).
  • Edit your application.
  • Update the Messages capability URLs:
  • Inbound URL: YOUR\_NGROK\_HTTPS\_URL/webhooks/inbound
  • Status URL: YOUR\_NGROK\_HTTPS\_URL/webhooks/status
  • Save changes.

7. Verification and Testing

  1. Ensure server.js and ngrok are running.
  2. Test Inbound: Send an SMS from your mobile to your Vonage number. Check the server.js console for logs.
  3. Test Outbound & Status: Run node send-sms.js. Receive the SMS. Check the server.js console for both the initial send attempt logs (from send-sms.js) and the subsequent status update logs (from the webhook hitting server.js).

8. Security Considerations

  • Webhook Signature Verification (Highly Recommended for Production): Vonage signs webhook requests using JWT (JSON Web Tokens) based on your application's private key or a shared secret. Verifying this signature ensures authenticity and integrity.
  • The @vonage/server-sdk can help. You typically use middleware to check the Authorization header (for JWT/private key method) or a custom header (for shared secret method).
  • Conceptual Example (JWT/Private Key Method Middleware):
    const { Vonage } = require('@vonage/server-sdk');

    app.use('/webhooks', (req, res, next) => {
      try {






        const signatureHeader = req.headers['authorization'];
        const isSignatureValid = (signatureHeader ) ? true : false;

        if (isSignatureValid) {
          console.log('Webhook signature verified successfully (Conceptual).');
          next();
        } else {
          console.warn('Invalid webhook signature (Conceptual).');
          res.status(401).send('Invalid signature');
        }
      } catch (error) {
        console.error('Error verifying webhook signature:', error);
        res.status(500).send('Signature verification error');
      }
    });
  • Consult the official Vonage documentation on securing webhooks and the @vonage/server-sdk documentation for the exact implementation details and required setup (like providing the public key or secret).
  • Environment Variables: Never commit .env files or private keys. Use .gitignore. Use secure environment variable injection in production.
  • Rate Limiting: Use middleware like express-rate-limit to protect webhook endpoints.
  • Input Validation: Sanitize and validate message content (req.body.text) if used elsewhere.
  • HTTPS: Always use HTTPS for production webhook URLs.

9. Error Handling and Logging

  • Webhook Handler Errors: Wrap internal processing logic in try...catch. Log errors but always send 200 OK to Vonage to prevent retries. Handle errors asynchronously if needed.
  • Robust Logging: Use libraries like winston or pino for structured, leveled logging in production.
  • Outbound Send Errors: Enhance error handling in send-sms.js (e.g., retries).
  • Monitoring: Use error tracking services (Sentry, Bugsnag).

10. Database Integration (Optional Next Step)

  1. Choose Database & ORM/Driver: (e.g., PostgreSQL + Prisma, MongoDB + Mongoose).
  2. Install Dependencies: npm install prisma @prisma/client pg (example).
  3. Define Schema: Model SmsMessage (direction, IDs, numbers, body, status, timestamps).
  4. Run Migrations: Apply schema (npx prisma migrate dev).
  5. Integrate: Use ORM client in webhook handlers to save/update messages and in send-sms.js to log outbound messages.
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();

try {
    await prisma.smsMessage.create({
        data: {
            direction: 'inbound',
            vonageMsgId: messageId,
            fromNumber: msisdn,
            toNumber: to,
            body: text,
            status: 'received',

        }
    });
    console.log('Inbound message saved to database.');
} catch (dbError) {
    console.error('Database error saving inbound message:', dbError);

}

11. Troubleshooting and Caveats

  • ngrok Issues: Check firewalls, ngrok status/interface, ensure server.js runs on the correct port.
  • Webhooks Not Received: Verify Vonage App URLs match current ngrok HTTPS URL exactly. Confirm number linkage. Check Vonage Dashboard API Logs.
  • Authentication Errors (Sending): Double-check VONAGE\_APPLICATION\_ID and VONAGE\_PRIVATE\_KEY\_PATH in .env. Ensure the key file exists and is readable. Check API error response data.
  • Incorrect API Selected: Ensure consistency between Vonage account settings (Messages API default) and code usage.
  • Missing 200 OK Response: Ensure webhook handlers always return 200 OK promptly, even if internal processing fails (log the error).
  • Number Formatting: Always use the full E.164 format for phone numbers, including the leading + and country code (e.g., +14155552671), both in your .env file and when sending messages. While the SDK might sometimes correctly interpret numbers without the +, relying on this can lead to errors. Using the full E.164 format consistently is the recommended best practice.
  • Carrier Filtering/Blocking: Delivery issues despite "submitted" status might be carrier-related. Contact Vonage support. Ensure compliance (e.g., 10DLC in US).

12. Deployment and CI/CD

  1. Choose Platform: Heroku, Vercel, AWS, Google Cloud, etc.
  2. Environment Variables: Configure VONAGE\_APPLICATION\_ID, VONAGE\_PRIVATE\_KEY\_PATH (or key content), VONAGE\_NUMBER, etc., securely in the platform's settings. Do not deploy .env.
  3. Private Key Handling in Env Vars: Since private keys are multi-line, directly pasting them into standard environment variables can be problematic. Common solutions:
  • Base64 Encode: Encode the key file content into a single Base64 string. Store this string in an environment variable (e.g., VONAGE\_PRIVATE\_KEY\_BASE64). In your code, decode it before passing it to the Vonage SDK.
     cat private.key | base64 > private.key.b64
     const privateKeyContent = Buffer.from(process.env.VONAGE_PRIVATE_KEY_BASE64, 'base64').toString('utf-8');
     const vonage = new Vonage({
       applicationId: vonageAppId,
       privateKey: privateKeyContent,
     });
  • Platform Secrets Management: Use built-in secrets management tools (e.g., AWS Secrets Manager, Google Secret Manager, Heroku Config Vars, Vercel Environment Variables supporting multi-line).
  • File Path (Less Ideal for Serverless): If deploying to a VM/container, you might securely place the key file on the filesystem and use VONAGE\_PRIVATE\_KEY\_PATH pointing to its location.
  1. Update Webhook URLs: Change Vonage Application URLs to your permanent production HTTPS URL (e.g., https://your-app.your-domain.com/webhooks/inbound).
  2. PORT Variable: Ensure server.js uses process.env.PORT.
  3. package.json Scripts: Add a start script:
   {
     ""scripts"": {
       ""start"": ""node server.js"",
       ""send"": ""node send-sms.js""
     }
   }
  1. Deployment Commands: Use platform CLI/UI (e.g., git push heroku main, Vercel deploy hooks). Set environment variables securely.
  2. CI/CD: Implement automated testing and deployment pipelines (GitHub Actions, GitLab CI, etc.).

13. Conclusion

You have built a Node.js application for two-way SMS using Express and the Vonage Messages API. You've covered setup, sending, receiving via webhooks, local testing, and key considerations for security, error handling, and deployment.

Next Steps:

  • Implement robust webhook signature verification.
  • Add database persistence.
  • Build specific application logic.
  • Set up proper logging, monitoring, and error tracking.
  • Deploy to production.

This guide provides a solid foundation for integrating SMS communication into your Node.js projects.

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 β†’