CursorTwilioIntermediate8 min read

How to Send SMS Notifications from Your Cursor App

Send text message notifications to users from your Cursor-built app using the Twilio API. Order confirmations, alerts, and verification codes.

Before you start

  • Cursor IDE with a Next.js or Node.js project
  • A Twilio account with a phone number
  • Your Twilio Account SID, Auth Token, and phone number

Step by step

1

Install the Twilio SDK

Add the Twilio Node.js library to your project.

npm install twilio
2

Add your Twilio credentials

Store your Twilio credentials in environment variables.

TWILIO_ACCOUNT_SID=ACxxxxxxxxxxxx
TWILIO_AUTH_TOKEN=your_auth_token
TWILIO_PHONE_NUMBER=+1234567890
3

Create the SMS utility

Build a reusable function for sending SMS messages.

Paste this into Cursor:

Create a lib/sms.ts file that:
1. Imports twilio and creates a client using TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN
2. Exports a sendSMS function that accepts 'to' phone number and 'body' message
3. Sends from TWILIO_PHONE_NUMBER
4. Returns the message SID on success
5. Handles errors and logs them
4

Create an API route for sending SMS

Build an endpoint that triggers SMS notifications.

Paste this into Cursor:

Create an API route at /api/send-sms that:
1. Accepts 'to' and 'message' in the request body
2. Validates the phone number format
3. Calls the sendSMS function
4. Requires authentication (check for logged-in user)
5. Returns the message SID
5

Send SMS on important events

Trigger text messages for order confirmations, alerts, or verification codes.

// Example: Send order confirmation
import { sendSMS } from '@/lib/sms'

await sendSMS({
  to: customer.phone,
  body: `Order #${order.id} confirmed! We'll notify you when it ships.`
})

Common errors

Twilio error 21608: unverified number

On trial accounts, you can only send to verified numbers.

Fix: Verify the recipient's number in Twilio Console → Verified Caller IDs, or upgrade to a paid account.

Invalid phone number format

Twilio requires E.164 format for phone numbers.

Fix: Format numbers as +[country code][number], e.g., +14155551234.

Rate limit exceeded

You're sending too many messages too quickly.

Fix: Add queuing with a delay between messages. Twilio allows 1 message per second per number.

Related guides