How to Check your Twilio Account Balance in Python

January 14, 2021
Written by
Reviewed by

How to Check your Twilio Account Balance in Python

Twilio APIs make it easy 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 retrieve your account balance using Python, which will allow you to build automated account balance monitoring within your application!

Prerequisites

To follow this tutorial you will need:

  • Python 3.6 or newer. If your operating system does not provide a Python interpreter, you can go to python.org to download an installer.
  • A free or paid Twilio account. If you are new to Twilio get your free account now! This link will give you $10 when you upgrade.

Creating a Python Project

Let’s begin by creating the directory where you will store this project. Open a terminal window, find a suitable parent directory, and enter the following commands:

$ mkdir twilio-balance
$ cd twilio-balance

Following best practices, you are going to create a Python virtual environment where the Python dependencies will be installed.

If you are using a Unix or MacOS system, open a terminal and enter the following commands to complete the tasks described above:

$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install twilio

For those of you following the tutorial on Windows, enter the following commands in a command prompt window:

$ python -m venv venv
$ venv\Scripts\activate
(venv) $ pip install twilio

The pip command installs the Twilio Python Helper library which is used to access Twilio APIs.

Configuring the Twilio Client

If you already have an application built with the Twilio Python Helper Library, you know that the client uses your Twilio Account SID and Auth Token to authenticate, and likely have this configured. If you are building a new application as part of this tutorial, these need to be configured.

Log into your Twilio Console and you will see your “Account SID” and “Auth Token” values assigned to your account in the dashboard:

Twilio account SID and auth token

Back in your terminal window, set two environment variables with the names TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN to these two values. Use the “Copy” buttons on the right to transfer the values easily and without making any mistakes. If you are using a Mac or Unix computer, do it as follows:

(venv) $ export TWILIO_ACCOUNT_SID=enter-your-account-sid-here
(venv) $ export TWILIO_AUTH_TOKEN=enter-your-auth-token-here

If you are doing this on a Windows computer, you can set your variables as follows:

(venv) $ set TWILIO_ACCOUNT_SID=enter-your-account-sid-here
(venv) $ set TWILIO_AUTH_TOKEN=enter-your-auth-token-here

Retrieving your account balance

Okay, now you are ready to retrieve your Twilio account balance using Python! To begin, you can do this in a Python console. Start it by typing python on the same terminal session in which you set the environment variables:

(venv) $ python
Python 3.8.6 (default, Oct 12 2020, 12:50:15)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>

Then import and initialize the Twilio client:

>>> from twilio.rest import Client
>>> client = Client()

Note that the Client() class expects the TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables. If you forget to set these, the client will not initialize and instead show an error message.

The client instance can get your account balance as follows:

>>> client.api.v2010.balance.fetch().balance
'13.55363'

Note that the amount is returned as a string. It would be more useful to have it as a number, so the result can be converted right after it is retrieved:

>>> float(client.api.v2010.balance.fetch().balance)
13.55363

You probably know what currency is used in your account, but if you need to retrieve that as well, you can do it as follows:

>>> client.api.v2010.balance.fetch().currency
'USD'

Let’s now integrate this into a simple script that can be executed directly from the command line. Open your favorite text editor and create a file called twilio_balance.py inside the twilio-balance directory. Enter the following code in this file:

from twilio.rest import Client

client = Client()
balance_data = client.api.v2010.balance.fetch()
balance = float(balance_data.balance)
currency = balance_data.currency

print(f'Your account has {balance:.2f}{currency} left.')

Save the file, and then run it from the same terminal you used above, which has the Twilio credentials stored as environment variables:

(venv) $ python twilio_balance.py
Your account has 13.55 USD left.

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. You can even send yourself a notification if the balance is lower than a specific amount.

I hope this improves your experience using Twilio APIs. I can’t wait to see what you build!

Miguel Grinberg is a Python Developer for Technical Content at Twilio. Reach out to him at mgrinberg [at] twilio [dot] com if you have a cool Python project you’d like to share on this blog!