Twilio offers a Usage Record API so you can better measure how much you’ve been using Twilio. Here are some examples of how you can use PHP to access the usage API.
$client = new Services_Twilio('AC123', '456bef');
foreach ($client->account->usage_records as $record) {
echo "Record: $record";
}
UsageRecords support several convenience subresources that can be accessed as properties on the record object.
$client = new Services_Twilio('AC123', '456bef');
foreach ($client->account->usage_records->last_month as $record) {
echo "Record: $record";
}
By default, Twilio will return your all-time usage for a given usage category.
$client = new Services_Twilio('AC123', '456bef');
$callRecord = $client->account->usage_records->getCategory('calls');
echo $callRecord->usage;
You can filter your UsageRecord list by providing StartDate and EndDate parameters.
$client = new Services_Twilio('AC123', '456bef');
foreach ($client->account->usage_records->getIterator(0, 50, array(
'StartDate' => '2012-08-01',
'EndDate' => '2012-08-31',
)) as $record) {
echo $record->description . "\n";
echo $record->usage . "\n";
}
You can use the today record subresource, and then retrieve the record directly with the getCategory function.
$client = new Services_Twilio('AC123', '456bef');
// You can substitute 'yesterday', 'all_time' for 'today' below
$smsRecord = $client->account->usage_records->today->getCategory('sms');
echo $smsRecord->usage;
The code below will retrieve daily summaries of recordings usage for August 2012. To retrieve all categories of usage, remove the ‘Category’ filter from the getIterator array.
$client = new Services_Twilio('AC123', '456bef');
foreach ($client->account->usage_records->daily->getIterator(0, 50, array(
'StartDate' => '2012-08-01',
'EndDate' => '2012-08-31',
'Category' => 'recordings',
)) as $record) {
echo $record->usage;
}