Building Production-Ready SMS Marketing Campaigns with Node.js, Express, and Vonage - code-examples -

Frequently Asked Questions

Use Node.js with the Express framework and the Vonage Messages API. This combination allows you to build an application that can manage recipient lists, send templated SMS messages, and handle replies and delivery statuses efficiently.
The Vonage Messages API is a multi-channel communication platform, but in this tutorial, it's used specifically for sending SMS messages. It integrates seamlessly with Node.js using the official @vonage/server-sdk package.
The example application uses a 'subscribed' flag in its recipient database model. When a user replies with a keyword like 'STOP', the webhook handler can update this flag to prevent future messages to that number.
Rate limiting is crucial to avoid overwhelming carriers and being flagged as spam. The tutorial enforces this using setTimeout based on the VONAGE_SEND_RATE_LIMIT_MPS environment variable.
The tutorial uses PostgreSQL as its relational database to store campaign details, recipient lists, and message logs. Prisma, an ORM, simplifies database interactions.
Ngrok is recommended for creating a public URL that tunnels requests to your local development server, enabling you to test Vonage webhooks during development.
Registration for A2P 10DLC is mandatory if you're sending SMS messages *to* US numbers using standard long codes (+1). This must be done through Vonage after the initial project setup to ensure deliverability and compliance with carrier regulations.
Prisma is used as an Object-Relational Mapper (ORM) to simplify database operations. It allows you to interact with the PostgreSQL database using JavaScript, providing type safety and a clean, efficient way to manage database interactions.
You can create campaigns by sending a POST request to the /api/campaigns route. The request should include the campaign name, message body, and an array of recipient phone numbers. This endpoint uses the Vonage Messages API to send the created campaign and logs results.
The code uses setTimeout to introduce a delay between each message sent via the Vonage API. The delay is calculated based on the VONAGE_SEND_RATE_LIMIT_MPS environment variable, which defines the maximum messages sent per second.
The subscribed flag in the Recipient model tracks whether a recipient has opted out of receiving messages. It's crucial for managing opt-outs and ensuring compliance.
The optional GET /api/campaigns/:id/status endpoint can provide a summary of message statuses (e.g. sent, delivered, failed) for a given campaign ID.
For development, you can use the VONAGE_PRIVATE_KEY_PATH environment variable, but for production always prefer the VONAGE_PRIVATE_KEY_CONTENT environment variable. Never directly store keys in your code repository.
Set Inbound and Status URLs for your Vonage Application, pointing to /webhooks/inbound and /webhooks/status on your Express server. Use ngrok to expose these URLs to Vonage during development.