Send SMS with Node.js, Express, and Vonage: A Developer Guide - code-examples -

Frequently Asked Questions

Use the Vonage Messages API and Node.js SDK. Set up an Express server with an endpoint, initialize the Vonage SDK with credentials, and use the `vonage.messages.send()` method to send SMS from your Vonage number.
The Vonage Messages API is a versatile communication platform enabling message delivery across various channels like SMS, WhatsApp, and Viber. It allows developers to send and receive messages programmatically.
Dotenv helps manage environment variables by storing sensitive information like Vonage API keys and secrets in a `.env` file, keeping them separate from your codebase and preventing accidental exposure in version control.
Use the Messages API when you need multi-channel messaging (SMS, WhatsApp, Viber). If you only need basic SMS, the SMS API (`vonage.sms.send()`) with API key/secret authentication is sufficient.
No, trial accounts require adding recipient numbers to a whitelist in the Vonage dashboard under "Sandbox & Test Numbers" -> "Test Numbers" before sending SMS to those numbers.
In the Vonage Dashboard, create a new application, generate keys (save `private.key`), enable "Messages" capability, set dummy webhook URLs, generate the application, and link your Vonage virtual number. Critically, ensure the default SMS API setting is "Messages API".
The Vonage Application ID is used for authentication and configuration with the Messages API. Along with the `private.key`, it grants access to API functionalities when initializing the Vonage Node.js SDK.
Implement robust error handling with `try...catch` blocks, logging detailed error messages, especially `err.response.data` from the Vonage SDK. For production, use structured logging libraries (Pino, Winston) and consider retry mechanisms with exponential backoff.
Use environment variables, strong input validation libraries (`joi`, `express-validator`), rate limiting middleware (`express-rate-limit`), and require authentication/authorization (API keys, JWTs, OAuth) for your API endpoint. Always use HTTPS in production.
The E.164 format is an international standard for phone numbers. It includes a plus sign (+) followed by the country code and national number, for example, +14155550100. Use this format for both `VONAGE_NUMBER` and recipient numbers.
This error usually occurs with trial accounts when sending to numbers not registered in the Vonage Dashboard's Sandbox Test Numbers. Add the recipient number to the whitelist to resolve this.
Double-check Vonage credentials (Application ID, private key path, number), ensure "Messages API" is set as the default in the dashboard, check private key file permissions, use E.164 format, and carefully examine error logs for details from the Vonage SDK.
Choose a platform (Heroku, AWS, GCP, etc.), configure environment variables securely, handle the `private.key` securely (secrets manager or build process), use a process manager (e.g., pm2), and enforce HTTPS for all traffic.