How do I export my SMS/Call Logs?

First, let’s make sure you are in the right place. Are you looking for raw call and sms logs? Or, are you actually looking for analytics? If you are looking for metrics, reporting, data analysis, etc., please check out our FAQ on doing analytics on Twilio data.

If you’re looking to do a raw export, you’ve come to the right place.

Exporting fewer than 1000 records

To retrive the last 1000 call records from your logs:

  1. Copy this URL into a text editor:
    https://api.twilio.com/2010-04-01/Accounts/AC12345/Calls.csv?PageSize=1000
  2. ‘AC12345′ is a fake Account SID, so please replace it with your own Account SID
  3. Paste the modified URL from your text editor into the URL bar of your browser and press “Enter”
  4. You’ll be prompted for two fields: “Username” and “Password.” For “Username,” enter your account SID. For “Password,” enter your auth token.

Congratulations, you just made a GET request to the Calls list resource! A CSV should now be downloading.

You can download your SMS logs by repeating the same process but using this URL instead:

https://api.twilio.com/2010-04-01/Accounts/AC12345/SMS/Messages.csv?PageSize=1000

This makes a GET request to the SMS Messages List resource.

Refining this search

The request we just made above asks Twilio to download all calls or messages from your account. But what if you just wanted to look at a subset of these records?

For example, let’s say you want to look only at calls to your Twilio number (781) 333-4444, or all messages sent on June 1st, 2012. Twilio supports filters such as “To”, “From”, “StartTime” (for calls), “DateSent” (for SMS messages), and more. You can append these to your query like this:

https://api.twilio.com/2010-04-01/Accounts/AC12345/SMS/Messages.csv?DateSent=2012-06-01&PageSize=1000

To see all your filtering options for exporting logs, see our Calls list documentation and our SMS/Messages list documentation.

Getting lots of data

If you are going to be downloading lots of data from the Twilio API, we highly suggest that you use one of the official Twilio Helper Libraries to do this.

Here is a simple example of how the PHP Helper Library can be used to download data a CSV of SMS your records. If you find this example timing out, or taking to long, try shortening the time interval of the DateSent filters.

<?php
/**
 * Download the library from: https://github.com/twilio/twilio-php
 * Copy the 'Services' folder into a directory containing this file.
 */
require('Services/Twilio.php');

$account_sid = "ACXXXXXXXXX"; // Your Twilio account sid
$auth_token = "YYYYYYYYYYYY"; // Your Twilio auth token

// Download data from Twilio API
$client = new Services_Twilio($account_sid, $auth_token);
$messages = $client->account->sms_messages->getIterator(0, 50, array(
	'DateSent>' => '2012-09-01',
	'DateSent<' => '2012-09-30',
	//'From' => '+17075551234', // **Optional** filter by 'From'...
	//'To' => '+18085559876', // ...or by 'To'
));

// Browser magic
$filename = $account_sid."_sms.csv";
header("Content-Type: application/csv") ;
header("Content-Disposition: attachment; filename={$filename}");

// Write headers
$fields = array(
	'SMS Message SID', 'From', 'To', 'Date Sent',
	'Status', 'Direction', 'Price', 'Body'
);
echo '"'.implode('","', $fields).'"'."\n";

// Write rows
foreach ($messages as $sms) {
	$row = array(
		$sms->sid, $sms->from, $sms->to, $sms->date_sent,
		$sms->status, $sms->direction, $sms->price, $sms->body
	);
	echo '"'.implode('","', $row).'"'."\n";
}

If you prefer not to use the official Helper Libraries, then you’re going to need to design your application to make use of the built-in nextpageuri attribute which Twilio’s API provides. The nextpageuri is a URL which keeps track of which call or SMS was the last record you fetched and is designed to guide your application through the paging process.

You can use the API Explorer to see examples of the nextpageuri in both XML and JSON.

There isn’t a nextpageuri is CSV format, so if you plan on getting extremely recent records in CSV, you will need to pull the records in either XML or JSON and convert the results to CSV.