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

Frequently Asked Questions

You can send SMS messages programmatically using Node.js, Express, and the Vonage API. Set up an Express server, integrate the Vonage Node.js SDK, create a POST endpoint to handle SMS sending logic, and configure your Vonage API credentials.
The Vonage Node.js SDK (`@vonage/server-sdk`) simplifies interaction with the Vonage APIs from your Node.js application. It handles authentication and provides methods for sending SMS messages, making voice calls, and more.
You need a Vonage virtual number to send SMS messages *from*. This number acts as the sender ID and is required by the Vonage API. Purchase one through the Vonage Dashboard and ensure it's SMS-capable.
While this tutorial uses the simpler `vonage.sms.send` method, the Messages API (`vonage.messages.send`) is recommended for more advanced use cases. This includes sending MMS, WhatsApp messages, or when you need features like delivery receipts.
Trial accounts can *only* send SMS to verified test numbers added in your Vonage Dashboard settings. To send to any number, you must upgrade your account by providing billing information.
Create a project directory, initialize npm (`npm init -y`), install `express`, `@vonage/server-sdk`, and `dotenv`, configure `package.json` for ES modules, set up a `.env` file with your API credentials, and create a `.gitignore` file.
The `.env` file stores your sensitive Vonage API credentials (API Key, API Secret, Virtual Number) and server configuration. It's loaded by the `dotenv` package. Never commit this file to version control.
The Vonage API returns a status code ('0' for success). Implement error handling to check for non-zero status codes and provide appropriate feedback to the user. The server logs should record detailed error messages.
The provided example has basic validation. Use a dedicated library like `libphonenumber-js` (the Node.js port of `google-libphonenumber`) for robust E.164 validation to prevent issues and improve reliability.
Never commit your `.env` file to version control. In production, use secure environment variable management provided by your deployment platform (e.g., platform secrets, vault) to prevent unauthorized access.
Use a middleware package like `express-rate-limit` to restrict how many requests a user can make within a time period. This protects your application and Vonage account from abuse and unexpected costs.
This is common with Vonage trial accounts. Ensure the recipient number is added to the Test Numbers list in your Vonage Dashboard under Account > Settings > Test Numbers. Upgrading your account removes this limitation.
Check that the `VONAGE_FROM_NUMBER` in your `.env` file is a valid, SMS-capable Vonage number assigned to your account and is in the correct format (typically E.164 without the leading '+', like '15551234567').
Extend your application by receiving SMS messages (webhooks), implementing delivery receipts, adding more robust error handling and retry logic, using asynchronous sending with queues, or integrating a database for message logging.