Frequently Asked Questions
Use the Vonage API and Express.js framework within your Node.js application. This involves setting up a POST route in Express to handle requests, and then utilizing the Vonage Node.js SDK to send messages via their SMS API. The server receives the recipient's number and message text, then uses Vonage to deliver the SMS.
The Vonage API, a Communication Platform as a Service (CPaaS), enables sending SMS messages programmatically. In Node.js, you interact with it using the Vonage Node.js Server SDK, which simplifies sending SMS messages, managing two-factor authentication, and other communication tasks. The article provides a tutorial on its usage for sending SMS.
Dotenv is a crucial module for storing sensitive information such as API keys and secrets as environment variables within a .env
file. It's instrumental in keeping these credentials secure and outside your main codebase, protecting them from accidental exposure through version control systems like Git.
While you can use the Vonage SMS API for two-factor authentication (2FA), Vonage's Verify API is often better suited for this specific purpose. It provides more advanced 2FA features and simplifies the process, streamlining security implementations.
Store your Vonage API_KEY
, API_SECRET
, virtual FROM_NUMBER
, and PORT
within a .env
file. Then, use dotenv.config()
in your index.js
file to load these variables into your Node.js environment so your application can use them to authenticate with Vonage and send messages.
You'll need express
for creating the API endpoint, @vonage/server-sdk
to interact with the Vonage APIs, and dotenv
to manage your Vonage API credentials securely as environment variables.
Define a POST
route handler in your Express app, typically at /send-sms
. This endpoint receives the recipient's number (to
) and the message body (text
) as parameters in the request. Inside the handler, use the Vonage SDK to send the message using the provided parameters.
A '0' status from the Vonage SMS API usually indicates successful message submission to the network, not final delivery to the recipient. To get confirmed delivery status, you need to implement delivery receipts (DLRs) using webhooks. These provide real-time updates on the message status as it progresses.
No, trial accounts have restrictions. You can only send SMS to numbers you've specifically verified in the Vonage dashboard's Sandbox / Test Numbers page. To lift this restriction, you must upgrade to a paid Vonage account.
The VONAGE_FROM_NUMBER
is your designated Vonage Virtual Number, which acts as the sender ID for your outgoing SMS messages. This number is what recipients will see as the source of the SMS.
The example code uses basic try...catch
blocks. For production, use libraries like Winston or Pino for structured logs and send them to centralized logging services. Implement more detailed error handling by checking Vonage error codes (like 'Non White-listed Destination') returned in the API response and take appropriate actions (e.g., retries or alerts).
Once your Express server is running, use tools like curl
or Postman to send POST
requests to the /send-sms
endpoint. The request body should be JSON formatted with the recipient's number (to
) and message (text
). Ensure your trial account numbers are whitelisted in the Vonage dashboard.
Never expose your API keys directly in your code. Always use environment variables (.env
file) and make sure this file is included in your .gitignore
. In production, manage secrets securely using your platform's specific mechanisms. Also implement input validation to prevent potential injection vulnerabilities and use rate limiting to protect against abuse.
How to Send SMS Messages with Vonage API in Node.js Express
Learn how to send SMS messages with Vonage SMS API using Node.js and Express. This complete guide walks you through building a production-ready SMS application with @vonage/server-sdk 3.24.1, covering secure API credential management, error handling, delivery receipt tracking, and testing best practices.
You'll create a functional Express SMS server with a REST API endpoint that accepts a recipient phone number and message text, then delivers SMS messages reliably using the Vonage API. This Node.js SMS integration works for notifications, alerts, appointment reminders, or two-factor authentication (though Vonage's Verify API is purpose-built for 2FA workflows).
This tutorial uses Vonage's official Node.js SDK (@vonage/server-sdk) with comprehensive error handling for delivery receipts (DLRs) and message status tracking.
Current Versions (as of 2024–2025):
Project Overview: Building a Vonage SMS API Integration
What You'll Build:
A Node.js Express server with a single API endpoint (
POST /send-sms
). This endpoint receives a destination phone number and text message body, then uses the Vonage Node.js SDK to send SMS messages programmatically.Problem Solved:
Integrate SMS sending capabilities into Node.js applications quickly and reliably using Vonage's enterprise-grade communication platform.
Technologies Used:
.env
intoprocess.env
, keeping credentials secureSystem Architecture:
POST
request to the/send-sms
endpoint.Prerequisites: What You Need to Send SMS with Vonage
Prerequisites:
Numbers
>Buy numbers
in the Vonage Dashboard. Ensure the number supports SMS in your target regionStep 1: Set Up Your Vonage Node.js SMS Project
Initialize your Node.js project and install the necessary dependencies.
Create a Project Directory:
Initialize the Node.js Project: Create a
package.json
file. The-y
flag accepts default settings.Install Dependencies: Install
express
,@vonage/server-sdk
, anddotenv
.Create Core Files:
Configure Environment Variables (
.env
): Open.env
and add your Vonage API credentials and rented number.VONAGE_API_KEY
: Your API key from the Vonage DashboardVONAGE_API_SECRET
: Your API secret from the Vonage DashboardVONAGE_FROM_NUMBER
: Your virtual phone number in E.164 format without spaces (e.g.,14155550100
)PORT
: The port your Express server listens on (e.g.,3000
)Important: Replace placeholder values with your actual credentials.
Configure Git Ignore (
.gitignore
): Prevent committing sensitive environment variables andnode_modules
.Your project structure:
Step 2: Implement Vonage SMS API with Node.js and Express
Write the code for your Express server in
index.js
.Require Dependencies and Load Environment Variables:
Why
dotenv.config()
first? It must run before accessingprocess.env
variables from.env
. The code checks that critical variables load successfully.Initialize Express App and Vonage Client: Create Express and Vonage SDK instances using environment variables.
Add Middleware: Configure Express to parse JSON request bodies.
Create the SMS Sending Endpoint (
/send-sms
): Define aPOST
route to handle SMS sending requests.Key Points:
async
function to useawait
for the asynchronousvonage.sms.send
call.to
andtext
are present in the request body.from
number from environment variables.vonage.sms.send({ to, from, text })
makes the API call.try...catch
block handles potential network errors or issues within the SDK call.resp
object from Vonage. Astatus
of'0'
indicates successful submission to the network. Non-zero statuses indicate errors (e.g., invalid number, insufficient funds). Return the Vonagemessage-id
on success.Start the Server: Make the Express application listen for incoming requests on the configured port.
Your
index.js
file is now complete.Step 3: Test Your Vonage SMS Integration
Run the server and test the SMS sending functionality.
Run the Server: Open your terminal in the project directory and run:
You should see output indicating the server is running:
Test with
curl
or Postman: Usecurl
(command-line) or Postman (GUI) to send aPOST
request to your endpoint.Using
curl
: ReplaceYOUR_RECIPIENT_NUMBER
with the phone number you want to send the SMS to (use a verified number for trial accounts). Format the number including the country code (e.g.,14155550123
).Using Postman:
Create a new request.
Set the method to
POST
.Enter the URL:
http://localhost:3000/send-sms
.Go to the
Body
tab, selectraw
, and chooseJSON
from the dropdown.Enter the JSON payload:
Click
Send
.Check the Response:
Successful Submission: You should receive a JSON response with a
200 OK
status:Check the recipient phone – the SMS should arrive shortly. You should also see confirmation logs in the terminal where
node index.js
is running.Error Response: If something goes wrong (e.g., invalid credentials, incorrect 'to' number format, trial account restrictions), you'll get an error response with a non-200 status code:
Check the terminal logs for more detailed error information.
Step 4: Handle Vonage SMS API Errors and Delivery Receipts
Your current implementation includes basic
try...catch
blocks and logs errors to the console. For production systems, implement:Winston
orPino
for structured logging (e.g., JSON format), which makes logs easier to parse and analyze, especially when sending them to log aggregation services (like Datadog, Splunk, ELK stack).console.log
.error-text
or status fields) to provide more informative feedback or trigger specific actions (e.g., retry logic for temporary network issues, alerts for authentication failures). Common error codes include:Step 5: Secure Your Vonage API Credentials
.env
file or hardcode API keys/secrets directly in your source code. Use environment variables and ensure.env
is in your.gitignore
. In production environments, use your hosting provider's mechanism for managing secrets (e.g., AWS Secrets Manager, Heroku Config Vars, Kubernetes Secrets).to
phone number (e.g., check format using a library likelibphonenumber-js
version 1.11+ with metadata synced to September 2025, ~145 KB max metadata) and sanitize thetext
input to prevent potential injection attacks if the text is ever displayed elsewhere.express-rate-limit
). This prevents users from sending excessive numbers of messages rapidly.Step 6: Troubleshoot Common Vonage SMS API Issues
VONAGE_API_KEY
andVONAGE_API_SECRET
in your.env
file are correct and have no extra spaces.FROM
NumberVONAGE_FROM_NUMBER
is a valid number you rented from Vonage in the correct format (e.g.,14155550100
).@vonage/server-sdk
is stable, always refer to the official Vonage documentation for the latest methods and response structures.vonage.sms.send()
. For multi-channel messaging (WhatsApp, Facebook Messenger, MMS) or advanced features, explore the Vonage Messages API withvonage.messages.send()
.Step 7: Deploy Your Vonage Node.js SMS Application
Deploy this application on a server or platform (like Heroku, Render, AWS EC2, Google Cloud Run, etc.). Key steps include:
VONAGE_API_KEY
,VONAGE_API_SECRET
, andVONAGE_FROM_NUMBER
as environment variables on your chosen hosting platform. Do not upload the.env
file.npm install
runs in the deployment environment to install dependencies.node index.js
.PORT
environment variable, which your code already handles).A
Procfile
for platforms like Heroku:Complete Vonage SMS Code Example
The complete code for this guide includes
index.js
,package.json
,.gitignore
, and a.env.example
file.Frequently Asked Questions About Vonage SMS with Node.js
How do I send SMS with Vonage in Node.js?
Install
@vonage/server-sdk
(version 3.24.1 for 2025) and create a Vonage client with your API key and secret. Usevonage.sms.send({ to, from, text })
to send messages. You need a Vonage account, API credentials, and a rented virtual number. The complete setup requires Express for the REST endpoint, dotenv for credential management, and proper error handling for production use.What does Vonage status code 0 mean?
Status code '0' indicates successful submission to the Vonage network, not delivery to the recipient's handset. To confirm actual delivery, implement webhook endpoints to receive Delivery Receipts (DLRs) from Vonage. DLR statuses include: DELIVRD (delivered), UNDELIV (undelivered), EXPIRED, FAILED, REJECTD, DELETED, and UNKNOWN.
What is Vonage Error Code 15?
Error Code 15 (Rejection Code 10 for SMPP) means "Illegal Sender" – this occurs when sending SMS to the US or Canada without a US pre-approved long number, toll-free, 10DLC, or short code associated with your Vonage account. To resolve this, obtain the appropriate number type for US/Canada SMS delivery through your Vonage Dashboard.
What is Vonage Error Code 99?
Error Code 99 indicates a "General Error" – meaning the request was processed but failed due to various factors. Check the Delivery Receipt (DLR) details for specifics about why the message failed. This error requires examining the
error-text
field in the response and consulting the Vonage SMS API error documentation for detailed troubleshooting.What version of @vonage/server-sdk should I use in 2025?
Use
@vonage/server-sdk
version 3.24.1 (latest as of early 2025) with Node.js v22 LTS and Express 5.1.0. Install withnpm install @vonage/server-sdk@3.24.1
. This version provides stable SMS API functionality with improved error handling. Always check npm for the latest version.How do I handle the "Non-Whitelisted Destination" error?
This is the most common error for Vonage trial accounts. Trial accounts can only send SMS to verified numbers. Add and verify recipient numbers in your Vonage Sandbox / Test Numbers page. To send to any number, upgrade your account by adding payment details in the Vonage Dashboard.
What are Delivery Receipts (DLRs) in Vonage SMS?
Delivery Receipts (DLRs) are webhook notifications from Vonage confirming whether your SMS was delivered to the recipient's handset. DLRs provide statuses like DELIVRD (delivered), UNDELIV (undelivered), EXPIRED, FAILED, REJECTD, DELETED, or UNKNOWN. Implement a webhook endpoint to receive DLRs for reliable message tracking. See the DLR Status documentation.
Should I use Vonage SMS API or Messages API?
For simple SMS sending with Node.js Express, use the SMS API via
vonage.sms.send()
with API Key/Secret authentication (covered in this guide). For multi-channel messaging (WhatsApp, Facebook Messenger, MMS) or advanced features, use the Vonage Messages API withvonage.messages.send()
, which requires Application ID and Private Key authentication.How do I validate phone numbers before sending SMS with Vonage?
Use
libphonenumber-js
version 1.11+ (with metadata synced to September 2025, ~145 KB max metadata) to validate phone number format before calling the Vonage API. Install withnpm install libphonenumber-js
and parse numbers to E.164 format. This prevents API errors from invalid phone numbers and improves delivery rates.Can I send SMS from a Vonage trial account?
Yes, but with restrictions. Trial accounts receive free testing credit but can only send SMS to verified numbers added in the Sandbox / Test Numbers page. Upgrade your account (add payment details) to send SMS to any number. Trial accounts are perfect for development and testing before production deployment.
Next Steps: Enhancing Your Vonage SMS Integration
You've successfully built a Node.js application capable of sending SMS messages using Express and the Vonage API.
From here, you could: