Building Scheduled SMS Reminders with Node.js, Express, and MessageBird - code-examples -

Frequently Asked Questions

Use Node.js with Express, MessageBird API, and a database like PostgreSQL. Build an API endpoint to accept appointment details, validate data, store it, and schedule SMS reminders via MessageBird at specified times before appointments, handling time zones correctly.
MessageBird is a Communications Platform as a Service (CPaaS) that provides the SMS API for sending reminders and the Lookup API for validating phone numbers. It handles the actual sending of the SMS messages based on your schedule.
PostgreSQL is a robust and reliable open-source relational database suitable for storing appointment data securely and efficiently. It provides persistence and data integrity for the application.
Reminders are sent a configurable duration before the appointment, for example, 3 hours prior, as set by the `REMINDER_HOURS_BEFORE` environment variable. The system ensures the appointment is sufficiently in the future to schedule the reminder reliably.
Yes, the reminder message is customizable. The message body is constructed using the customer's name, treatment details, and the localized date and time of the appointment to provide a personalized reminder.
Use the MessageBird Lookup API to validate phone numbers before scheduling reminders. This ensures the numbers are in the correct format and of the mobile type, improving the reliability of SMS delivery. An example country code is demonstrated in the code.
The project leverages Express.js as the web framework to handle API requests and routing, allowing the creation of a structured and maintainable Node.js backend application.
The system uses `moment-timezone` (or alternatives like `date-fns-tz` or the built-in `Intl` object) to handle timezones. It expects client input with timezone information, stores appointments in UTC, and sends reminders formatted to the user's specified local time.
Prisma is a next-generation ORM (Object-Relational Mapper). It simplifies database interactions by allowing developers to define their data models using a schema and then interact with the database using generated JavaScript code.
The app includes robust error handling and logging using `winston`. Validation errors are handled by `express-validator`. For operational errors, like database or API issues, custom AppError objects provide detailed information for logging and appropriate HTTP responses.
The `express-rate-limit` middleware provides basic rate limiting to protect the API from abuse. This feature helps to prevent excessive requests from a single client, mitigating potential security risks and improving the application's resilience.
Set up by initializing a Node.js project, installing dependencies (Express, MessageBird SDK, Prisma, etc.), defining the database schema with Prisma, and configuring environment variables like API keys and database connection details.
Incoming request data is validated using the express-validator middleware. It checks for required fields, data types, formats, and performs basic checks like preventing past appointment dates, ensuring timezone strings are valid, and provides informative error messages.