How to Check your Twilio Account Balance in JavaScript
Time to read: 2 minutes
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:
Initiate a new Node.js project by running the following command:
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:
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:
On the next line of the file, add the following, again replacing the placeholder with your actual 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:
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:
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:
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).
Related Posts
Related Resources
Twilio Docs
From APIs to SDKs to sample apps
API reference documentation, SDKs, helper libraries, quickstarts, and tutorials for your language and platform.
Resource Center
The latest ebooks, industry reports, and webinars
Learn from customer engagement experts to improve your own communication.
Ahoy
Twilio's developer community hub
Best practices, code samples, and inspiration to build communications and digital engagement experiences.