February 16th, 2025
How to Send Custom SMS Messages from Google Sheets Using Twilio and Google Apps Script
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.
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.
Why Twilio + Google Sheets?
- Scalability: Send text messages to dozens, hundreds, or even thousands of recipients.
- Convenience: Access and update recipient data in Google Sheets, which most people already know how to use.
- Automation: Turn your spreadsheet into a mini dashboard that triggers real-time or on-demand SMS notifications with no extra coding effort.
Prerequisites
- Twilio Account
- Sign up for a free Twilio account if you don’t have one.
- You’ll need your Account SID, Auth Token, and a Twilio phone number to send texts.
- Google Sheet with Recipient Data
- For example, have columns for Name, Phone Number, and Message/Notes.
- Make note of the sheet name (e.g., “Sheet1”) and which columns you’ll reference.
- Basic Knowledge of Apps Script
- Familiarity with accessing spreadsheet data and making HTTP requests in Apps Script.
Step 1: Prepare Your Spreadsheet
- In your Google Sheet, set up a header row with at least these columns:
- Name
- PhoneNumber
- Message
- (Optionally) Status if you want to track whether an SMS was sent successfully.
- Populate the rows below with the data you need to send. For example:
| 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).
Step 2: Open the Apps Script Editor
- In Google Sheets, click on Extensions > Apps Script.
- A new tab opens where you can write and edit your script.
Step 3: Add Your Twilio Credentials
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.
Step 4: Write the Script
Below is a sample function called sendBulkSms()
. It:
- Reads rows in the specified sheet.
- Pulls each row’s phone number and message text.
- Uses UrlFetchApp to make a POST request to the Twilio API.
- Optionally writes the sending status back to the sheet.
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}`);
}
});
}
Key Points in the Code
- Twilio Endpoint: We use the standard Twilio Messaging API endpoint:
https://api.twilio.com/2010-04-01/Accounts/{AccountSID}/Messages.json
. - Basic Auth: Twilio requires Basic Auth using your Account SID and Auth Token. Apps Script’s
Utilities.base64Encode()
helps encode these credentials. - HTTP Status 201: A successful message request returns an HTTP 201 status. If you get anything else, we log an error message in the spreadsheet.
- Custom Message: This example includes the
Name
in the SMS body for a personal touch.
Step 5: Run and Authorize the Script
- Click the Run (play button) in the Apps Script editor.
- A dialog box appears asking for permissions. Approve them so the script can use
UrlFetchApp
. - Check your sheet after it runs. The Status column should say “Sent” for each successful row, or display an error message.
Step 6: (Optional) Add a Menu Button
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.
Step 7: Next Steps & Customizations
- Scheduling: Use a time-driven trigger to automatically run
sendBulkSms()
at specific intervals—e.g., daily or weekly. - Conditional Sending: Modify the code to only send messages for rows where a certain condition is met (e.g., “Status is blank” or “Payment is confirmed”).
- Error Handling & Logging: Add a Logger.log() to record any exceptions in the script’s execution logs.
- Advanced Personalization: Use placeholders in the message (e.g.,
Hello {{Name}}
) and replace them dynamically for more robust templating. - Form Submission Trigger: Combine this approach with a Google Form so that each new form submission automatically triggers an SMS.
Conclusion
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!