April 13th, 2025
Don’t Automate Chaos: Why Process Design Must Precede Automation
Don’t let automation magnify your inefficiencies. Learn why optimizing your business processes before automating is the key to unlocking long-term success and ROI.
Automate text messaging with Twilio and Google Apps Script! Learn how to pull data from Google Sheets and send personalized SMS notifications to customers, team members, or event attendees—right from your spreadsheet.
Patrick Flanagan
February 16th, 2025
If your workflow depends on sending text messages—whether it’s to remind users about an event, confirm an order, or simply notify team members—doing this manually can quickly become tedious. Luckily, you can use Google Apps Script and Twilio to automate the entire process. In this tutorial, you’ll learn step-by-step how to integrate your spreadsheet with Twilio to send custom SMS messages directly from Google Sheets.
| Name | PhoneNumber | Message | Status |
|------------ |-------------- |--------------------------- |------- |
| John Doe | +12345678901 | Your order is ready! | |
| Jane Smith | +19876543210 | Don't forget our meeting. | |
3. Note the exact name of the sheet tab (often “Sheet1” by default).
Before coding, keep your Account SID, Auth Token, and Twilio phone number handy. It’s often best to store sensitive data (like Auth Token) using Apps Script’s Properties Service, but for simplicity, we’ll hardcode them in the script here. In a production environment, consider securing them better.
Below is a sample function called sendBulkSms()
. It:
function sendBulkSms() {
// 1. Twilio credentials
const TWILIO_ACCOUNT_SID = 'YOUR_TWILIO_ACCOUNT_SID';
const TWILIO_AUTH_TOKEN = 'YOUR_TWILIO_AUTH_TOKEN';
const TWILIO_PHONE_NUMBER = '+1234567890'; // Your Twilio number
// 2. Sheet details
const SHEET_NAME = 'Sheet1';
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getSheetByName(SHEET_NAME);
// 3. Determine the range of data
const lastRow = sheet.getLastRow();
// Assuming headers are in row 1, data starts in row 2
const dataRange = sheet.getRange(2, 1, lastRow - 1, sheet.getLastColumn());
const dataValues = dataRange.getValues();
// 4. Base64-encoded credentials for Twilio (needed for Basic Auth)
const credentials = Utilities.base64Encode(TWILIO_ACCOUNT_SID + ':' + TWILIO_AUTH_TOKEN);
// 5. Loop through each row
dataValues.forEach((row, index) => {
// Adjust indices based on your columns
// e.g., row = [Name, PhoneNumber, Message, Status]
const name = row[0];
const phoneNumber = row[1];
const message = row[2];
if (!phoneNumber || !message) {
// If phone number or message is missing, skip
return;
}
// 6. Construct Twilio API endpoint
const twilioUrl = 'https://api.twilio.com/2010-04-01/Accounts/' + TWILIO_ACCOUNT_SID + '/Messages.json';
// 7. Payload for Twilio API
const payload = {
To: phoneNumber,
From: TWILIO_PHONE_NUMBER,
Body: `Hello ${name}, ${message}`
};
// 8. Parameters for UrlFetchApp
const options = {
method: 'post',
muteHttpExceptions: true,
payload: payload,
headers: {
Authorization: 'Basic ' + credentials
}
};
// 9. Send the SMS
let response = UrlFetchApp.fetch(twilioUrl, options);
let responseCode = response.getResponseCode();
let responseBody = JSON.parse(response.getContentText());
// 10. Update status in the sheet (column 4 in this example)
const statusColumn = 4; // "Status" is 4th column
const currentRow = index + 2; // Because data starts at row 2
if (responseCode === 201) {
sheet.getRange(currentRow, statusColumn).setValue('Sent');
} else {
sheet.getRange(currentRow, statusColumn).setValue(`Failed: ${responseBody.message}`);
}
});
}
https://api.twilio.com/2010-04-01/Accounts/{AccountSID}/Messages.json
.Utilities.base64Encode()
helps encode these credentials.Name
in the SMS body for a personal touch.UrlFetchApp
.To make sending SMS even more convenient, you can create a custom menu in your Google Sheets toolbar:
function onOpen() {
SpreadsheetApp.getUi()
.createMenu('Twilio SMS')
.addItem('Send Bulk SMS', 'sendBulkSms')
.addToUi();
}
Now, whenever you open this sheet, you’ll see a “Twilio SMS” menu at the top. Clicking Send Bulk SMS calls your function without needing to open the script editor.
sendBulkSms()
at specific intervals—e.g., daily or weekly.Hello {{Name}}
) and replace them dynamically for more robust templating.By integrating Twilio with Google Apps Script, you’ve built a powerful SMS engine right inside Google Sheets. This approach not only saves time but also provides a scalable way to handle your text communication—whether you’re a small business updating clients, a teacher alerting students, or a team leader dispatching operational messages.
With just a few lines of code, you can streamline your workflow and ensure important messages get delivered directly to people’s phones, cutting through email clutter and guaranteeing higher open rates. Ready for more automation ideas? Explore other APIs or set up triggers to make your spreadsheet the ultimate communication hub.
Happy automating—and texting!
About the Author
This tutorial was written by Patrick Flanagan, a Google Apps Script enthusiast who specializes in custom workflow automations, helping businesses harness the full potential of Google Workspace and more. If you have questions or need assistance with your own App Script projects, reach out to learn more!