Developer Quick-Start: Learn how to build an SMS scheduling application with Plivo, Node.js, and Express. Send automated appointment reminders, alerts, and time-sensitive notifications with step-by-step code examples. Want to skip 10DLC vetting queues, carrier surcharges, and multi-step SDK boilerplate? You can send the same SMS directly via SMSRoute's REST API with instant no-KYC provisioning.
Build a production-ready Node.js application using Express to schedule SMS messages for future delivery via the Plivo Messages API. Create an API endpoint that accepts scheduling requests, manages pending messages with node-cron, and sends automated appointment reminders at the specified time.
Project Overview and Goals
What You'll Build:
You'll create a backend API service that enables users to schedule SMS messages. The core functionality includes:
- An API endpoint (POST /schedule) that accepts SMS scheduling requests (recipient number, message content, desired send time)
- A scheduling mechanism (node-cron) that triggers SMS sending at the correct time
- Integration with the Plivo Messages API to send SMS messages
- Basic in-memory storage for scheduled jobs (with caveats for production discussed)
- Error handling, logging, and security considerations
Problem Solved:
Send automated SMS communications at specific future dates and times β such as appointment reminders, follow-up messages, or time-sensitive alerts β without manual intervention when sending.
Technologies Used:
- Node.js: JavaScript runtime for building server-side applications
- Express: Minimal and flexible Node.js web application framework for building APIs
- Plivo Messages API: Communication API for sending messages across various channels, including SMS (uses plivo SDK)
node-cron: Simple cron-like job scheduler for Node.js that triggers tasks at specific timesdotenv: Manages environment variables securelyuuid: Generates unique identifiers for scheduled jobs
Why Plivo?
Plivo provides reliable and scalable communication APIs with extensive features and global reach. The Messages API offers a unified way to handle multiple channels, though this guide focuses on SMS scheduling and appointment reminders.
System Architecture:
+-----------+ +-------------------+ +-----------------+ +-----------------+ +--------------+
| Client |------>| Node.js/Express |----->| Scheduling Logic|----->| Plivo Service |----->| Plivo Cloud |
| (e.g. curl| | API Server | | (node-cron) | | (SDK Wrapper) | | (Messages API|
| Postman) |<------| (Listens on Port) |<-----| (In-Memory Store| +-----------------+ +------|-------+
+-----------+ +-------------------+ +-----------------+ |
| SMS
v
+---------------+
| SMS Recipient |
+---------------+
Prerequisites:
- Node.js and npm (or yarn) installed on your system (Download Node.js)
- Plivo API Account to get API credentials and a virtual number (Sign up for Plivo β free credit available for new accounts)
- Plivo CLI (optional but recommended) for managing applications and numbers β install via npm: npm install -g plivo-cli
- ngrok (optional) for testing webhooks locally if you extend the app to handle status updates or replies (ngrok Website)
- Basic knowledge of JavaScript, Node.js, and REST APIs
Final Outcome:
You'll have a running Node.js Express application with a /schedule endpoint that accepts SMS
scheduling requests and sends messages at the specified future time using Plivo.
1. Setting Up the Project
Initialize your Node.js project and install the necessary dependencies.
Step 1: Create Project Directory
Create a new directory for your project and navigate into it:
mkdir plivo-sms-scheduler
cd plivo-sms-scheduler
Step 2: Initialize Node.js Project
Initialize the project using npm (or yarn) β the -y flag accepts default settings:
npm init -y
This creates a package.json file.
Step 3: Install Dependencies
Install Express, the Plivo SDK, node-cron, dotenv, and uuid:
npm install express plivo node-cron dotenv uuid
- express: Web framework
- plivo: Official Plivo SDK for Node.js
- node-cron: Task scheduler
- dotenv: Loads environment variables from a .env file
- uuid: Generates unique IDs for tracking jobs
Step 4: Project Structure (Recommended)
Create a basic structure for better organization:
mkdir src
mkdir src/routes
mkdir src/services
mkdir src/config
- src/: Contains your main application code
- src/routes/: Holds Express route definitions
- src/services/: Contains business logic (interacting with Plivo or the scheduler)
- src/config/: Configuration files (Plivo SDK initialization)
Step 5: Create Basic Express Server (src/app.js)
Create app.js inside the src directory with this basic Express setup:
require('dotenv').config();
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.status(200).json({ message: 'Plivo SMS Scheduler API is running!' });
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
});
module.exports = app;
Step 6: Create Start Script
Modify the scripts section in your package.json:
{
"main": "src/app.js",
"scripts": {
"start": "node src/app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
}
Test the basic server by running npm start and navigating to http://localhost:3000
or using curl http://localhost:3000. You should see the JSON message. Stop the server with
Ctrl+C.
2. Setting Up Plivo Credentials and SDK
Get credentials and a virtual phone number to interact with the Plivo API. The Plivo SDK uses Auth ID and Auth Token for authentication.
Step 1: Get Your Plivo API Credentials
- Sign up or log in to your Plivo Console
- Navigate to your Dashboard β you'll see your Auth ID and Auth Token
- Copy these credentials (you'll need them for the .env file)
Step 2: Obtain a Plivo Virtual Number
Get a Plivo phone number to send SMS messages from.
Using the Plivo Console:
- Navigate to "Phone Numbers" β "Buy Numbers" in your Plivo Console
- Select country, capabilities (SMS), and type
- Click "Search" and buy a suitable number β note it down in E.164 format (e.g., +12025551234)
Step 3: Configure Environment Variables
Create .env in your project root (same level as package.json). Never commit
this file to Git.
ini
PLIVO_AUTH_ID=YOUR_AUTH_ID
PLIVO_AUTH_TOKEN=YOUR_AUTH_TOKEN
PLIVO_SMS_FROM_NUMBER=YOUR_PLIVO_NUMBER
Replace YOUR_AUTH_ID, YOUR_AUTH_TOKEN, and YOUR_PLIVO_NUMBER with your
actual values from the Plivo Console.
Add .env to your .gitignore:
# .gitignore
node_modules
.env
npm-debug.log
Step 4: Initialize Plivo SDK (src/config/plivoClient.js)
Initialize the SDK instance using your Auth ID and Auth Token:
const plivo = require('plivo');
if (!process.env.PLIVO_AUTH_ID || !process.env.PLIVO_AUTH_TOKEN) {
console.error('Error: PLIVO_AUTH_ID and PLIVO_AUTH_TOKEN must be set in .env');
process.exit(1);
}
const client = new plivo.Client(
process.env.PLIVO_AUTH_ID,
process.env.PLIVO_AUTH_TOKEN
);
console.log('Plivo SDK initialized successfully.');
module.exports = client;
This creates a Plivo client instance using your Auth ID and Token. Basic checks ensure critical environment variables are present.
3. Implementing the SMS Scheduling Logic with Node-Cron
Use node-cron to run tasks at specific times and a simple in-memory object to track scheduled
jobs.
Important Caveat: This in-memory storage is not suitable for production. If the server restarts, all scheduled jobs are lost. For production, use a persistent store like Redis or a database with a dedicated job queue library (e.g., BullMQ, Agenda).
Step 1: Create Scheduler Service (src/services/schedulerService.js)
Create a scheduler service to manage job scheduling and storage:
const cron = require('node-cron');
const { v4: uuidv4 } = require('uuid');
const smsService = require('./smsService');
const scheduledJobs = {};
const scheduleSms = (recipient, message, sendAt) => {
const now = new Date();
if (!(sendAt instanceof Date) || isNaN(sendAt) || sendAt <= now) {
throw new Error('Invalid or past date provided for scheduling.');
}
const jobId = uuidv4();
const task = cron.schedule(sendAt, async () => {
console.log(`[Scheduler] Executing job ${jobId} at ${new Date()}`);
try {
await smsService.sendSms(recipient, message);
console.log(`[Scheduler] Job ${jobId}: SMS sent successfully to ${recipient}.`);
} catch (error) {
console.error(`[Scheduler] Job ${jobId}: Failed to send SMS to ${recipient}. Error: ${error.message}`);
} finally {
delete scheduledJobs[jobId];
console.log(`[Scheduler] Job ${jobId} removed from memory.`);
}
}, {
scheduled: true,
timezone: "Etc/UTC"
});
scheduledJobs[jobId] = {
task: task,
details: {
jobId,
recipient,
message,
sendAt: sendAt.toISOString(),
createdAt: now.toISOString()
}
};
console.log(`[Scheduler] SMS for ${recipient} scheduled successfully. Job ID: ${jobId}, Send At: ${sendAt.toISOString()}`);
return jobId;
};
const cancelSms = (jobId) => {
const job = scheduledJobs[jobId];
if (job) {
job.task.stop();
delete scheduledJobs[jobId];
console.log(`[Scheduler] Job ${jobId} cancelled successfully.`);
return true;
}
console.log(`[Scheduler] Job ${jobId} not found for cancellation.`);
return false;
};
const listScheduledJobs = () => {
return Object.values(scheduledJobs).map(job => job.details);
};
module.exports = {
scheduleSms,
cancelSms,
listScheduledJobs,
};
Explanation:
scheduledJobs: Simple in-memory database β keys are jobId, values contain the node-cron task object and job detailsscheduleSms:
- Takes recipient, message, and a Date object (sendAt)
- Validates sendAt is a valid future date
- Generates a unique jobId using uuid
- Uses cron.schedule(sendAt, ...) to schedule the task for the specified date and time β provides a Date object, which node-cron supports for one-off tasks
- The function passed to cron.schedule runs at sendAt and calls smsService.sendSms
- Includes basic logging and removes the job from scheduledJobs after execution (using finally)
- Sets timezone explicitly (UTC recommended for servers)
- Stores the task and details in scheduledJobs
- Returns the jobId
cancelSms: Finds a job by ID, stops the cron task using task.stop(), and removes it from memorylistScheduledJobs: Returns details of all pending jobs (useful for admin/status endpoints)
4. Implementing the SMS Sending Service with Plivo API
Encapsulate the Plivo SDK interaction to send SMS messages.
Step 1: Create SMS Service (src/services/smsService.js)
const plivoClient = require('../config/plivoClient');
const fromNumber = process.env.PLIVO_SMS_FROM_NUMBER;
if (!fromNumber) {
console.error('Error: PLIVO_SMS_FROM_NUMBER must be set in .env');
process.exit(1);
}
const sendSms = async (recipient, message) => {
console.log(`[SMS Service] Attempting to send SMS to ${recipient}`);
try {
const response = await plivoClient.messages.create({
src: fromNumber,
dst: recipient,
text: message,
});
console.log(`[SMS Service] Message sent successfully. Message UUID: ${response.messageUuid}`);
return response;
} catch (error) {
console.error(`[SMS Service] Error sending SMS to ${recipient}:`, error.response ? error.response : error.message);
throw new Error(`Failed to send SMS via Plivo: ${error.message}`);
}
};
module.exports = {
sendSms,
};
Explanation:
- Imports the configured plivoClient instance
- Retrieves PLIVO\_SMS\_FROM\_NUMBER from environment variables and exits if not set
- sendSms function is async β the SDK call is asynchronous (plivoClient.messages.create returns a Promise)
- Uses plivoClient.messages.create with required parameters: src, dst, text
- Includes try...catch for error handling β logs detailed errors and rethrows a generic error to signal failure to the scheduler
- Logs success, including the messageUuid returned by Plivo (useful for tracking)
5. Building the API Layer (Express Routes for SMS Scheduling)
Create the Express route that uses your scheduler service.
Step 1: Create Schedule Route (src/routes/scheduleRoutes.js)
const express = require('express');
const schedulerService = require('../services/schedulerService');
const router = express.Router();
router.post('/', (req, res, next) => {
const { recipient, message, sendAt } = req.body;
if (!recipient || !message || !sendAt) {
return res.status(400).json({ error: 'Missing required fields: recipient, message, sendAt (ISO 8601 format string).' });
}
let sendAtDate;
try {
sendAtDate = new Date(sendAt);
if (isNaN(sendAtDate)) {
throw new Error('Invalid date format.');
}
} catch (error) {
return res.status(400).json({ error: 'Invalid date format for sendAt. Please use ISO 8601 format (e.g., YYYY-MM-DDTHH:mm:ssZ).' });
}
const now = new Date();
if (sendAtDate <= now) {
return res.status(400).json({ error: 'Scheduled time must be in the future.' });
}
try {
const jobId = schedulerService.scheduleSms(recipient, message, sendAtDate);
res.status(202).json({
message: 'SMS scheduled successfully.',
jobId: jobId,
details: {
recipient: recipient,
sendAt: sendAtDate.toISOString(),
}
});
} catch (error) {
console.error('[API Route /schedule] Error scheduling SMS:', error);
res.status(500).json({ error: 'Failed to schedule SMS.', details: error.message });
}
});
router.get('/', (req, res) => {
const jobs = schedulerService.listScheduledJobs();
res.status(200).json({ scheduledJobs: jobs });
});
router.delete('/:jobId', (req, res) => {
const { jobId } = req.params;
if (!jobId) {
return res.status(400).json({ error: 'Job ID is required.' });
}
const cancelled = schedulerService.cancelSms(jobId);
if (cancelled) {
res.status(200).json({ message: `Job ${jobId} cancelled successfully.` });
} else {
res.status(404).json({ error: `Job ${jobId} not found or already executed/cancelled.` });
}
});
module.exports = router;
Explanation:
- Imports express.Router and schedulerService
POST /route:
- Extracts recipient, message, and sendAt from req.body
- Basic validation: Checks if fields exist and if sendAt is a valid ISO 8601 date string representing a future time (use express-validator or joi for production)
- Calls schedulerService.scheduleSms with validated data
- Responds with 202 Accepted (appropriate for tasks accepted for later processing) and the jobId
- Note: Response omits message field for security β avoid returning sensitive message content in production APIs
- Includes basic error handling for validation errors and service errors
GET /route: Calls schedulerService.listScheduledJobs to return pending job detailsDELETE /:jobIdroute: Extracts jobId from req.params, calls schedulerService.cancelSms, returns success or 404 if job not found
Step 2: Mount the Router in app.js
Update src/app.js to use this router.
require('dotenv').config();
const express = require('express');
const scheduleRoutes = require('./routes/scheduleRoutes');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.status(200).json({ message: 'Plivo SMS Scheduler API is running!' });
});
app.use('/api/schedule', scheduleRoutes);
app.use((err, req, res, next) => {
console.error('[Global Error Handler]:', err.stack || err);
const statusCode = err.statusCode || 500;
const message = err.message || 'Internal Server Error';
res.status(statusCode).json({ error: message });
});
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
require('./config/plivoClient');
console.log('Scheduler service initialized.');
});
module.exports = app;
This adds scheduleRoutes under the /api/schedule path and includes a basic global
error handler middleware.
6. Error Handling, Logging, and Retries
Production apps need more robust error handling beyond the basics included here.
Consistent Error Strategy: Use the global error handler middleware (in app.js)
to catch unhandled errors. Define custom error classes if needed (e.g., ValidationError,
NotFoundError).
Logging: Replace console.log/console.error with a structured
logger like pino or winston. This enables different log levels (debug, info, warn,
error), JSON formatting, and sending logs to files or external services.
Example with pino:
npm install pino pino-pretty
const pino = require('pino');
const logger = pino({
level: process.env.LOG_LEVEL || 'info',
transport: {
target: 'pino-pretty',
options: {
colorize: true
}
}
});
module.exports = logger;
Retries: The current schedulerService logs errors but doesn't retry. For
critical reminders, implement a retry mechanism within the catch block of the
cron.schedule task function.
- Simple retry: Basic for loop with delays
- Exponential backoff: Increase delay between retries (e.g., 1 second, 2 seconds, 4 seconds, 8 seconds) β libraries like async-retry simplify this
- Consider: How many retries? What maximum delay? What happens after final failure (log to error queue, notify admin)?
7. Adding Security Features
Input Validation: Use a dedicated library (express-validator, joi)
for robust validation of recipient (E.164 format), message (length, content), and
sendAt. Sanitize inputs to prevent injection attacks.
Rate Limiting: Protect the /api/schedule endpoint from abuse using
express-rate-limit:
npm install express-rate-limit
const rateLimit = require('express-rate-limit');
const apiLimiter = rateLimit({
windowMs: 15 * 60 * 1000,
max: 100,
message: 'Too many requests from this IP, try again after 15 minutes'
});
app.use('/api/', apiLimiter);
API Key/Authentication: Currently the API is open. For production, protect it with API keys, JWT tokens, or other authentication mechanisms.
Secure Headers: Use middleware like helmet to set HTTP headers for security
(XSS protection, content sniffing prevention, etc.):
npm install helmet
const helmet = require('helmet');
app.use(helmet());
8. Database Schema and Persistence (Production Consideration)
The in-memory scheduledJobs store is not persistent. For production:
Choose a Database:
- Redis: Excellent for caching and simple key-value storage β good for job queues when combined with libraries like BullMQ
- PostgreSQL/MongoDB: More robust for storing job details, status, results, and retry counts
Schema/Data Model: Create a table/collection for ScheduledJobs with fields:
- jobId (Primary Key, UUID)
- recipient (String)
- message (Text)
- sendAt (Timestamp/DateTime)
- status (Enum: "PENDING", "SENT", "FAILED", "CANCELLED")
- createdAt (Timestamp)
- updatedAt (Timestamp)
- plivoMessageUuid (String, nullable β store after successful send)
- failureReason (Text, nullable)
- retryCount (Integer, default 0)
Job Queue Library: Libraries like BullMQ (Redis-based) or Agenda
(MongoDB-based) handle job persistence, scheduling, retries, concurrency, and worker processes more reliably
than node-cron with manual persistence logic.
9. Verification and Testing
Step 1: Manual Verification (curl / Postman)
- Start the server: npm start
- Schedule an SMS: Send a POST request to http://localhost:3000/api/schedule
Using curl:
curl -X POST http://localhost:3000/api/schedule \
-H "Content-Type: application/json" \
-d '{
"recipient": "YOUR_PERSONAL_PHONE_NUMBER",
"message": "Hello from the Plivo Scheduler! Testing 123.",
"sendAt": "2025-04-20T18:30:00Z"
}'
Replace YOUR_PERSONAL_PHONE_NUMBER and adjust sendAt to be a minute or two in the
future using ISO 8601 format (UTC "Z" or specify offset).
Using Postman: Create a POST request, set the URL, select Body β raw β JSON, and paste the JSON payload.
- Check response: You should get a 202 Accepted response with a jobId
- Check logs: Monitor the console output where npm start is running β you should see logs for scheduling, then later for execution and sending (or failure)
- Check phone: Verify you receive the SMS on YOUR\_PERSONAL\_PHONE\_NUMBER at approximately the scheduled time
Step 2: List Pending Jobs
Send a GET request to http://localhost:3000/api/schedule β you should see your scheduled job
(until it executes):
curl http://localhost:3000/api/schedule
Step 3: Cancel a Job
- Schedule another job far in the future β note its jobId from the response
- Send a DELETE request to http://localhost:3000/api/schedule/YOUR\_JOB\_ID:
curl -X DELETE http://localhost:3000/api/schedule/paste_job_id_here
- Check the response (should be 200 OK)
- List jobs again (GET /api/schedule) β the cancelled job should be gone
Step 4: Unit & Integration Tests (Recommended)
For robust applications, write automated tests:
- Unit tests (jest, mocha): Test individual functions in isolation β mock dependencies like node-cron and the plivo SDK, test validation logic, date parsing, error handling
- Integration tests (supertest): Test API endpoints β start the Express server, send HTTP requests using supertest, assert responses, mock external dependencies to avoid actual API calls
10. Best Practices for SMS Appointment Reminders
Timing Considerations:
- Appointment reminders should be sent 24-48 hours before the appointment, with an optional follow-up reminder 2-4 hours before
- Time zone handling: Always store and process times in UTC, then convert to recipient's local time zone when scheduling
- Compliance: Follow TCPA regulations β send SMS only between 8 AM and 9 PM in the recipient's local time zone
Message Content Best Practices:
- Keep messages concise (under 160 characters when possible to fit in a single SMS segment)
- Include key details: date, time, location, and opt-out instructions
- Provide a callback number or link for rescheduling
- Personalize with recipient name when available
Example reminder message:
Hi John, reminder: Your appointment is tomorrow, Apr 20 at 2:30 PM at City Dental (123 Main St). Reply CONFIRM or call 555-0123. Reply STOP to opt out.
Production Enhancements:
- Persistent storage: Use PostgreSQL or MongoDB to store scheduled jobs
- Job queue: Implement BullMQ or Agenda for reliable job processing
- Status webhooks: Set up Plivo delivery receipts to track message status
- Retry logic: Implement exponential backoff for failed deliveries
- Monitoring: Add health checks and alerting for failed jobs
- Send SMS with Plivo and Node.js Express: Complete Tutorial
- Plivo Node.js Express: Two-Way SMS Messaging Guide
- E.164 Phone Number Format Guide
- Understanding the FCC TCPA Compliance Process
Conclusion
You've built a complete SMS scheduling application with Plivo, Node.js, and Express. This foundation supports appointment reminders, time-sensitive alerts, and automated notification systems. For production deployment, implement persistent storage, robust error handling, and monitoring to ensure reliable message delivery.
Ready to scale your SMS capabilities? Sign up for Plivo and start building enterprise-grade communication solutions.
Simpler Alternative: Send the Same SMS With SMSRoute (No-KYC, Direct Routes)
If you want to replace legacy aggregator SDK boilerplate, multi-week compliance queues, and opaque carrier surcharges with a single HTTP request, SMSRoute delivers global SMS over direct Tier-1 carrier interconnects with instant API access and transparent per-country pricing.
curl -X POST https://api.sendsmsnokyc.com/messages \
-H "x-api-key: $SMSROUTE_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"to": ["+14155551234"],
"channel": ["sms"],
"body": "Your verification code is 482910"
}'
- Instant onboarding: No KYC paperwork or sales queues β grab an API key and fund with crypto (BTC/USDT) or card.
- Direct Tier-1 routes: Verify real handset delivery, DLR latency, and Sender ID preservation before launch via SMSRoute Live Carrier Testing.
- Transparent global pricing: Compare exact per-country termination rates on our live pricing page.
Ship Global SMS in One API Call
Skip legacy SDK sprawl and carrier bottlenecks. Send OTPs, alerts, and bulk campaigns across 190+ countries with SMSRoute.
Explore SMSRoute Developers β