Build Appointment Reminders with Next.js and Twilio Message Scheduling - code-examples -

Frequently Asked Questions

Use Twilio's Message Scheduling feature with a Messaging Service. Create an appointment, specify the reminder time, and Twilio handles the sending. This eliminates the need for custom cron jobs or background workers on your backend.
Twilio Message Scheduling allows you to schedule SMS/MMS messages for future delivery. This is ideal for appointment reminders, notifications, or any time-sensitive communication that needs to be sent automatically at a specific time.
A Messaging Service is mandatory for using Twilio's Message Scheduling. It manages the sending process, including selecting the appropriate number from your pool, and ensures compliance requirements are met. This simplifies scalability and number management.
Use date-fns-tz when working with user-provided time zones and converting local times to UTC for storage and scheduling. This library handles various time zones accurately and ensures consistency in scheduling.
No, Twilio's scheduling has a minimum 15-minute lead time. If you try to schedule a message sooner, the API will return an error. Reminders must be scheduled at least 15 minutes from now, and within 7 days.
The app uses Next.js and Node.js for the core framework, Twilio Programmable Messaging for SMS, Prisma ORM with PostgreSQL for data, Zod for validation, and date-fns/date-fns-tz for time zone handling.
Create a file in the 'pages/api' directory (e.g., 'appointments.ts'). Inside, export a default asynchronous function that handles the request and response. This function will contain your API logic to process and schedule appointments.
Prisma is an Object-Relational Mapper (ORM) used for database interaction. It simplifies database operations, provides type safety, and manages migrations, making it easier to work with PostgreSQL in the project.
Zod provides data validation, ensuring data integrity and preventing errors. It validates the incoming requests against a predefined schema to ensure all required data is present and is in the correct format.
Collect the user's time zone along with appointment details. Convert the local appointment time to UTC using a library like date-fns-tz. Store appointment times in UTC in your database, then convert back to local time when needed for display or reminders.
Wrap your Twilio API calls in a try...catch block. Check for 'error.response' and 'error.status' to identify specific Twilio errors. Return appropriate error messages to the user, and log details for debugging.
PostgreSQL is used. Initialize Prisma with PostgreSQL using 'npx prisma init --datasource-provider postgresql'. Configure the 'DATABASE_URL' in '.env.local' with your database credentials.
Store sensitive information like API keys and the database URL in '.env.local'. This file is automatically ignored by Git, preventing accidental exposure.
In the Twilio Console, go to Messaging > Services > Create Messaging Service. Add your Twilio phone number as a sender to the service. Then, obtain the Messaging Service SID (starting with 'MG') to use in your app.