How to Check your Twilio Account Balance in PHP

February 05, 2025
Written by
Reviewed by

How to Check your Twilio Account Balance in PHP

Through a large array of APIs, Twilio makes it easy to add a broad array of communication functionality to your applications, such as SMS, MMS, and email. But, to use that functionality, your account balance must be in credit.

You can check your account balance at any time via the Twilio Console. After logging in to the Twilio Console, click Admin in the top right-hand corner, then click Account Admin > Account Billing.

Screenshot showing the admin dropdown with Account billing highlighted in the menu.

While this is good for checking occasionally, doing this each time is both manual and quite inconvenient. Gladly, you can keep track of your account balance via an API call.

In this short tutorial, you'll learn how to do so in PHP so that you can add automated account balance monitoring to your applications.

Prerequisites

To follow along with this tutorial you'll need the following:

Set up the project

Let's begin by creating a project directory and changing into it by running the commands below in your terminal:

mkdir check-twilio-account-balance
cd check-twilio-account-balance

Then we'll add the third-party packages that we'll need; these are:

To add them, run the command below.

composer require twilio/sdk vlucas/phpdotenv

Write the PHP code

Now, it's time to write PHP code. Open your preferred IDE or text editor, create a new PHP file named index.php, and in it paste the code below:

<?php

declare(strict_types=1);

use Twilio\Rest\Client;

require_once "vendor/autoload.php";

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required([
    'TWILIO_ACCOUNT_SID',
    'TWILIO_AUTH_TOKEN',
]);

$client = new Client($_ENV['TWILIO_ACCOUNT_SID'], $_ENV['TWILIO_AUTH_TOKEN']);
$balance = $client->getAccount()->balance->fetch()->balance;

$numberFormatter = new NumberFormatter("en_US", NumberFormatter::CURRENCY);
$numberFormatter->setAttribute(NumberFormatter::FRACTION_DIGITS, 3);
print $numberFormatter->formatCurrency((float)$balance, "USD");

The code starts by using PHP dotenv to load the environment variables in a file named .env (which we'll set up shortly) into PHP's $_ENV and $_SERVER superglobals. If TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN are not defined and set, it will throw an exception.

Then, it initialises a new Twilio\Rest\Client class with your Twilio Account SID and Auth Token. This object is the central class in the PHP Helper Library that coordinates requests to Twilio's APIs.

After that, it retrieves the account's balance (internally making a call to the account's Balance resource) and formats it using the NumberFormatter class, rounding the amount to three decimal places for better readability.

You could use the Money package instead, if you prefer. However, NumberFormatter does everything in fewer lines of code.

Retrieve your Twilio credentials

The final thing to do is to retrieve your Account SID and Auth Token from your Twilio account and store them for the code to load as environment variables. To do that, create a new file named .env in the top-level directory of the project. Then, in that file paste the following code:

TWILIO_ACCOUNT_SID=<TWILIO_ACCOUNT_SID>
TWILIO_AUTH_TOKEN=<TWILIO_AUTH_TOKEN>

Now, log in to the Twilio Console and, in the Account Info panel, copy your Account SID and Auth Token. Paste the two values into .env in place of <TWILIO_ACCOUNT_SID> and <TWILIO_AUTH_TOKEN> respectively.

Twilio account info with Account SID, Auth Token, and Twilio phone number details shown.

Run the code and retrieve your account balance

With everything done, you're ready to retrieve your Twilio account balance. To do that, run the following command:

php index.php

You should see output similar to the below:

Your current Twilio account balance is $53.245.

That's how to retrieve our Twilio account balance with PHP

Thanks to Twilio's Helper Library for PHP, there's very little that you have to do, making it trivial to integrate automated account balance checking into your application. If you were so inclined, you could refactor it to notify you via SMS if the balance falls below a defined amount.

I hope this improves your experience integrating with Twilio.

Matthew Setter is a PHP and Go Editor in the Twilio Voices team. He’s also the author of Mezzio Essentials and Deploy with Docker Compose. You can find him at msetter@twilio.com and on GitHub.