Frequently Asked Questions
Set up a new RedwoodJS project, install the Vonage Server SDK, configure environment variables with your Vonage credentials, create a Vonage client library to handle SDK initialization and SMS sending logic, and create a RedwoodJS API function to expose the SMS sending functionality over HTTP. Test the setup by sending a POST request to the function's endpoint with the recipient's number and message. You'll need a Vonage API account and virtual number for sending SMS messages from your application via the Vonage Messages API.
The Vonage Messages API allows developers to send messages across various channels, including SMS, from their applications. It offers reliability, scalability, and a user-friendly SDK. In this RedwoodJS guide, it's used to send SMS notifications, alerts, or general messages programmatically. You'll need Vonage API credentials and an SMS-enabled Vonage Virtual Number.
Vonage is chosen for its reliability, scalability, and easy-to-use Node.js SDK. The Vonage Messages API provides a powerful and flexible way to integrate SMS messaging into Node.js and RedwoodJS applications, simplifying the process of sending alerts, notifications, and other text messages. The provided guide uses the official Vonage Node.js SDK `@vonage/server-sdk`.
Consider a job queue for high-volume SMS sending or when your API endpoint needs to respond very quickly without waiting for Vonage's confirmation. For low-volume or individual messages triggered by user actions, a direct API call within the serverless function is usually sufficient, whereas a job queue becomes more beneficial for bulk messages or near real-time API response requirements. The provided example implements retries for transient errors, which is sometimes sufficient without introducing a queue.
Vonage supports international SMS. Ensure your account is enabled for the destination countries and be aware of international messaging costs and specific country regulations. The "to" number must be in E.164 format, which starts with '+' followed by country code and number. The provided implementation does basic E.164 format validation, but third party libraries are available for more robust validation if required.
Never commit your `.env` file or `private.key` to version control. Use environment variables provided by your deployment platform for production credentials. The Vonage guide provides a comprehensive `.gitignore` setup to prevent accidental commits of these sensitive files. For local development, a `private.key` file is loaded, and for deployment, a `VONAGE_PRIVATE_KEY_BASE64` environment variable is used.
VONAGE_PRIVATE_KEY_PATH specifies the path to your Vonage private key file for local development. It's used by the Vonage client library to initialize the Vonage SDK. The path is resolved using path.resolve ensuring the key is found, even if called from different locations within the project structure. Ensure this file is *not* committed to version control.
The provided example implements error handling at both the client and API handler level. The `vonageClient.js` catches errors from the SDK, logs them with specific details from Vonage when available, and rethrows them up to the API handler. The `sendSms.js` API handler then catches and returns an appropriate HTTP error response, enabling more robust error reporting and improved debugging.
The `sendSms` function is a RedwoodJS API handler responsible for receiving HTTP requests to send SMS messages. It parses the request body, validates the input, calls the Vonage client library to send the SMS, and returns a JSON response indicating success or failure, including logging for debugging. The provided implementation includes validation for `to` and `message` parameters, specifically checking for the presence of the `+` at the start of the `to` number. Further validation of the number could be performed if necessary.
Vonage automatically concatenates long messages into multiple segments, but be mindful of character limits and associated costs. Standard SMS messages are limited to 160 characters for GSM-7 encoding or 70 for UCS-2, with longer messages split into segments incurring additional costs. It is advisable to inform the users about the potential costs associated with sending longer messages.
RedwoodJS uses Yarn workspaces to manage dependencies. `yarn workspace api add @vonage/server-sdk` installs the Vonage SDK specifically for the API side of your project, where it's needed. This keeps the web side lean and improves dependency management.
The RedwoodJS logger (`pino`) logs informational messages, including the Vonage message ID (`messageUuid`), and any errors encountered during the SMS sending process. This information is crucial for debugging and monitoring. In production, configure log draining to a service like Papertrail, Datadog, or CloudWatch.
Transient network errors can be handled using a retry mechanism. The provided Vonage and RedwoodJS example demonstrates how to integrate an external retry library like `async-retry` around the Vonage `messages.send` call, ensuring messages are sent even if there are transient network problems. Vonage may also implement their own internal retries when communicating with carriers.
The RedwoodJS Prisma schema can be extended to include `SmsLog` which can store a history of sent SMS messages, including recipient, message content, status, timestamp, and Vonage ID. The provided example demonstrates how to insert data into this schema after a message has been successfully submitted to Vonage. Two implementations are provided: one waits for the database write to complete, whereas the other does not. You'll need to consider which is most suitable for your requirements.
Content Loading Error
We encountered an error while processing this content.