I built a system for mass SMS sending using the Twilio Messaging API

I built a system for mass SMS sending using the Twilio Messaging API

I built a system for mass SMS distribution using the Twilio Messaging API
2025.09.05

Hello, I'm Subaru.
This time I built a system for bulk SMS sending using Twilio Messaging API.

Introduction

Twilio is a cloud-based API platform for integrating communication features like phone, SMS, and chat. By using Twilio's API, you can build and customize communication solutions freely.
This time, I'd like to try sending bulk SMS messages using Twilio Messaging API.

Preparing the Twilio Console

Setting Up a Phone Number

We need to prepare a phone number for sending SMS. Navigate to Phone Number > Manage in the Twilio console. If you already have a number, use it from Active numbers. If you don't have one yet, purchase one from Buy a number.

Console's Active number

Console Verification

Before creating the script, check and use the following items. You can verify your account SID and token from the Twilio console dashboard, and use the phone number you purchased in the console. (Phone Numbers > Manage > Active numbers)

  • Account SID
  • Account token
  • Purchased phone number

Environment Setup

Creating the Script

Create a project folder and install the Twilio package. We'll be working with a Node.js environment for this project.

			
			npm install twilio csv-parse @types/node typescript ts-node

		

Creating the .env File

Since we're using environment variables for accountSid, authToken, and fromNumber, we'll add the actual values to the .env file.

			
			TWILIO_ACCOUNT_SID=AccountSID
TWILIO_AUTH_TOKEN=AuthToken
TWILIO_FROM_NUMBER=Purchased phone number

		

Implementation### Creating ts file

Next, let's create the main script for execution.
Save the following code as sms-sender.ts.

			
			import fs from 'fs';
import { parse } from 'csv-parse';
import twilio from 'twilio';
import dotenv from 'dotenv';

dotenv.config();

const accountSid = process.env.TWILIO_ACCOUNT_SID!;
const authToken = process.env.TWILIO_AUTH_TOKEN!;
const client = twilio(accountSid, authToken);

const fromNumber = process.env.TWILIO_FROM_NUMBER!;
const messageBody = 'Hello, this is a bulk sending test!!!';

// Load recipient list from CSV file
const sendMessages = async () => {
    const parser = fs.createReadStream('recipients.csv').pipe(parse({ columns: true, trim: true }));

    for await (const row of parser) {
        const to = row.phone;
        try {
            const message = await client.messages.create({
                body: messageBody,
                from: fromNumber,
                to: to,
            });
            console.log(`Sent to ${to}: ${message.sid}`);
        } catch (error) {
            console.error(`Failed to send to ${to}:`, error);
        }
    }

    console.log('All messages processed.');
};

sendMessages().catch(console.error);

		

Edit the messageBody to customize the actual message to be sent.

Creating csv file

Next, for the bulk sending destinations, create a recipients.csv file and list the destination phone numbers one per line (in E.164 format).

			
			phone
+81xxxxxxxxxx
+81xxxxxxxxxx

		

With this, the setup is complete, so let's check the actual operation.

Verification

Execute the bulk sending with the following command:

			
			npx ts-node sms-sender.ts

		

As you can see below, we confirmed that the SMS was actually sent.

IMG_8877

Summary

In this article, we built a bulk SMS sending system using the Twilio Messaging API.
I hope this blog has been helpful to you.

Announcement

We host Twilio/SendGrid seminars every month

Classmethod hosts Twilio/SendGrid seminars every month.

Classmethod holds monthly Twilio/SendGrid seminars. Each seminar explains the basics for those who are not familiar with Twilio and SendGrid, so if you are considering implementing Twilio/SendGrid in the future or have already implemented it and want to revisit the basics, please feel free to join us.

Share this article

FacebookHatena blogX

Related articles