Menu

Rate this page:

Thanks for rating this page!

We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

Appointment Reminders with Node.js and Express

Download the Code

This Node.js Express web application sends out reminders for future appointments that customers can create through the application as well. This is done through a background job that runs every minute.

On this tutorial we'll point out the key bits of code that make this application work. Check out the project README on GitHub to see how to run the code yourself.

Check out how Yelp uses SMS to confirm restaurant reservations for diners.

Let's get started! Click the button below to get started.

Let's get started!

Configure the application to use Twilio

Before we can use the Twilio API to send reminder text messages we need to configure our account credentials. These can be found on your Twilio Console. You'll also need an SMS-enabled phone number - you can find or purchase a new one here.

Loading Code Sample...
      
      
      
      
      .env

      In order to send an appointment reminder we need to have an appointment first!

      Let's create some appointments

      Create a new appointment

      On the controller we input the information required (a customer's name and phone number, plus a time and date for the appointment) by saving it on an Appointment model.

      We use mongoose in this application to store our model in MongoDB.

      var AppointmentSchema = new mongoose.Schema({
        name:String,
        phoneNumber: String,
        notification : Number,
        timeZone : String,
        time : {type : Date, index : true}
      });
      
      Loading Code Sample...
          
          
          
          
          routes/appointments.js

          Create a new appointment

          routes/appointments.js

          Now that we have our Appointment created, let's see how to schedule a reminder for it.

          Schedule the reminder job

          Schedule a job to send reminders

          Every minute we'd like our application to check the appointments database to see if any appointments are coming up that require reminders to be sent out.

          To do this we use node-cron.

          We configure on the start function both the job code we'd like to run, and the interval on which to run it. Then we call it from the application execution entry point like this: scheduler.start()

          Loading Code Sample...
              
              
              
              
              scheduler.js

              This start function uses a notificationsWorker, next we'll see how it works.

              Run the job!

              Create a worker function to run the job

              To actually execute our recurring job logic, we create a worker function which uses a Static Model Method to query the database for upcoming appointments and sends reminders as necessary.

              Loading Code Sample...
                  
                  
                  
                  
                  workers/notificationsWorker.js

                  Create a worker function to run the job

                  workers/notificationsWorker.js

                  Next, let's see how the Appointment job works in detail.

                  Find appointments that need reminders

                  Find appointments that need reminders

                  Our recurring job uses a static model method of the Appointment model to query the database for appointments coming up in the current minute and send out reminder messages using a Twilio REST Client we previously initialized with our Twilio account credentials.

                  Because of the fact that appointments are defined in different time zones, we use Moment.js library in order to properly query every upcoming appointment considering its time zone.

                  Loading Code Sample...
                      
                      
                      
                      
                      models/appointment.js

                      All that is left is to send the actual SMS. We'll see that next.

                      Send reminder messages with the Twilio API

                      Send reminder messages with the Twilio API

                      This code is called for every appointment coming up that requires a reminder to be sent. We provide a configuration object with a to field, which is the customer's phone number, a from field, which is a number in our account, and a body field, which contains the text of the message. Then we pass it to the sendMessage method along with a callback to log errors and success.

                      Loading Code Sample...
                          
                          
                          
                          
                          models/appointment.js

                          That's it! Our application is all set to send out reminders for upcoming appointments.

                          Where to next?

                          Where to next?

                          We hope you found this sample application useful. If you're a Node.js/Express developer working with Twilio you might enjoy these other tutorials:

                          Workflow Automation

                          Build a ready-for-scale automated SMS workflow for a vacation rental company.

                          Browser Calls

                          Make browser-to-phone and browser-to-browser calls with ease.

                          Did this help?

                          Thanks for checking out this tutorial! If you have any feedback to share with us, please reach out on Twitter... we'd love to hear your thoughts, and know what you're building!

                          Mario Celi Jose Oliveros Agustin Camino Andrew Baker David Prothero Kat King Hector Ortega

                          Need some help?

                          We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd browsing the Twilio tag on Stack Overflow.

                          Loading Code Sample...