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

Frequently Asked Questions

Use the Vonage Messages API and the Vonage Node.js Server SDK. Create an Express.js endpoint that takes the recipient's number and message, then uses the SDK to send the SMS via Vonage.
The Vonage Messages API is a unified platform for sending messages across multiple channels, such as SMS, MMS, WhatsApp, and more. It simplifies the integration of messaging into applications.
Dotenv loads environment variables from a .env file into process.env. This best practice helps keep sensitive credentials like API keys out of your source code, enhancing security.
Ngrok is helpful when developing locally and needing to expose your server to the internet. It is particularly useful for handling incoming messages (webhooks) during development, not for sending SMS messages.
Sign up for a Vonage API account. Create a Vonage application, generate keys, enable the Messages capability, and link a Vonage virtual number to your application. Ensure default SMS API is set to 'Messages API'.
Import the Vonage library (`@vonage/server-sdk`). Create a new Vonage instance, passing in your API key, API secret, Application ID, and private key. Ensure you read your private key from the `private.key` file.
Implement a try-catch block around the vonage.messages.send() call. Inspect the error?.response?.data object for specific error codes and messages from Vonage. Return appropriate HTTP status codes and informative messages based on the error.
This error usually occurs with trial Vonage accounts. Add the recipient's phone number to your Test Numbers whitelist in the Vonage dashboard. Upgrading to a paid account removes this limitation.
A Vonage Application acts as a container for your project's configuration within Vonage. It holds settings like linked numbers, capabilities (e.g., Messages, Voice), and webhooks for incoming messages or events.
Use environment variables (process.env) to store API keys and secrets. Do not commit .env files to version control. Utilize your deployment platform's secure mechanisms for handling sensitive data in production.
E.164 is an international telephone number format that includes a '+' and the country code, followed by the subscriber number, like +14155550101. Use this format for recipient numbers when sending SMS with Vonage.
Use Express.js to create a server and define routes. Use the Vonage Node.js SDK to send SMS messages via API requests to the Vonage platform.
Double-check the VONAGE_PRIVATE_KEY_PATH in your .env file. It should point to the correct path within your project where the private.key file was placed (usually project root). Also, ensure the file content hasn't been modified.
Incorrect API key, secret, application ID, or an invalid private key. Check your .env file against your Vonage dashboard values. Also, ensure your account has 'Messages API' selected as default for SMS.
Verify correct API credentials in .env, ensure the recipient number is in E.164 format and whitelisted (if applicable), check your Vonage application settings, and consult Vonage logs for more details.