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

Frequently Asked Questions

Use the Vonage Messages API with the @vonage/server-sdk and an Express.js server. Create a /send-sms endpoint that takes the recipient's number and message text, then uses the SDK to send the SMS via the Vonage API. This setup allows your Node.js application to programmatically send SMS messages.
The Vonage Messages API is a service that allows you to send and receive messages across multiple channels, including SMS, MMS, WhatsApp, and more. It provides a flexible and powerful way to integrate messaging into your applications using various programming languages, including Node.js.
Dotenv is used for managing environment variables securely. It loads variables from a .env file into process.env, preventing sensitive information like API keys and secrets from being hardcoded in your application and accidentally exposed in version control.
Ngrok is useful when developing locally and you need to expose your local server to the public internet. This is particularly important if you want to receive SMS messages or status updates via webhooks, as Vonage needs a publicly accessible URL to send these requests to.
Log into your Vonage Dashboard, navigate to Applications, and click "Create a new application." Give your application a name, generate public and private keys (store the private key securely), enable the Messages capability, set the Inbound and Status URLs for webhooks, and click "Generate new application." This creates the application and provides you with an Application ID.
The Vonage Application ID is a unique identifier for your Vonage application. Along with your private key, it is used to authenticate your application with the Vonage Messages API and other Vonage services. This ID is required when initializing the Vonage SDK.
After creating a Vonage Application, go to its configuration page in the dashboard. In the "Link virtual numbers" section, find the number you want to send SMS messages from and click "Link." This associates your Vonage number with your application so you can send messages from it using the Vonage API.
The Vonage Private Key Path specifies the location of your downloaded private.key file on your server. It's used by the @vonage/server-sdk for authentication with the Vonage API and should be stored securely. Never expose this file in version control systems like Git.
Use a try...catch block around the vonage.messages.send() call to catch potential errors. Provide specific error responses to the user, log detailed error information (including the Vonage API's error response if available) for debugging, and consider implementing retry mechanisms with exponential backoff for transient errors. Use structured JSON for errors where possible.
Implement robust input validation using libraries like Joi or express-validator, sanitize user input to prevent injection attacks, use rate limiting to avoid abuse, and consider API key authentication or JWT for internal or protected endpoints. Securely manage Vonage credentials using environment variables and avoid hardcoding them.
Use a dedicated logging library like Winston or Pino. Configure different log levels, format logs in JSON for easier parsing and analysis, and output logs to files or external logging services. Also, log unhandled exceptions and promise rejections for complete error tracking.
Standard SMS messages are limited to 160 characters when using GSM-7 encoding. Messages with non-GSM characters (emojis, some accented characters) use UCS-2 encoding, and each segment holds around 70 characters. Vonage charges per segment, so long messages are split into multiple segments.
Set a valid Status URL in your Vonage Application configuration. This URL should point to an endpoint in your application (e.g., /webhooks/status) that can receive delivery receipts (DLRs) from Vonage. Implement logic to process these status updates, typically updating a database or logging the status. Secure the webhook endpoint.
Trial accounts often have restrictions on sending to unverified numbers. Add the recipient's phone number to your allowed list in the Vonage Dashboard under Settings > Test Numbers. Also verify your 'from' number is linked and the default SMS API is set to Messages API, not SMS API.