Way to extract userID for querying a users details

We have a large amount of users that we are trying to audit who is in an escalation policy, who has notification rules set up with a phone, and who is attached to a service. From what I understand, I need to query the https://api.pagerduty.com/users/*/contact_methods API endpoint to return contact information. The only way to query this endpoint is by having the users id string to pass through.

I see the string exists as part of some urls when querying https://api.pagerduty.com/users endpoint for all users but there doesn’t appear to be an easy way to extract this value for querying against PagerDuty.

Is there an easy way to map a user email to this Id for better querying? Or is there a way to query a users information via email address instead of this random string of characters that mean nothing outside of PagerDuty? All of our audits go based on the users name or their email and as of yet I haven’t found a good way to convert/map these emails to PagerDuty ids and manually using the UI to check 700 users contact methods is not a good use of time.

Thank you in advance!

Hello @GavinClark,

The simplest way that comes to mind is, for each email, to use the Users Index with query=<email address>, and then take the first result.

A much more efficient approach (it just requires a bit more client-side code) would be to fetch and memoize the list of users and perform the audit locally, i.e. by paginating through all the results of the users index and then generating a map of users by email address for the audit. (Tip if you’re using Python: pdpyras.APISession.dict_all will do this in one fell swoop by passing it keyword argument by='email')

In either case, it’s a good idea to have notification_rules and contact_methods set in the includes parameter so that way you don’t have to make an additional API call for each and every user to get their notification rules and another one for their contact methods. When you do it this way, you get all the related notification rules/contact methods within each user object.

Thank you for the reply, Demitri! I was unaware pdpyras existed, this is a real help! Using the first example in the Basic Usage section:

from pdpyras import APISession

api_token = ‘your-token-here’
session = APISession(api_token)
for user in session.iter_all(‘users’):
print(user[‘id’], user[‘email’], user[‘name’])

I was able to get the readout of all users and their emails mapped to their id. I tried adding notification_rules[‘notification_rules’] on to the print line to see if it’d work, but that fails stating notification_rules is undefined. Is there a way to add something like that to the above or would I have to iterate through each user since it’s a different endpoint?

Thanks!

That’s great!

To reiterate, one needs to set the include[] parameter. How to do that:

In pdpyras, as with requests, the keyword argument params is how to pass that through. (Note, the *_all methods pass params straight through to the requests API, with no modifications except to limit and offset for handling pagination).

Includes are multivalued (and this needs to go in our docs because it’s not explicit) so putting it all together:

session.dict_all('users', by='email', params={'include[]': ['notification_rules']})

That will give you a dictionary object of all users in the account, keyed by email address, with each value itself being a dictionary representation of the user and containing a notification_rules key.

(Pardon the late reply; I was out of office last week)