Send SMS with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use the Vonage SMS API with the Node.js SDK and Express. Create a POST endpoint that accepts the recipient's number and message, then use the SDK to send the SMS via Vonage's servers. The provided tutorial includes a step-by-step guide and code examples to get you started quickly.
The Vonage SMS API enables sending text messages programmatically from your applications. This guide specifically shows how to use the API with API Key and Secret authentication for sending outbound SMS messages from a Node.js backend.
Dotenv helps manage sensitive credentials like API keys and secrets by storing them in a '.env' file. This prevents accidentally committing these credentials to version control, enhancing security.
Use `vonage.sms.send()` when sending SMS messages with API Key and Secret authentication in the Vonage Node.js SDK. This method is suitable for straightforward SMS sending scenarios, as described in this guide.
The example provided in the tutorial sends to one recipient at a time. For sending to multiple recipients, you would need to loop through an array of numbers or adapt the API call within the SDK if it provides batch sending functionalities.
First, create a project directory and initialize a Node.js project with npm. Install `express`, `@vonage/server-sdk`, and `dotenv`. Create `index.js`, `.env`, and `.gitignore` files. Then, configure Vonage credentials and virtual number in the `.env` file.
Express.js provides the web server framework for creating the API endpoint that receives requests to send SMS messages. It handles routing the `/send-sms` request to the logic that interacts with the Vonage SMS API.
The provided code example demonstrates error handling by checking the 'status' field in the Vonage API response. A status of '0' signifies success, while other codes indicate errors, which are then handled appropriately.
During the trial period of a Vonage account, you must whitelist the recipient phone numbers for testing. Add the recipient's number to your approved test numbers in the Vonage dashboard to prevent the 'Non-Whitelisted Destination' error.
Log in to your Vonage API Dashboard, where the API Key and Secret are usually displayed on the main page or under 'API settings'. Copy these values for use in your application.
A Vonage virtual number serves as the sender ID for your SMS messages. You need a Vonage number to send SMS from, which you can obtain from the Vonage dashboard. Make sure it's in the correct format, such as +14155552671.
After starting your Node.js server, test the `/send-sms` endpoint with tools like `curl` or Postman. Send a POST request with the recipient's number and message in JSON format. Verify the response and check if the recipient received the SMS.
This error occurs when you're using a trial Vonage account and the recipient number isn't whitelisted. Ensure the recipient's number is added to your approved test numbers in the Vonage dashboard.
Double-check your Vonage API key and secret in your `.env` file for typos, making sure they match what's in your Vonage Dashboard. Ensure the `.env` file is loaded correctly using `require('dotenv').config()` early in your Node.js code.