Last active
February 20, 2025 19:14
-
-
Save Johnsonkdavid/4600580d2e9c01854c97fd1fca63f733 to your computer and use it in GitHub Desktop.
Python api code to export Users list from Pagerduty to CSV file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
##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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The
pagerduty
python client can handle this easy, no need to hit the REST API directly (it's still usingrequests
behind the scenes).Rewrote this here https://gist.github.com/atrepca/57f49e02782b5f6c60fe9f8ba97101e3