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

Frequently Asked Questions

Use the Vonage Messages API with the @vonage/server-sdk library. Set up an Express.js server with a POST endpoint, initialize the Vonage SDK with your credentials, and call vonage.messages.send() with recipient number, sender number, and message text. Ensure E.164 formatting for phone numbers and proper private key handling.
The Vonage Messages API enables sending messages programmatically over various channels, including SMS. It provides multi-channel capabilities and robust features for sending notifications, alerts, or implementing two-factor authentication.
Dotenv keeps sensitive credentials like API keys and private keys out of your codebase. It loads environment variables from a .env file, enhancing security and simplifying configuration management.
Link a Vonage virtual number to your Vonage application before sending SMS messages. This number will be the "from" address for your outgoing SMS messages. Make sure this number is SMS-capable in your target region.
While this tutorial uses Application ID and Private Key, the API Key and Secret are not strictly required for SMS via the Messages API. However, they are necessary for other Vonage APIs and some SDK features.
Store the entire content of your private.key file as the VONAGE_PRIVATE_KEY_CONTENT environment variable. Never commit the .env file to version control and use your deployment platform's secure environment variable storage for production.
E.164 is an international standard for phone numbers, which includes a '+' followed by the country code and the subscriber number with no spaces or dashes. Example: +14155550100.
Install Express and create an app. Use middleware for JSON and URL-encoded body parsing (express.json(), express.urlencoded()). Define a POST route (e.g., /api/send-sms) that receives to and message data and calls the sendSms function.
Rate limiting prevents abuse and protects your application and the Vonage API from excessive requests. Use middleware like express-rate-limit to restrict requests per IP or other criteria.
A database isn't strictly required for this basic SMS sending guide. However, it is essential for logging messages, tracking delivery status with webhooks, or managing user information.
While not included in this basic guide, you'll need to set up status webhooks in your Vonage Application and implement a corresponding endpoint in your Express app to receive and store delivery receipts in a database.
401 Unauthorized indicates incorrect credentials, "Invalid 'to' number" often signals incorrect formatting or lack of whitelisting (for trial accounts), and 429 Too Many Requests indicates sending rate limits.
Ensure VONAGE_PRIVATE_KEY_CONTENT contains the *exact* key content (including "-----BEGIN..." and "-----END..." lines) and correct newline handling. Check for any extra characters or encoding issues in the .env file or environment variables.
Connect your Git repository to Heroku. Set environment variables, including the multi-line private key content (VONAGE_PRIVATE_KEY_CONTENT), using the Heroku CLI or dashboard Config Vars section. Push your code to deploy.
Use a command like curl -X POST -H "Content-Type: application/json" -d '{"to": "+14155550101", "message": "Test message"}' http://localhost:3000/api/send-sms, replacing +14155550101 with a valid number.