Skip to content

Instantly share code, notes, and snippets.

@Johnsonkdavid
Last active July 19, 2024 20:18
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()
@hai2259
Copy link

hai2259 commented Jul 19, 2024

Hi Johnson,
Thank you for the script. Is there a way to include user last login date in this export?
Thanks again.

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