Thursday, 25 March, 2021 UTC


Summary

Twilio APIs make it possible to add communication features to your application, but to consume these APIs you have to keep track of your spending and ensure your account balance stays above zero.
Your balance is available in the main page when you log in to your Twilio Console, but having to actively monitor your account is tedious and inconvenient.
In this tutorial you are going to learn how to write a script that retrieves your account balance using JavaScript. You can also schedule this script to run on a regular cadence, and even set up SMS based notifications if you drop below a certain balance.
Prerequisites
To get started with this tutorial, you’ll need the following:
  • Node.js installed on your machine
  • A free Twilio account (sign up with this link and get $10 in free credit when you upgrade your account)
Create a Node.js project
From your terminal, create a new folder called twilio-balance and change working directories into it:
mkdir twilio-balance cd twilio-balance 
Initiate a new Node.js project by running the following command:
npm init -y 
This command will create a package.json file inside twilio-balance.
With your Node.js project setup, you can now install the two dependencies required for this app: the Twilio Node Helper Library and dotenv, the package used to load environment variables:
npm install twilio dotenv 
All that remains for project setup is to create a new file for your script called index.js, and a .env file to hold your Twilio account credentials.
Configure your environment variables
The Twilio Node Helper Library uses your Twilio Account SID and Auth Token to authenticate. You’ll need to retrieve these credentials from your Twilio Console.
Once you’ve found your credentials, open the file you created called .env in your favorite text editor. On the first line of the file, add the following, taking care to replace the placeholder with your actual Account SID:
ACCOUNT_SID=<YOUR_ACCOUNT_SID> 
On the next line of the file, add the following, again replacing the placeholder with your actual Auth Token:
AUTH_TOKEN=<YOUR_AUTH_TOKEN> 
You can now save and close this file, you won’t use it again.
Retrieve your account balance
Now you are ready to retrieve your Twilio account balance using JavaScript!
Open the file called index.js inside the twilio-balance directory. Enter the following code in this file:
require('dotenv').config(); const client = require('twilio')(process.env.ACCOUNT_SID, process.env.AUTH_TOKEN) client.balance.fetch() .then((data) => { const balance = Math.round(data.balance * 100) / 100; const currency = data.currency; console.log(`Your account balance is ${balance} ${currency}.`) }); 
This code first loads your environment variables, and then initializes a new Twilio client using the Node Helper Library.
It then uses the client to fetch your account balance data, which will be returned in the form of an object. As an example, here’s the data returned from my script:
{ accountSid: 'XXXXXXXXXXXXXXXXX', balance: '125.80656', currency: 'USD' } 
Using the values in this object, the code will then round the account balance to two decimal places and log a message to the console with the balance and currency information.
That’s it! Save and close the file.
Run your script
Head back to your terminal and run your script:
node index.js 
In a moment you will see a message logged to your console that says “Your account balance is <your balance> <your currency>.”
Conclusion
And that is it! You can now incorporate this little bit of logic into a larger application to check your balance as often as you need. Or, you could schedule the script and send an SMS notification to yourself with the balance.
I hope this improves your experience using Twilio APIs. I can’t wait to see what you build!
Ashley is a JavaScript Editor for the Twilio blog. To work with her and bring your technical stories to Twilio, find her at @ahl389 on Twitter. If you can’t find her there, she’s probably on a patio somewhere having a cup of coffee (or glass of wine, depending on the time).