Retrieve call logs
Learn to retrieve call logs with Twilio Programmable Voice by sending a GET request to the Calls.json endpoint or fetching a specific call resource by SID. You can use this guide to monitor calls, view call metrics, inspect call history, and troubleshoot call issues.
See Related reference documentation to learn more about the Call resource used in this guide.
Info
You can see all incoming and outgoing calls, as well as message activity, by viewing a specific phone number in Twilio Console. In Twilio Console, go to Products & services > Numbers & senders, and select a phone number. In the legacy Console, open a phone number and choose the Calls Log or Messages Log tab.
- Create a Twilio account.
- In the Twilio Console, create a compliance profile, then buy a voice-enabled phone number.
- In the legacy Console, buy a voice-enabled phone number. Only Messaging-enabled phone numbers need a compliance profile.
Select your programming language and complete the prerequisites:
- Install Python 3.3 or later.
- Install Flask and the Twilio Python Helper Library. Using pip, run the following command:
pip install flask twilio
- Install and set up ngrok.
Extract your credentials from your Twilio account then save these credentials as environment variables.
-
Go to the Twilio Console. The Let's get building page appears.
-
Click API keys and Auth tokens. The API keys & auth tokens page appears with the Auth Tokens tab selected.
-
Scroll to your Account SID.
-
Click the copy button next to your Account SID.
-
Run the following command, replacing
YOUR_ACCOUNT_SIDwith your Account SID.macOS TerminalWindows command linePowerShellexport TWILIO_ACCOUNT_SID=YOUR_ACCOUNT_SIDThis command creates an environment variable on your development system for your account SID.
-
To display the Auth Token, click the eye button in the Primary auth token box.
-
Highlight and copy the Auth Token.
-
Run the following command, replacing
YOUR_AUTH_TOKENwith your Authentication Token.macOS TerminalWindows command linePowerShellexport TWILIO_AUTH_TOKEN=YOUR_AUTH_TOKENThis command creates an environment variable on your development system for your auth token.
You can use Twilio's REST API to retrieve logs about the phone calls to and from your Twilio account. If you just want to check a couple logs, however, you should try looking at the voice logs in your Twilio console first.
To list all phone calls for your Twilio account, just call @client.calls.list().
Python only requires the Twilio Python helper library as noted in the prerequisites.
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011calls = client.calls.list(limit=20)1213for record in calls:14print(record.end)
You can also filter the list results. This example only returns phone calls to the phone number +15558675309 which had a call status of busy but you can filter on other call properties as well.
Python only requires the Twilio Python helper library as noted in the prerequisites.
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011calls = client.calls.list(to="+15558675310", status="busy", limit=20)1213for record in calls:14print(record.end)
To retrieve data about a specific call, you can get that CallSid directly.
Python only requires the Twilio Python helper library as noted in the prerequisites.
1# Download the helper library from https://www.twilio.com/docs/python/install2import os3from twilio.rest import Client45# Find your Account SID and Auth Token at twilio.com/console6# and set the environment variables. See http://twil.io/secure7account_sid = os.environ["TWILIO_ACCOUNT_SID"]8auth_token = os.environ["TWILIO_AUTH_TOKEN"]9client = Client(account_sid, auth_token)1011call = client.calls("CA42ed11f93dc08b952027ffbc406d0868").fetch()1213print(call.to)
To use the webhooks in this code sample, Twilio must communicate with your web app. These communications consist of HTTP requests sent between URLs accessible over the internet. Twilio requires access to Internet-addressable, or public, URLs.
Protect your webhooks
Twilio supports HTTP Basic and Digest Authentication. This type of authentication protects your TwiML URLs on your web server with an Auth Token. This limits access to the URLs to you and Twilio. Protect your auth tokens as you would your passwords. To learn more, see HTTP authentication.
Most production systems have a public URL, but development systems might not. To permit Twilio to access your local dev app, use ngrok. To create a public URL for your app, run ngrok at the command line:
ngrok http 3000 --url https://<your-app-name>.ngrok.dev
This command creates a public URL https://<your-app-name>.ngrok.dev. that forwards HTTP requests to your web app listening on localhost:3000.
With your public URL created, configure its connection to Twilio using a webhook.
After following this guide, you can retrieve single or filtered call logs using Twilio Programmable Voice in your application.
Explore the following guides to build on what you've learned in this guide:
- Call tracking: Track and attribute phone calls to specific marketing campaigns or channels.
- Voice Insights overview: Analyze call quality and performance metrics in real time.