Tutorials

Build Scheduled SMS Reminders with Next.js and Plivo

A guide detailing how to build a production-ready application using Next.js and Plivo to schedule and automatically send SMS reminders, covering setup, implementation, deployment, and monitoring.

JH
Jacky Hossein
β€’ Principal Telecommunications Architect β€’ Published Nov 5, 2026 β€’ 16 min read

Developer Quick-Start: A guide detailing how to build a production-ready application using Next.js and Plivo to schedule and automatically send SMS reminders, covering setup, implementation, deployment, and monitoring. 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.

This guide details how to build a production-ready application using Next.js and Plivo to schedule and automatically send SMS reminders. We'll cover everything from project setup and core logic implementation to deployment and monitoring.

The final application will enable users (or an admin) to define a phone number, a message, and a future date/time. At the scheduled time, the system will automatically send the specified message to the target phone number via SMS using the Plivo API. This is ideal for appointment reminders, event notifications, follow-ups, and more.

Project overview and goals

Goal: Create a reliable system for scheduling and sending SMS messages at specific future times.

Problem Solved: Automates the process of sending timely reminders, reducing no-shows for appointments, ensuring users receive critical information on time, and freeing up manual effort.

Technologies:

  • Next.js (App Router): A React framework for building full-stack web applications. Chosen for its robust features, server components, API routes, and excellent developer experience.
  • Plivo: A cloud communications platform providing SMS APIs. Chosen for its reliable SMS delivery, developer-friendly APIs, and clear pricing.
  • Prisma: A next-generation Node.js and TypeScript ORM. Used for database interaction, schema management, and type safety.
  • Vercel Postgres: A serverless SQL database. Chosen for its ease of integration with Vercel and Next.js.
  • Vercel Cron Jobs: A serverless scheduler for running jobs reliably. Used to trigger the check for and sending of due reminders.
  • Tailwind CSS: A utility-first CSS framework for styling the minimal UI.
  • TypeScript: For static typing and improved code quality.

System Architecture:

(Note: The following diagram uses Mermaid syntax. Ensure your publishing platform supports Mermaid rendering, or replace this with a static image alternative.)

graph TD
    A[User/Client Browser] -- 1. Schedule Request (Form Submit) --> B(Next.js App / API Route);
    B -- 2. Save Schedule --> C[(Vercel Postgres via Prisma)];
    D{Vercel Cron Job} -- 3. Trigger Check (e.g., every minute) --> E(Next.js Cron Handler API Route);
    E -- 4. Query Due Schedules --> C;
    E -- 5. Send SMS Request --> F(Plivo SMS API);
    F -- 6. Send SMS --> G([End User Phone]);
    E -- 7. Update Schedule Status --> C;

Prerequisites:

  • Node.js (v18 or later recommended)
  • npm, yarn, or pnpm package manager
  • A Plivo account with API credentials and a Plivo phone number
  • A Vercel account (for Vercel Postgres and Cron Jobs deployment)
  • Git and a GitHub account (or similar Git provider)

Final Outcome: A deployed Next.js application capable of accepting SMS scheduling requests via a simple UI or API, storing them, and reliably sending them at the scheduled time using Plivo and Vercel Cron Jobs.

1. Setting up the project

Let's initialize our Next.js project and install the necessary dependencies.

  1. Create Next.js App: Open your terminal and run the following command, choosing options like TypeScript, Tailwind CSS, App Router, etc., when prompted.
   npx create-next-app@latest plivo-scheduler-app
   cd plivo-scheduler-app
  1. Install Dependencies: Add Plivo, Prisma, and date-fns for date manipulation.
   npm install plivo @prisma/client date-fns date-fns-tz zod
   npm install --save-dev prisma
  • plivo: Plivo Node.js SDK.
  • @prisma/client: Prisma database client.
  • date-fns & date-fns-tz: Robust date/time handling and time zone support.
  • zod: Schema declaration and validation library.
  • prisma (dev): Prisma CLI for migrations and studio.
  1. Initialize Prisma: Set up Prisma with PostgreSQL as the provider.
   npx prisma init --datasource-provider postgresql

This creates a prisma directory with a schema.prisma file and a .env file for your database connection string.

  1. Configure Environment Variables: Open the .env file (renamed automatically from prisma/.env to the project root by create-next-app, or create .env.local if preferred). Add placeholders for Plivo credentials and the database URL. We'll get the actual values later.

.env

   # .env.local (or .env)

   # Database (Vercel Postgres will provide this)
   POSTGRES_PRISMA_URL=""postgresql://user:password@host:port/database?sslmode=require""
   POSTGRES_URL_NON_POOLING=""postgresql://user:password@host:port/database?sslmode=require"" # For migrations

   # Plivo Credentials
   PLIVO_AUTH_ID=""YOUR_PLIVO_AUTH_ID""
   PLIVO_AUTH_TOKEN=""YOUR_PLIVO_AUTH_TOKEN""
   PLIVO_SENDER_NUMBER=""YOUR_PLIVO_PHONE_NUMBER"" # Must be in E.164 format, e.g., +14155552671

   # Cron Job Security (Optional but Recommended)
   CRON_SECRET=""YOUR_SECURE_RANDOM_STRING""
  • Security: .env.local is ignored by Git by default, keeping your secrets safe. Never commit files containing sensitive credentials.
  • POSTGRES_PRISMA_URL vs POSTGRES_URL_NON_POOLING: Vercel Postgres provides two URLs. The Prisma client uses the pooled connection (POSTGRES\_PRISMA\_URL). Prisma Migrate needs the non-pooled version (POSTGRES\_URL\_NON\_POOLING). Ensure both are set, especially when deploying.
  • CRON_SECRET: A secret string you generate to verify that requests to your cron handler endpoint genuinely come from Vercel Cron Jobs.
  1. Project Structure: Your basic structure within the app directory will look like this:
  • app/page.tsx: Frontend UI for scheduling.
  • app/api/schedule/route.ts: API endpoint to handle schedule creation.
  • app/api/cron/route.ts: API endpoint triggered by Vercel Cron to send due messages.
  • lib/: Utility functions (Prisma client, Plivo client).
  • prisma/schema.prisma: Database schema definition.

2. Implementing core functionality

We'll now build the key parts: the UI form, the API to save schedules, the Plivo integration, and the cron job logic.

2.1 Database Schema (prisma/schema.prisma)

Define the model for storing scheduled SMS messages.

Prisma

// prisma/schema.prisma

generator client {
  provider = ""prisma-client-js""
}

datasource db {
  provider  = ""postgresql""
  url       = env(""POSTGRES_PRISMA_URL"") // Uses connection pooling
  directUrl = env(""POSTGRES_URL_NON_POOLING"") // Used for migrations
}

model Schedule {
  id          String   @id @default(cuid()) // Unique identifier
  phoneNumber String   // Recipient phone number (E.164 format)
  message     String   // SMS message content
  sendAt      DateTime // Scheduled time in UTC
  status      String   @default(""PENDING"") // PENDING, SENT, FAILED
  plivoMessageId String?  // Store Plivo's message UUID after sending
  error       String?  // Store error message if sending fails
  createdAt   DateTime @default(now())
  updatedAt   DateTime @updatedAt

  @@index([sendAt, status]) // Index for efficient querying by the cron job
}
  • sendAt: Stores the scheduled time in UTC to avoid time zone ambiguity.
  • status: Tracks the state of the scheduled message.
  • plivoMessageId: Useful for tracking the message status within Plivo if needed.
  • error: Logs any issues during sending.
  • @@index: Crucial for performance. The cron job frequently queries for PENDING schedules around the current time.

2.2 Prisma Client Setup (lib/prisma.ts)

Create a reusable Prisma client instance.

import { PrismaClient } from '@prisma/client';

declare global {

  var prisma: PrismaClient | undefined;
}

export const prisma =
  global.prisma ||
  new PrismaClient({
    log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
  });

if (process.env.NODE_ENV !== 'production') {
  global.prisma = prisma;
}
  • This pattern prevents creating multiple Prisma Client instances during development hot-reloading.
  • Logging is more verbose in development.

2.3 Plivo Client Setup (lib/plivo.ts)

Initialize the Plivo client and create a helper function for sending SMS.

import * as plivo from 'plivo';

const authId = process.env.PLIVO_AUTH_ID;
const authToken = process.env.PLIVO_AUTH_TOKEN;
const senderNumber = process.env.PLIVO_SENDER_NUMBER;

if (!authId || !authToken || !senderNumber) {
  throw new Error(""Plivo credentials (AUTH_ID, AUTH_TOKEN, SENDER_NUMBER) are not set in environment variables."");
}

const client = new plivo.Client(authId, authToken);

interface SendSmsResult {
  success: boolean;
  messageUuid?: string;
  error?: string;
}

export async function sendPlivoSms(to: string, body: string): Promise<SendSmsResult> {
  console.log(`Attempting to send SMS via Plivo to: ${to}`);
  try {
    const response = await client.messages.create({
      src: senderNumber as string,
      dst: to,
      text: body,



    });

    console.log(`Plivo API Response:`, response);


    if (response.messageUuid && response.messageUuid.length > 0) {
        const firstUuid = response.messageUuid[0];
        console.log(`SMS submitted successfully to Plivo. Message UUID: ${firstUuid}`);
        return { success: true, messageUuid: firstUuid };
    } else {

        const errorMessage = response.error || 'Unknown error from Plivo (No message UUID)';
        console.error(`Plivo sending failed (API level): ${errorMessage}`);
        return { success: false, error: errorMessage };
    }
  } catch (error: any) {
    const errorMessage = error.message || 'Failed to send SMS via Plivo';
    console.error(`Plivo sending failed (SDK/Network level): ${errorMessage}`, error);
    return { success: false, error: errorMessage };
  }
}
  • Error Handling: Checks for environment variables and wraps the API call in a try...catch.
  • Result Object: Returns a structured object indicating success/failure and including the messageUuid or error message.
  • Logging: Includes logs for attempting and results of sending.
  • Type Safety: Uses TypeScript interfaces.
  • Message UUID: The code accesses response.messageUuid[0] because the Plivo Node.js SDK returns the UUID(s) in an array.

2.4 Scheduling UI (app/page.tsx)

A simple form to create new schedules.

""use client"";

import { useState, FormEvent } from 'react';
import { format, parse, isValid } from 'date-fns';
import { utcToZonedTime, zonedTimeToUtc } from 'date-fns-tz';

export default function HomePage() {
  const [phoneNumber, setPhoneNumber] = useState('');
  const [message, setMessage] = useState('');
  const [scheduleDate, setScheduleDate] = useState('');
  const [scheduleTime, setScheduleTime] = useState('');
  const [timeZone, setTimeZone] = useState(Intl.DateTimeFormat().resolvedOptions().timeZone);
  const [status, setStatus] = useState('');
  const [isLoading, setIsLoading] = useState(false);

  const handleSubmit = async (e: FormEvent) => {
    e.preventDefault();
    setIsLoading(true);
    setStatus('');


    if (!phoneNumber || !message || !scheduleDate || !scheduleTime) {
      setStatus('Error: Please fill in all fields.');
      setIsLoading(false);
      return;
    }


    const dateTimeString = `${scheduleDate}T${scheduleTime}:00`;
    let localDate: Date;
    try {

        localDate = parse(dateTimeString, ""yyyy-MM-dd'T'HH:mm:ss"", new Date());
        if (!isValid(localDate)) {
            throw new Error(""Invalid date/time format"");
        }
    } catch (err) {
        setStatus(`Error: Invalid date or time format. Use YYYY-MM-DD and HH:MM.`);
        setIsLoading(false);
        return;
    }


    const scheduledAtUtc: Date = zonedTimeToUtc(localDate, timeZone);
    console.log(""Local Input:"", localDate, ""TimeZone:"", timeZone, ""Calculated UTC:"", scheduledAtUtc);


    if (scheduledAtUtc <= new Date()) {
      setStatus('Error: Scheduled time must be in the future.');
      setIsLoading(false);
      return;
    }

    try {
      const response = await fetch('/api/schedule', {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({
          phoneNumber,
          message,

          scheduledAt: scheduledAtUtc.toISOString(),
        }),
      });

      const result = await response.json();

      if (response.ok) {
        setStatus(`Success! Schedule created with ID: ${result.id}`);

        setPhoneNumber('');
        setMessage('');
        setScheduleDate('');
        setScheduleTime('');
      } else {
        setStatus(`Error: ${result.error || 'Failed to create schedule.'}`);
      }
    } catch (error) {
      console.error(""Scheduling failed:"", error);
      setStatus('Error: An unexpected error occurred.');
    } finally {
      setIsLoading(false);
    }
  };




  const renderTimezoneSelector = () => (
    <select
        value={timeZone}
        onChange={(e) => setTimeZone(e.target.value)}
        // Added text-black for explicit visibility on light backgrounds.
        // Be mindful this might conflict if implementing a dark mode theme.
        className=""mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm text-black""
    >
        {/* Add more timezones as needed */}
        <option value=""America/New_York"">America/New_York (ET)</option>
        <option value=""America/Chicago"">America/Chicago (CT)</option>
        <option value=""America/Denver"">America/Denver (MT)</option>
        <option value=""America/Los_Angeles"">America/Los_Angeles (PT)</option>
        <option value=""Europe/London"">Europe/London (GMT/BST)</option>
        <option value=""UTC"">UTC</option>
        {/* Add the user's detected timezone if it's not already in the list */}
         {Intl.DateTimeFormat().resolvedOptions().timeZone !== 'UTC' && !['America/New_York', 'America/Chicago', 'America/Denver', 'America/Los_Angeles', 'Europe/London'].includes(Intl.DateTimeFormat().resolvedOptions().timeZone) && (
            <option value={Intl.DateTimeFormat().resolvedOptions().timeZone}>{Intl.DateTimeFormat().resolvedOptions().timeZone} (Detected)</option>
         )}
    </select>
  );

  return (
    <main className=""flex min-h-screen flex-col items-center justify-center p-6 bg-gray-100"">
      <div className=""w-full max-w-md p-8 space-y-6 bg-white rounded-lg shadow-md"">
        <h1 className=""text-2xl font-bold text-center text-gray-900"">Schedule SMS Reminder</h1>
        <form onSubmit={handleSubmit} className=""space-y-4"">
          <div>
            <label htmlFor=""phoneNumber"" className=""block text-sm font-medium text-gray-700"">Phone Number (E.164 format)</label>
            <input
              id=""phoneNumber""
              name=""phoneNumber""
              type=""tel""
              required
              value={phoneNumber}
              onChange={(e) => setPhoneNumber(e.target.value)}
              placeholder=""+14155552671""
              // Added text-black for explicit visibility on light backgrounds.
              // Be mindful this might conflict if implementing a dark mode theme.
              className=""mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm text-black""
            />
          </div>
          <div>
            <label htmlFor=""message"" className=""block text-sm font-medium text-gray-700"">Message</label>
            <textarea
              id=""message""
              name=""message""
              rows={3}
              required
              value={message}
              onChange={(e) => setMessage(e.target.value)}
              // Added text-black for explicit visibility on light backgrounds.
              // Be mindful this might conflict if implementing a dark mode theme.
              className=""mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm text-black""
            />
          </div>
          <div className=""grid grid-cols-2 gap-4"">
             <div>
                <label htmlFor=""scheduleDate"" className=""block text-sm font-medium text-gray-700"">Date</label>
                <input
                  id=""scheduleDate""
                  name=""scheduleDate""
                  type=""date"" // HTML5 date input
                  required
                  value={scheduleDate}
                  onChange={(e) => setScheduleDate(e.target.value)}
                  // Added text-black for explicit visibility on light backgrounds.
                  // Be mindful this might conflict if implementing a dark mode theme.
                  className=""mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm text-black""
                />
             </div>
             <div>
                 <label htmlFor=""scheduleTime"" className=""block text-sm font-medium text-gray-700"">Time</label>
                 <input
                   id=""scheduleTime""
                   name=""scheduleTime""
                   type=""time"" // HTML5 time input
                   required
                   value={scheduleTime}
                   onChange={(e) => setScheduleTime(e.target.value)}
                   // Added text-black for explicit visibility on light backgrounds.
                   // Be mindful this might conflict if implementing a dark mode theme.
                   className=""mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-indigo-500 focus:ring-indigo-500 sm:text-sm text-black""
                 />
             </div>
          </div>
           <div>
             <label htmlFor=""timeZone"" className=""block text-sm font-medium text-gray-700"">Time Zone</label>
             {renderTimezoneSelector()}
           </div>

          <div>
            <button
              type=""submit""
              disabled={isLoading}
              className={`w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white ${
                isLoading ? 'bg-indigo-400' : 'bg-indigo-600 hover:bg-indigo-700'
              } focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 disabled:opacity-50`}
            >
              {isLoading ? 'Scheduling...' : 'Schedule Reminder'}
            </button>
          </div>
          {status && (
            <p className={`text-sm ${status.startsWith('Error:') ? 'text-red-600' : 'text-green-600'}`}>
              {status}
            </p>
          )}
        </form>
      </div>
    </main>
  );
}
  • ""use client"";: Necessary for using React hooks (useState) and event handlers.
  • State Management: Uses useState for form inputs, loading state, and status messages.
  • Time Zone Handling:
  • Detects the user's local time zone using Intl.DateTimeFormat.
  • Provides a basic dropdown to select the time zone (acknowledged as basic).
  • Uses date-fns-tz's zonedTimeToUtc to convert the user's input date/time (parsed in their selected time zone) into a UTC Date object before sending it to the backend API.
  • API Call: Uses fetch to POST the schedule data to /api/schedule.
  • Feedback: Displays success or error messages to the user.
  • Basic Styling: Uses Tailwind CSS classes. Added explicit text-black to inputs for visibility on light backgrounds; this might need adjustment for dark mode support.

3. Building the API layer

We need an API endpoint to receive the schedule requests from the frontend (or other clients).

3.1 Schedule Creation API (app/api/schedule/route.ts)

This Next.js Route Handler validates the incoming request and saves the schedule to the database.

import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { z } from 'zod';
import { isValid, parseISO } from 'date-fns';

const scheduleSchema = z.object({



  phoneNumber: z.string().regex(/^\+[1-9]\d{1,14}$/, { message: ""Invalid phone number format (must be E.164, e.g., +14155552671)""}),
  message: z.string().min(1, { message: ""Message cannot be empty"" }).max(1600, { message: ""Message too long"" }),
  scheduledAt: z.string().refine((val) => {
    const date = parseISO(val);
    return isValid(date) && date > new Date();
  }, { message: ""Invalid scheduled date or date is not in the future""}),
});

export async function POST(request: Request) {
  try {
    const body = await request.json();


    const validation = scheduleSchema.safeParse(body);
    if (!validation.success) {
      console.error(""Schedule validation failed:"", validation.error.errors);

      return NextResponse.json({ error: validation.error.flatten().fieldErrors }, { status: 400 });
    }

    const { phoneNumber, message, scheduledAt } = validation.data;


    const scheduledAtDate = parseISO(scheduledAt);


    const newSchedule = await prisma.schedule.create({
      data: {
        phoneNumber,
        message,
        sendAt: scheduledAtDate,
        status: 'PENDING',
      },
    });

    console.log(""New schedule created:"", newSchedule);


    return NextResponse.json({ id: newSchedule.id, message: ""Schedule created successfully"" }, { status: 201 });

  } catch (error: any) {
    console.error(""Error creating schedule:"", error);


     if (error instanceof z.ZodError) {

        return NextResponse.json({ error: ""Invalid input data"" }, { status: 400 });
    }

    return NextResponse.json({ error: ""Internal Server Error"", details: error.message }, { status: 500 });
  }
}
  • Route Handler: Uses the standard Next.js App Router route.ts pattern.
  • Input Validation: Leverages zod to define a schema (scheduleSchema) and validate the request body. This ensures the phone number format is basic E.164, the message isn't empty, and scheduledAt is a valid ISO date string representing a future time. Returns specific error messages on failure. A note clarifies the basic nature of the regex and suggests libphonenumber-js for better validation.
  • Database Interaction: Uses the imported prisma client to create a new schedule record.
  • UTC Handling: Assumes the scheduledAt string received from the client is already in UTC (ISO 8601 format), as prepared by the frontend. It parses this string into a Date object, which Prisma handles correctly as a UTC timestamp in the database.
  • Error Handling: Includes a try...catch block to handle potential errors during JSON parsing, validation, or database operations, returning appropriate HTTP status codes.
  • Response: Returns a 201 Created status with the ID of the newly created schedule on success, or error details on failure.

3.2 Testing the API Endpoint

You can test this endpoint using curl or a tool like Postman/Insomnia after running your Next.js development server (npm run dev).

Curl Example:

FUTURE_UTC_DATE=""2025-04-20T11:00:00Z""

curl -X POST http://localhost:3000/api/schedule \
 -H ""Content-Type: application/json"" \
 -d '{
   ""phoneNumber"": ""+14155552671"",
   ""message"": ""Your test reminder from curl!"",
   ""scheduledAt"": ""'""$FUTURE_UTC_DATE""'""
 }'
  • Note: Added strong emphasis regarding the need to change the hardcoded future date in the curl command.

4. Integrating with third-party services

4.1 Plivo Setup

  1. Sign Up: Create an account at Plivo.com.
  2. Get Credentials:
  • Navigate to your Plivo Console dashboard.
  • On the overview page, you'll find your Auth ID and Auth Token.
  • Copy these values.
  1. Buy a Phone Number:
  • Go to Phone Numbers -> Buy Numbers in the Plivo console.
  • Search for a number with SMS capabilities in your desired region (e.g., USA).
  • Purchase the number.
  • Note the number in E.164 format (e.g., +12025551234).
  1. Update Environment Variables: Paste your Auth ID, Auth Token, and the purchased phone number into your .env.local file.

.env

   # .env.local
   PLIVO_AUTH_ID=""YOUR_ACTUAL_PLIVO_AUTH_ID""
   PLIVO_AUTH_TOKEN=""YOUR_ACTUAL_PLIVO_AUTH_TOKEN""
   PLIVO_SENDER_NUMBER=""+12025551234"" # Your purchased Plivo number
   # ... other variables
  1. Add Test Numbers: During the free trial, Plivo requires you to verify destination numbers. Go to Phone Numbers -> Sandbox Numbers and add the phone number(s) you intend to send test messages to.

4.2 Vercel Postgres Setup

  1. Create Database:
  • Go to your Vercel Dashboard.
  • Navigate to the Storage tab.
  • Click Create Database and select Postgres.
  • Choose a region, give it a name (e.g., plivo-scheduler-db), and create it.
  1. Connect Project:
  • Select the Vercel project you intend to deploy this application to (or create one by importing your Git repository).
  • Click Connect Project.
  1. Get Connection Strings:
  • Once connected, go to the database settings (.env.local tab).
  • Vercel provides several environment variables. Copy the values for POSTGRES\_PRISMA\_URL and POSTGRES\_URL\_NON\_POOLING.
  1. Update Environment Variables: Paste these connection strings into your local .env.local file, replacing the placeholders.

4.3 Apply Database Schema

With the database connection string configured in .env.local, apply your Prisma schema to the Vercel Postgres database.

npx prisma db push
  • This command synchronizes your database schema with the definition in prisma/schema.prisma. It's suitable for development and simple production setups. For complex changes requiring explicit migration steps later, consider using prisma migrate dev (local) and prisma migrate deploy (CI/CD).

5. Implementing error handling, logging, and retries

Our current code has basic try...catch blocks and console.log statements. Let's refine this.

  • Error Handling:
  • The API route (/api/schedule) uses Zod for specific validation errors and returns 400 status codes.
  • General server errors return a 500 status.
  • The sendPlivoSms function returns a { success: boolean, error?: string } object, allowing the caller (cron job) to handle Plivo-specific failures.
  • Logging:
  • console.log and console.error are used throughout. For production, consider integrating a dedicated logging service (like Logtail, Datadog, Axiom) by replacing console calls or using a library like pino.
  • Vercel automatically captures console output from Serverless Functions and Cron Jobs, viewable in the deployment logs.
  • Retries:
  • Plivo: Plivo handles some level of carrier-side retries internally.
  • Cron Job: Vercel Cron Jobs have built-in basic retry mechanisms if the handler function itself fails (e.g., throws an unhandled error or returns a 5xx status).
  • Application-Level Retries: The current cron job logic (Section 12.1 - Note: Section 12.1 was referenced but not provided in the original text) implements a simple retry strategy by design: if sending an SMS fails and the schedule remains PENDING (or is marked FAILED but you add logic to retry FAILED ones), the next scheduled run of the cron job will attempt to process it again. This relies on subsequent job invocations, not an immediate retry within the same failed invocation. For more sophisticated retries (e.g., exponential backoff for specific errors), a dedicated job queue system (like BullMQ with Redis) would be needed.

Example: Testing Error Scenario

  1. Temporarily put an invalid PLIVO\_AUTH\_ID in your .env.local.
  2. Trigger the cron job manually (see Section 12.2 - Note: Section 12.2 was referenced but not provided in the original text) or wait for it to run.
  3. Check the Vercel logs for the /api/cron function. You should see error messages from the sendPlivoSms function indicating authentication failure. The schedule status in the database should ideally be updated to FAILED.

6. Database schema and data layer

We defined the schema (prisma/schema.prisma) and set up the Prisma client (lib/prisma.ts) in Section 2. The Schedule model and its indices are designed for this use case.

  • Entity Relationship Diagram (ERD): In this simple case, we only have one main entity: Schedule.

(Note: The following diagram uses Mermaid syntax. Ensure your publishing platform supports Mermaid rendering, or replace this with a static image alternative.)

  erDiagram
      SCHEDULE {
          String id PK
          String phoneNumber
          String message
          DateTime sendAt
          String status ""PENDING, SENT, FAILED""
          String plivoMessageIdnullable
          String errornullable
          DateTime createdAt
          DateTime updatedAt
      }
  • Data Access: All database interactions happen via the prisma client instance, providing type safety and abstracting SQL. The primary query pattern is in the cron handler (Section 12.1 - Note: Section 12.1 was referenced but not provided in the original text) where we fetch pending schedules due to be sent.
  • Migrations: We used prisma db push for simplicity. For production evolution, use prisma migrate dev locally to generate SQL migration files and prisma migrate deploy in your deployment pipeline (CI/CD) to apply them reliably.
  • Sample Data: You can use Prisma Studio to manually add test data.
  npx prisma studio

This opens a web UI where you can view and manipulate your database data.

7. Adding security features

  • Input Validation: Zod in app/api/schedule/route.ts provides strict validation of incoming data, preventing malformed requests and basic injection attempts. Sanitize or carefully handle message content if rendering it elsewhere later.
  • Environment Variables: Secrets (PLIVO\_AUTH\_ID, PLIVO\_AUTH\_TOKEN, POSTGRES\_..., CRON\_SECRET) are stored securely in environment variables and not committed to Git (.env.local). Use Vercel's environment variable management for deployment.
  • Cron Job Security: (Note: The original text ended here. Further details on Cron Job Security were likely intended but not provided.)

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 β†’