Trunk count via API

Is there a simple way to get the count of active trunks using API?

I would imagine something like this:

import requests

url = "https://api.mypurecloud.com/api/v2/telephony/providers/edges/trunks"
headers = {
    "Authorization": "Bearer MY_SUPER_SECRET_TOKEN",
    "Content-Type": "application/json"
}

response = requests.get(url, headers=headers)
trunks = response.json().get("entities", [])

# Count the number of trunks that are connected
active_trunks = sum(1 for trunk in trunks if trunk.get("connectedStatus") == "connected")

print(f"Total number of active trunks: {active_trunks}")

But for the life of me, this does not give me what I want. Where am I going wrong? (Other than the use of "sum" and "for" together - don't kill me :slight_smile: )

It looks like there may be two things that are causing you issues.

  • connectedStatus isn't a string so that check will always fail.
  • It doesn't look like every trunk will have a connectedStatus object associated with it (looks like tieing trunks from what I saw)

See if the following change works.

# Count the number of trunks that are connected
active_trunks = 0

for trunk in trunks:
  if 'connectedStatus' not in trunk.keys():
    continue
  if trunk['connectedStatus']['connected'] == True:
      active_trunks += 1

I was playing with it, and the interesting thing is that if you use "active" you will just get the total number of trunks, not the number of trunks actually being used at this time. I wonder if there is a way to get a count of idle trunks vs. busy similar to what we see in Phone Trunks menu where you see the total number of currently engaged inbound and outbound trunks.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.