PagerDuty Python REST API Sessions (pdpyras)
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
- breaking The `pdpyras` library is officially deprecated by PagerDuty. For all new projects, use the `python-pagerduty` library instead, which is its successor. Existing projects should also consider migrating.
- breaking When migrating from `pdpyras` to `python-pagerduty`, many class and exception names have changed. For example, `APISession` becomes `RestApiV2Client`, `EventsAPISession` becomes `EventsApiV2Client`, and `PDClientError` becomes `Error`.
- gotcha When using an account-level API key (created by an administrator) for REST API v2 endpoints that take actions on incidents (e.g., creating notes, resolving), you must supply the `default_from` keyword argument to the `APISession` constructor with a valid PagerDuty user's email address. Failing to do so will result in an `HTTP 400` response.
- gotcha Error handling differentiates between low-level `requests`-like methods (`get`, `post`) which return `requests.Response` objects even on HTTP errors, and higher-level methods (`list_all`, `iter_all`) which raise `pdpyras.PDClientError` (or its subclasses) on non-success HTTP statuses after exhausting retries. A `401 Unauthorized` error will immediately raise `PDClientError`.
Install
-
pip install pdpyras
Imports
- APISession
from pdpyras import APISession
- EventsAPISession
from pdpyras import EventsAPISession
- ChangeEventsAPISession
from pdpyras import ChangeEventsAPISession
- PDSession
from pdpyras import PDSession
Quickstart
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}")