Pygerduty Python Client for PagerDuty
Pygerduty is a Python client library for interacting with PagerDuty's REST API and Events API. Originally designed for PagerDuty API v1, it includes some updates for v2 compatibility in a separate module. The latest version is 0.38.3, released in April 2020.
Warnings
- breaking The PagerDuty API v1, which `pygerduty` primarily targets, was officially deprecated by PagerDuty in July 2017 and is no longer supported. The library's `v2` submodule (introduced later) might provide limited v2 compatibility, but the library is largely unmaintained for modern PagerDuty API usage.
- deprecated The library lacks recent maintenance. The last PyPI release was in April 2020, and the GitHub repository shows no significant activity since then. It is effectively unmaintained.
- breaking Python 2 support is no longer relevant as Python 2 reached end-of-life. While older versions of `pygerduty` claimed Python 2.5+ compatibility, modern Python applications should use Python 3.6+.
- gotcha Authentication for the PagerDuty API requires both a subdomain (e.g., 'yourcompany' from 'yourcompany.pagerduty.com') and an API key. For PagerDuty API v2, the `From` header (an email address of a PagerDuty user) is often required for certain actions if an account-level API key is used.
Install
-
pip install pygerduty
Imports
- PagerDuty
import pygerduty pager = pygerduty.PagerDuty("your_subdomain", "your_api_key") - PagerDuty (V2 API)
import pygerduty.v2 pager_v2 = pygerduty.v2.PagerDuty("your_subdomain", "your_api_key")
Quickstart
import os
import pygerduty
# Ensure your PagerDuty subdomain and API key are set as environment variables
PD_SUBDOMAIN = os.environ.get('PD_SUBDOMAIN', 'your_subdomain_here')
PD_API_KEY = os.environ.get('PD_API_KEY', 'YOUR_API_KEY_HERE')
if not PD_SUBDOMAIN or not PD_API_KEY:
print("Please set PD_SUBDOMAIN and PD_API_KEY environment variables.")
else:
try:
# Instantiate a PagerDuty client (targets v1 API by default)
pager = pygerduty.PagerDuty(PD_SUBDOMAIN, PD_API_KEY)
# List schedules (example using v1 API pattern)
print(f"Fetching schedules for subdomain: {PD_SUBDOMAIN}")
for schedule in pager.schedules.list():
print(f" - ID: {schedule.id}, Name: {schedule.name}")
# For PagerDuty API v2 interactions (if implemented in your version/fork)
# import pygerduty.v2
# pager_v2 = pygerduty.v2.PagerDuty(PD_SUBDOMAIN, PD_API_KEY)
# print("\nFetching users via v2 API (if supported by your client config):")
# for user in pager_v2.users.list():
# print(f" - ID: {user.id}, Name: {user.name}, Email: {user.email}")
except Exception as e:
print(f"An error occurred: {e}")
print("Please check your PagerDuty subdomain, API key, and network connectivity.")