Skip to content

Instantly share code, notes, and snippets.

@Johnsonkdavid
Last active February 20, 2025 19:14
Show Gist options
  • Save Johnsonkdavid/4600580d2e9c01854c97fd1fca63f733 to your computer and use it in GitHub Desktop.
Save Johnsonkdavid/4600580d2e9c01854c97fd1fca63f733 to your computer and use it in GitHub Desktop.
Python api code to export Users list from Pagerduty to CSV file
##Tested and verified in python3 env
#!/usr/bin/python3
import requests
import csv
import json
headers = {
"Content-Type": "application/json",
"Accept": "application/vnd.pagerduty+json;version=2",
"Authorization": "Token token=xxxxxxxxxxxx" ##Replace your api token here
}
def get_pd_users():
#CSV file header for your report file
header=['UserName', 'Email', 'Role']
with open('pagerduty_users.csv', 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(header)
for offset in range(0, 2000, 100):
url = 'https://api.pagerduty.com/users?limit=100&offset={0}'.format(offset)
response = requests.request("GET", url, headers=headers)
p=json.loads(response.text)
for x in range(len(p['users'])):
Usr_name = p['users'][x]['name']
Mail_id = p['users'][x]['email']
Role = p['users'][x]['role']
output = (Usr_name,Mail_id,Role)
out_list = list(output)
writer.writerow(out_list)
def main():
get_pd_users()
if __name__ == "__main__":
main()
@atrepca
Copy link

atrepca commented Feb 20, 2025

The PagerDuty API is not exposing this @hai2259, but their support team can help get a user report including the last login date.

@atrepca
Copy link

atrepca commented Feb 20, 2025

The pagerduty python client can handle this easy, no need to hit the REST API directly (it's still using requests behind the scenes).

Rewrote this here https://gist.github.com/atrepca/57f49e02782b5f6c60fe9f8ba97101e3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment