Back to Blog
Language How-Tos

How to Schedule SMS Messages in Twilio Using Node.js

Schedule SMS for future delivery in Node.js using the Twilio Messaging API with the SendAt parameter and proper ISO 8601 timestamp formatting.

DA
Danial A
Senior Twilio Consultant, Telphi Consulting
June 22, 2026
8 min read
Twilio
Node.js
JavaScript
How-To
Code
How to Schedule SMS Messages in Twilio Using Node.js

This tutorial shows how to schedule SMS delivery at a specific future time using Twilio's MessageScheduling feature in Node.js. It targets developers who need to send appointment reminders, payment alerts, or marketing messages at a precise future moment without running a cron job or background worker. You will use the sendAt and scheduleType parameters with a Twilio Messaging Service to queue a message for future delivery and optionally cancel it before it sends.

Prerequisites and Setup

Install the packages with npm install twilio dotenv and create a Messaging Service in the Twilio Console under Messaging, Services, then enable Message Scheduling in that service's settings. Copy the Messaging Service SID, which starts with MG, and add it to your .env file. Scheduled messages require a Messaging Service SID as the sender instead of a direct from number, because the scheduling infrastructure runs through the Messaging Service layer. The sendAt value must be a UTC ISO 8601 timestamp between 15 minutes and 7 days from the current time.

npm install twilio dotenv

# .env
TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token_here
TWILIO_MESSAGING_SERVICE_SID=MGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

The Complete Code

JavaScript's Date object generates the correct UTC timestamp; add the desired offset in milliseconds and call .toISOString() to produce the required format. The scheduleType field must be the string 'fixed' to activate scheduling; omitting it causes Twilio to ignore sendAt and send immediately. The returned MessageInstance has a status of scheduled and a SID you can use to cancel the message before it sends. Cancelling is done by calling client.messages(sid).update({ status: 'canceled' }) before the sendAt time arrives.

require('dotenv').config();
const twilio = require('twilio');

const client = twilio(
  process.env.TWILIO_ACCOUNT_SID,
  process.env.TWILIO_AUTH_TOKEN
);
const MESSAGING_SERVICE_SID = process.env.TWILIO_MESSAGING_SERVICE_SID;

async function scheduleSMS(to, body, minutesFromNow) {
  const sendAt = new Date(Date.now() + minutesFromNow * 60 * 1000).toISOString();
  try {
    const message = await client.messages.create({
      to,
      body,
      messagingServiceSid: MESSAGING_SERVICE_SID,
      sendAt,
      scheduleType: 'fixed',
    });
    console.log('Scheduled SID:', message.sid, 'Status:', message.status);
    return message.sid;
  } catch (err) {
    console.error('Schedule error', err.code, err.message);
    return null;
  }
}

async function cancelScheduled(sid) {
  try {
    await client.messages(sid).update({ status: 'canceled' });
    console.log('Cancelled:', sid);
    return true;
  } catch (err) {
    console.error('Cancel error', err.code, err.message);
    return false;
  }
}

scheduleSMS('+15551234567', 'Your appointment is in 1 hour.', 30);

Testing and Debugging

Schedule a message 20 minutes in the future and check the Twilio Console under Messaging Logs where the status shows as scheduled. If you receive error code 21705, your Messaging Service does not have scheduling enabled; open the service settings in the Console and toggle on Message Scheduling. Error code 21706 means your sendAt time is less than 15 minutes from now or more than 7 days ahead; adjust the offset and retry. Test the cancellation function before the send time and refresh the Console to confirm the message status changes to canceled.

Taking It Further

Accept a JavaScript Date object as the parameter instead of a minutes offset so callers can pass any arbitrary future time without performing the arithmetic themselves. Store each scheduled message SID in a database with the recipient, body, and intended send time so you can cancel or reschedule messages from a management UI. Build a reminder system that schedules two messages per appointment: one a day before and one an hour before, and cancels both SIDs if the appointment is rescheduled. For high-volume scheduling, batch your create calls and respect the Twilio API rate limits by adding a delay between requests.

Contact Telphi Consulting to build an automated scheduled messaging system integrated with your CRM or booking platform in Node.js.

Share this article:
0 views

Ready to Transform Your Business Communications?

Get a free consultation with our VoIP experts and discover how we can help you save costs, improve efficiency, and scale your business.

Comments (0)

Join the discussion and share your thoughts (AI-moderated for quality)

Protected by AI moderation

Be the first to comment

No comments yet. Share your thoughts below.