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

Frequently Asked Questions

Use the Vonage Messages API with the Vonage Node.js SDK. Set up an Express route to handle requests, then use the SDK to send SMS messages via the API. This allows you to integrate SMS functionality directly into your Node.js applications.
The Vonage Messages API is a service that allows you to send and receive messages through different channels like SMS, WhatsApp, and more. This tutorial focuses on using it to send SMS notifications from your Node.js app.
Dotenv loads environment variables from a `.env` file. This keeps sensitive credentials like your Vonage API key and secret out of your source code, which improves security and prevents accidental exposure.
While the provided Node.js code uses the Messages API directly, it's beneficial to set the default SMS API in your Vonage account settings to the Messages API for consistency, especially if you plan to implement features involving webhook functionality, as webhook formats differ between the legacy SMS API and the Messages API.
No, trial accounts have restrictions. You can only send SMS messages to numbers you've verified in your Vonage Dashboard under 'Getting Started' > 'Add test numbers'. For unrestricted sending, you'll need to upgrade to a paid account.
Use `npm install express @vonage/server-sdk dotenv`. This command installs Express for the web framework, the Vonage Server SDK to interface with the Vonage API, and dotenv to securely manage environment variables.
The `.gitignore` file specifies files and directories that Git should ignore when tracking changes. It’s crucial to add `node_modules` and `.env` to your `.gitignore` to prevent accidentally committing dependencies and your sensitive Vonage API credentials.
First, install it with npm, then initialize it with your API key and secret, which you obtain from the Vonage API Dashboard. These credentials are loaded into your project from the '.env' file via dotenv.
E.164 is an international standard for phone number formatting. It includes a '+' sign followed by the country code and the national subscriber number without any spaces or special characters. For example, a US number would be +14155550100.
Create a POST route handler in your Express app (e.g., `app.post('/send-sms', ...)`). This route will receive the recipient's phone number and the message text from a request. Use this data to send the SMS using the Vonage SDK.
A 400 Bad Request error indicates an issue with the client's request, typically due to missing required fields or invalid formatting. Ensure the request body contains 'to' and 'text' fields, and that the 'to' number follows E.164 format.
After starting your server, use a command like `curl -X POST -H "Content-Type: application/json" -d '{"to": "+1XXXXXXXXXX", "text": "Test message"}' http://localhost:3000/send-sms`. Replace +1XXXXXXXXXX with a valid E.164 formatted phone number and 'Test message' with your desired SMS content.
This usually occurs with trial Vonage accounts. Ensure the recipient's phone number is added to your allowed list of test numbers in the Vonage Dashboard. If you need to send to any number, upgrade to a paid account.
Use a try-catch block around your API calls to handle errors. Log these errors for debugging and return appropriate error responses to the client. For production, consider using a logging framework like Winston or Pino.