PagerDuty Python REST API Sessions (pdpyras)

5.4.1 · deprecated · verified Thu Apr 09

pdpyras is a lightweight Python client that provides an opinionated wrapper around the Requests HTTP library for interacting with the PagerDuty REST API v2 and Events API v2. It aims to simplify common tasks like authentication, pagination, and error handling. The library currently supports Python 3.6 through 3.13, with version 5.4.1 being the latest release. However, this library has been officially deprecated by PagerDuty in favor of `python-pagerduty` since early 2025, with version 5.4.1 being the final bugfix release that also introduces a deprecation warning upon import.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an `APISession` and fetch PagerDuty incidents. It emphasizes using environment variables for API keys and `default_from` email for security and proper API usage, especially with account-level keys. Replace placeholders with your actual PagerDuty API key and a valid PagerDuty user email.

import os
from pdpyras import APISession

# Retrieve PagerDuty API key and 'From' email from environment variables
# It's highly recommended to use environment variables for sensitive data.
API_KEY = os.environ.get("PAGERDUTY_API_KEY", "YOUR_PAGERDUTY_API_KEY")
# For account-level API keys, a 'From' header (user email) is often required
# for write operations to certain endpoints. Use a valid PagerDuty user email.
DEFAULT_FROM_EMAIL = os.environ.get("PAGERDUTY_FROM_EMAIL", "your_email@example.com")

if API_KEY == "YOUR_PAGERDUTY_API_KEY":
    print("WARNING: PAGERDUTY_API_KEY environment variable not set. Using placeholder.")
if DEFAULT_FROM_EMAIL == "your_email@example.com":
    print("WARNING: PAGERDUTY_FROM_EMAIL environment variable not set. Using placeholder.")

try:
    # Initialize a PagerDuty REST API v2 session.
    # For user-level API keys, `default_from` might be inferred or not needed.
    # For account-level API keys, it is often essential for POST/PUT operations.
    session = APISession(API_KEY, default_from=DEFAULT_FROM_EMAIL)

    # Example: Fetch and print the first 2 incidents that are currently triggered or acknowledged.
    print("\nFetching recent incidents (triggered or acknowledged)...")
    incidents = session.list_all(
        'incidents',
        params={'statuses[]': ['triggered', 'acknowledged'], 'limit': 2}
    )

    if incidents:
        for i, incident in enumerate(incidents):
            print(f"Incident {i+1}: ID={incident['id']}, Summary='{incident['summary']}', Status={incident['status']}')")
    else:
        print("No incidents found or an issue occurred with the API call.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →