Building a RedwoodJS App with Vonage Appointment Scheduling & SMS Reminders - code-examples -

Frequently Asked Questions

Start by creating a new RedwoodJS app using `yarn create redwood-app ./vonage-scheduler`. Then, install necessary dependencies like `@vonage/server-sdk`, `uuid`, and `date-fns` using yarn. Set up your database connection in `api/db/schema.prisma`, define the `Appointment` model within the schema, and apply the migration using `yarn rw prisma migrate dev`.
The Vonage Messages API is used to send SMS confirmations and reminders to users who book appointments through your RedwoodJS application. It's integrated using the `@vonage/server-sdk` which is initialized with your Vonage API key and secret.
RedwoodJS utilizes Prisma as its Object-Relational Mapper (ORM) for database interactions. This simplifies database access, migrations, and management within the application. Prisma also makes it easy to switch databases (PostgreSQL, SQLite, MySQL).
The article suggests sending SMS reminders a configurable amount of time before the appointment. The timing is controlled by an environment variable (`APPOINTMENT_REMINDER_MINUTES_BEFORE`) and an external scheduler triggers the reminder function.
Yes, RedwoodJS, through Prisma, supports SQLite and MySQL in addition to PostgreSQL. Adjust the provider and URL settings within your `schema.prisma` file to switch databases.
Generate a RedwoodJS page with `yarn rw g page AppointmentBooking /book` and modify the generated component to include a form with fields for selecting the appointment time and entering a phone number. The form submission should trigger the `createAppointment` mutation.
An external scheduler like Vercel Cron Jobs or OS cron is used to periodically trigger a RedwoodJS function (`/api/sendReminders`) responsible for checking upcoming appointments and sending reminder SMS messages. This function interacts with both the database and the Vonage API.
Create a helper function (`api/src/lib/vonage.js`) to initialize the Vonage client using `@vonage/server-sdk`. Then within your services, import and call this function to send SMS messages via the Vonage API. Ensure API keys and secrets are stored securely in environment variables.
The `sendSms` helper function in the article is designed to handle common Vonage API errors by returning a specific object that includes success status, any error messages and details, and an optional message ID. The service then logs these details and uses the success status to update appointment confirmation status appropriately.
The booking code provides a unique identifier for each appointment. Although not fully implemented in this example, it's intended to facilitate appointment cancellation or lookup functionalities in a more complete application.
Reminders are sent via a dedicated Redwood Function (`/api/sendReminders`) triggered externally by a scheduler. This function queries the database for upcoming appointments and sends SMS reminders using the Vonage API.
The E.164 format ensures consistent and reliable phone number handling, crucial for SMS delivery. It's the internationally recommended standard and using it avoids issues caused by varying national number formats. Libraries like `libphonenumber-js` provide robust validation.
You'll need Node.js, Yarn, a Vonage API account with a virtual number, access to a database (PostgreSQL, SQLite, or MySQL), and basic command-line familiarity. The frontend examples also assume Tailwind CSS is set up.