📌 Getting Detailed Price Summaries about Calls

Overview

This snippet will show how you can utilize the List Calls API to pull detailed call reports within a specific date range! The results will include some helpful summary stats about total calls, inbound vs outbound, total cost, and total duration. We will also export further detail on a per-call record basis to a CSV for record-keeping or extra review.

To quickly run this code, check out our recipe below and replace the variables with your own! Or, keep reading for an in-depth walkthrough.

What do I need to run this code?

For the following code to work, you will need to have pandas, DateTime, and the SignalWire Python SDK installed.

Read about the different ways to install pandas here.

Read about DateTime and how to install using pip here.

Read about the SignalWire Python SDK and how to install it here.

The API also requires that you authenticate yourself using your Project ID, API Token, and Space URL. If you do not know where to find these values, check out our guide here!

Step by Step Code Walkthrough

The first step as always will involve importing the required libraries and authenticating our SignalWire client using the authentication variables mentioned above. We will use datetime and pandas to enhance our results in this snippet. We will also use the List Calls API with date range filters to narrow down our results to a particular month, although you can narrow down your results by many other factors too!

from datetime import datetime
from signalwire.rest import Client as signalwire_client
import pandas as pd

client = signalwire_client("ProjectID", "AuthToken", signalwire_space_url='EXAMPLE.signalwire.com')
calls = client.calls.list(start_time_after=datetime(2022, 4, 1), start_time_before=datetime(2022, 4, 30))

In the next section, we will create two empty lists to store our overall call data for exporting as well as directional data for some summary statistics. We will loop through each call record in calls and append the direction to direction and all the other data to d. To finish up this section, we will store the number of total inbound calls and total outbound calls in their own variables to be used later.

d = []
direction = []

for record in calls:
    d.append((record.from_, record.to, record.start_time, record.sid, record.price, record.direction, record.duration))
    direction.append(record.direction)

total_inbound=int(direction.count("inbound"))
total_outbound=int(direction.count("outbound"))

Next, we will create a Pandas dataframe with column headers and add the data stored in d to the dataframe. This will let us export to CSV for easier record keeping and review!

df = pd.DataFrame(d, columns=('From', 'To', 'Date', 'Call SID',  'Price', 'Call Direction', 'Call Duration (s)'))
print(df)
df.to_csv('MarchCalls.csv', index=False, encoding='utf-8')

In this last section, we will use the data above to create some quick summary stats to provide an overview of how the call traffic looked during this particular date range. Pandas provides an easy way to sum the total duration and cost columns while Python reformats the results to look more readable!

totalCalls = len(df)
totalCost = df['Price'].sum()
formattedCost = "${:,.2f}".format(totalCost)
totalDuration = round((df['Call Duration (s)'].sum())/60, 2)

print("You had " + str(totalCalls) + " total calls during your selected date range.")
print("There were " + str(total_inbound) + " inbound calls and " + str(total_outbound) + " outbound calls.")
print("The total cost of calls in your selected date range is approximately " + formattedCost + " USD.")
print("there was a total duration of " + str(totalDuration) + " minutes.")

Wrap up

This easy snippet shows how you can utilize SignalWire's APIs with some additional libraries to get even better reports built on your call traffic. Keep reading other voice snippets to find out how you can utilize our APIs in creative ways!

Sign Up Here

If you would like to test this example out, you can create a SignalWire account and space here.

Please feel free to reach out to us on our Community Slack or create a Support ticket if you need guidance!


Did this page help you?