Pygerduty Python Client for PagerDuty

0.38.3 · deprecated · verified Sat Apr 11

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

Install

Imports

Quickstart

This quickstart demonstrates how to instantiate a `pygerduty` client for the PagerDuty v1 API (default behavior) and fetch a list of schedules. It uses environment variables for authentication credentials.

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.")

view raw JSON →