How to View Your Twilio Account Usage Using Go
Time to read: 4 minutes
In this tutorial, you're going to learn how to retrieve those details using Go and Twilio's Go Helper Library, along with a few other classes to make the whole process easier and simpler.
If you’re not familiar with the REST API, it supports retrieving actions made by your Twilio account during any time period and by any usage category. So if you want to create some tooling to perform reporting or analytics on your account(s), then this is the API to use.
Prerequisites
- An IDE or text editor of your choice, such as VIM, Visual Studio Code, or Goland
- A Twilio account (free or paid). If you are new to Twilio click here to create a free account
- Go (1.18 or above) along with some prior knowledge and experience
Set up the project directory structure
The first thing you need to do is create a project directory, then initialise a new Go project, and then change into the new directory. Do that by running the following commands, wherever you want to store the project directory.
Add the required dependencies
With the project directory in place, the next thing to do is add the required dependencies, of which there are 4:
- Twilio's Go Helper Library: This package simplifies interacting with Twilio’s many APIs in Go.
- Go Dotenv: This package simplifies setting environment variables.
- Go Pretty: This package simplifies creating command line application output.
- Bojanz's Currency: This package simplifies formatting currency data.
To install them, run the commands below.
Retrieve your Twilio credentials
The next thing to do is to make your Twilio credentials (your Twilio Account SID and Auth Token) available to the application.
To do that, first, create a new file named .env in the top-level directory of the project. Then, in that file paste the code below. It adds environment variables and a placeholder for both which will be replaced with the real values shortly.
Next, from the Twilio Console's Dashboard, copy your Account SID and Auth Token and, in .env, replace the respective placeholder values with them (<TWILIO_ACCOUNT_SID>
and <TWILIO_AUTH_TOKEN>
).
Create a Go class to retrieve your account usage records
With the project setup completed, it’s now time to write the application’s core code. To do that, create a new file named twilio-usage-records.go in the project’s top-level directory and in it, paste the code below.
The code, as always, starts off by importing all of the classes that it needs, before defining the main
function.
That function defines the parameters to send to the usage records request in a new ListUsageRecordParams
struct. The struct supports seven parameters, which you can see below. However, only the maximum number of records to retrieve will be set in the first example.
Category
: If set, the records are filtered to those that fit into a specific category.EndDate
: If set, only records that occurred on or before this date are includedIncludeSubaccounts
: Whether to include records from the master account and all its subaccountsLimit
: The maximum number of records to returnPageSize
: How many records to return in each page of records. The default is 50 and the maximum is 1000PathAccountSid
: The SID of the Account that created the recordsStartDate
: If set, only records that occurred on or after this date are included
After that, it loads the environment variables set in .env. If not successful, then an error is printed to the terminal. Otherwise, it attempts to retrieve usage records from the user’s account.
If the records were not able to be retrieved, an error is printed to the terminal. Otherwise, the records are iterated over, and the start date, end date, category, and pricing information (price and price unit) are extracted. The start and end dates, along with the pricing information, are formatted to make them just that much easier to read.
The extracted information is added to a go-pretty table so that, when printed, the information is rendered in a consistent and coherent way. After all of the details have been added to the table, the table is printed to the terminal.
Test that the code works
Now, it’s time to test that the code works. To do that, run the command below in the project’s top-level directory.
All being well, you should see output similar to the example below.
Filter records by predefined date ranges
Now, let’s build on the first example by filtering the returned usage records by date. The Twilio Go Helper Library makes this reasonably simple because of a series of helper structs, including ListUsageRecordToday
, ListUsageRecordThisMonth
, and ListUsageRecordLastMonth
. These helper structs filter records to only those that occurred today, this month, and last month, respectively.
Let's filter the returned records from the previous month. To do that, change params
to be defined as follows.
Then, change usageRecords
, to be initialised as follows:
If you run it, you should see terminal output, similar to the example below.
Filter records by start date, end date, and category
Finally, let’s filter records by a broader date range as well as by category. There are nine categories available. These are:
Category | Description |
---|---|
calls | Inbound and outbound voice calls. This does not include SIP or client calls. |
sms | SMS messages |
pfax-minutes | Programmable fax minutes |
pfax-pages | Programmable fax pages |
phonenumbers | Phone numbers owned by the account |
recordings | Recordings of voice calls |
transcriptions | Transcriptions of voice calls |
pv | All Programmable Video usage |
totalprice | This is the total price of all usage. |
Let's filter the usage records to SMS records sent and received in the last quarter. To do that, add the following code block before the definition of params
:
Then, update the definition of params
to match the following code:
Finally, add "time"
to the imports list at the top of the file, if your text editor or IDE hasn't already done it for you.
Now, run the code again. You should see terminal output similar to the example below.
That’s how to view your Twilio account usage using Go
While there is a lot more on offer than I've covered in this tutorial, you now know the essentials.
Do you prefer to use PHP? Then check out this tutorial by Marcus Battle.
Matthew Setter is a PHP Editor in the Twilio Voices team and a PHP and Go developer. He’s also the author of Mezzio Essentials and Docker Essentials. When he's not writing PHP code, he's editing great PHP articles here at Twilio. You can find him at msetter@twilio.com, and on Twitter, and GitHub.
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.