Exporting Room Session Data
This code snippet shows how you can easily use the List Room Sessions endpoint of SignalWire's Video API to pull all of the activity sessions within each room.
Full code example: List Video Room Session Activity
import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
SpaceURL = 'example.signalwire.com'
projectID = ""
authToken = ""
url = f"https://{SpaceURL}/api/video/room_sessions?page_size=1000"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload, auth=HTTPBasicAuth(projectID, authToken)).json()
roomSessions = response['data']
while "next" in response['links'].keys():
response = requests.get(response['links']['next'], auth=HTTPBasicAuth(projectID, authToken)).json()
roomSessions.extend(response['data'])
d = []
for session in roomSessions:
d.append((session['id'], session['name'], session['start_time'], session['end_time'], session['duration'], session['cost_in_dollars']))
df = pd.DataFrame(d, columns=('Room ID', 'Name', 'Start Time', 'End Time', 'Duration (seconds)', 'Cost in Dollars'))
print(df.to_string())
print(f"There were {len(df)} total room sessions with a total duration of {round((df['Duration (seconds)'].sum())/60, 2)} minutes and a total cost of {'${:,.2f}'.format(df['Cost in Dollars'].sum())} dollars.")
df.to_csv('RoomSessionData.csv', index=False, encoding='utf-8')
Check out our handy Try It feature in our docs by replacing the variables here with your own and running it in the browser. You can then easily copy the generated code in cURL or your favorite language!
What do I need to run this code?
Required Libraries:
- pandas
- requests
- HTTPBasicAuth (which is technically a branch off of the requests library above)
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!
List Room Sessions Code Walkthrough
Despite how quickly this code can pull all your video room session data and format it in a readable way for you, it's quite simple! We will start by importing the required libraries and defining our authentication variables.
import pandas as pd
import requests
from requests.auth import HTTPBasicAuth
# assign auth variables
SpaceURL = 'example.signalwire.com'
projectID = ""
authToken = ""
Next, we will create a request to the room sessions endpoint using the Python requests
library using HTTPBasicAuth for authentication, storing the data array of the JSON response in roomSessions
.
# define URL, payload, and headers for API Endpoint
url = f"https://{SpaceURL}/api/video/room_sessions?page_size=1000"
payload={}
headers = {}
response = requests.request("GET", url, headers=headers, data=payload, auth=HTTPBasicAuth(projectID, authToken)).json()
roomSessions = response['data']
However, as shown with the page_size
in the URL defined above, this will only return 1000 session objects. It's important to make sure you add pagination in whatever language you choose to use to make sure that you end up with only a subset of the full data.
while "next" in response['links'].keys():
response = requests.get(response['links']['next'], auth=HTTPBasicAuth(projectID, authToken)).json()
roomSessions.extend(response['data'])
Next, we will loop through each room session object and store the data that we want to export in an array. In this example, we track session ID, room name, start_time, end_time, duration, and cost. However, you could access any of the objects returned in the data array of the API response. To see all of your options, go to the list room sessions endpoint and click the 200 OK under Response. This will populate a full list of all the returned data!
# Sets up an empty array
d = []
# loop through sessions
for session in roomSessions:
d.append((session['id'], session['name'], session['start_time'], session['end_time'], session['duration'], session['cost_in_dollars']))
Lastly, we will use Pandas to create a dataframe with the array above and export to CSV. We will also print some helpful summary stats that tell us the total number of room sessions, total duration of minutes, and total cost in dollars.
df = pd.DataFrame(d, columns=('Room ID', 'Name', 'Start Time', 'End Time', 'Duration (seconds)', 'Cost in Dollars'))
print(df.to_string())
print(f"There were {len(df)} total room sessions with a total duration of {round((df['Duration (seconds)'].sum())/60, 2)} minutes and a total cost of {'${:,.2f}'.format(df['Cost in Dollars'].sum())} dollars.")
df.to_csv('RoomSessionData.csv', index=False, encoding='utf-8')
That's all there is to it - SignalWire's collection of simple video REST APIs make for incredibly easy management and administration of your video rooms and sessions.
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!