Check Twilio Balance using Apps Script

Search for a command to run...

No comments yet. Be the first to comment.
Bun, the hyper-fast JavaScript runtime known for its incredible speed and Zig-based architecture, is being completely rewritten in Rust. For a project that has over 22 million monthly downloads and ea

Next.js 16.3 is almost here, and it's packed with a ton of improvements and let's see them in this post 1.Instant Navigation For a while now, there’s been a valid, lingering debateServer Components vs

It’s official. React has merged its long-awaited Rust port of the React Compiler (formerly known as React Forget) into the main repository. What started as an experimental research project by Joseph S

If you follow modern technology trends, you have likely been led to believe that artificial intelligence is entirely built on Python. Every tutorial, machine learning framework documentation, and open

Just when the JavaScript ecosystem was catching its breath after last week’s TanStack Router compromise, the Mini Shai-Hulud supply chain worm has continued its relentless march. Developed by the thre

In this post, we will learn how to check the Twilio Balance using Apps Script and send an email to the user when the threshold level meets.
But Twilio already sends an email when the balance reaches $5 then why do we need this script?
If that was the question that runs through your mind means think of this scenario what would you do if your client asks you to send an email when Twilio balance reaches $10 instead of $5. You cannot check every day then what you can do?
Automate it by writing a script using Apps Script
What is Apps Script
Create a Twilio account (If you are new)
Enable Apps Script
Logic to fetch the current balance from Twilio
Logic to send an email when threshold value matches
Trigger functions to check Twilio balance daily

Overview of what we are going to do — Source: Nidhinkumar Inspired from Priyanka Vergadia
Google Apps Script is a rapid application development platform that makes it fast and easy to create business applications that integrate with Google Workspace. You can write code in modern JavaScript and have access to built-in libraries such as Gmail, Calendar, Drive, etc.,

Source: Nidhinkumar
Go to the Twilio page and do the signup if you are a new user else login with the credentials available.
If you have an existing project means you can skip this step else create a new project and click Programmable SMS and click Whatsapp now you could see a page like below

Twilio Sandbox
Now copy that phone number and create a contact in your phone and then open Whatsapp and select that contact and type the code join bush-period (add the code which you see on your Twilio Sandbox :)
Once the test connection is completed click the Home icon where you could see the ACCOUNT SID and AUTH TOKEN copy both the credentials and keep it handy we will use it later

Account SID and Auth Token
Open Google Sheets and click on Tools -> Script Editor in Google Sheets like below

Script Editor
Once you click the Script Editor you will see a window like below.

Script Editor
Here we will write the logic to get the current balance as well as sending an email once the threshold value reaches.
To check the Twilio balance using the REST API. we can use the curl command like below
`curl -G https://api.twilio.com/2010-04-01/Accounts/your_account_sid/Balance.json \
-u "your_account_sid:your_auth_token"`
It would be something like this when you add your Account SID and Auth Token
curl -G https://api.twilio.com/2010-04-01/Accounts/AC123abc45678901c3ff481ea2/Balance.json \
-u "AC123abc45678901c3ff481ea2:baa9fe5ad79a7a97dxxx00c012a3d4ce"
Once you run the command in your terminal you could see the current balance of your Twilio account without opening the Twilio console.
In the above curl command, we have authenticated using our Account SID and Auth Token
Now we will implement the same in Apps Script for that first, rename the function to checkTwilioBalance
function checkTwilioBalance() {}
Once the function is renamed we will make the API call from Apps Script to Twilio using UrlFetchApp
What we have done in the above code we have used the UrlFetchApp which will fetch the data from the URL which we define.
Since the Twilio API contains authentication so we have used headers in which we have passed our Account SID and Auth Token
Now if you save the function and click the Run button you would be asked for review permission like below

Review Permissions
Click on Review Permissions and then click Allow for the Gmail account connected with it. Once you gave the necessary permissions your function will be run.
To see the logs click on View-> Logs.

View Logs
Click Logs you could see the response which we receive from Twilio like below

Twilio Balance
Now we are able to get the response from Twilio to Apps Script.
Now we will write the logic to send an email when the balance is less than $7 or whatever the limit you like
What we have done in the above code is we check if the current Twilio balance is less than 7 if so then we will call the sendEmail with subject and body
Now we will write the logic to sendEmail to the clients like below
Once done you are ready to test the script. Click on the Start(Play) icon to run the script and make sure the function is checkTwilioBalance is selected like below

Running the script
Now you will be asked for permission to allow Apps Script to use Gmail for sending an email like below

Permission Review


Account Review and Allowing to access Gmail

Email Send From AppScript when Balance is Low
Now we got the email triggered when the balance is low by clicking the Run script. But every time we cannot run the application manually right. so, what we do?
Don’t worry Apps Script provides an option to run the script automatically by setting the triggers
Click Edit -> Current Project’s Trigger which will open a new tab click Add Trigger which will open a pop-up modal like below

Trigger

Trigger
Click Save now it will automatically check the Twilio balance every two hours (you can change it to your wish) and if the threshold meets then it will trigger an email.
You have learned how to check the Twilio balance and send Email using Apps Script
Happy Learning :)