PagerDuty Python API Client
pypd is a community-supported Python client for PagerDuty's v2 API, allowing developers to interact with PagerDuty services. It provides an interface for common API operations. The latest version is 1.1.0, released in March 2018, indicating a low release cadence.
Warnings
- breaking The 'pypd' library is community-supported and has not been updated since March 2018. For new projects, PagerDuty itself recommends using the more actively maintained `python-pagerduty` library, which is a successor to other PagerDuty Python clients like `pdpyras`.
- gotcha Authentication requires a PagerDuty API token (v2) and potentially an API URL. It is crucial to handle these credentials securely, ideally through environment variables (e.g., `PD_API_KEY`, `PD_API_URL`) rather than hardcoding them.
- gotcha The library name 'pypd' can easily be confused with 'pypdf' or 'PyPDF2', which are popular Python libraries for PDF file manipulation. Ensure you are installing and importing the correct library for PagerDuty API interactions.
Install
-
pip install pypd
Imports
- pypd
import pypd
Quickstart
import pypd
import os
# Configure API key either directly or from environment variables
# It's recommended to use environment variables for security.
# export PD_API_KEY='YOUR_PAGERDUTY_API_KEY'
# export PD_API_URL='https://api.pagerduty.com'
# Option 1: Set directly (less secure for production)
# pypd.api_key = os.environ.get('PD_API_KEY', 'YOUR_PAGERDUTY_API_KEY')
# pypd.api_url = os.environ.get('PD_API_URL', 'https://api.pagerduty.com')
# Option 2: Initialize from environment variables (recommended)
pypd.from_env()
# Example: Fetch an incident by ID
try:
# Replace 'YOUR_INCIDENT_ID' with an actual incident ID
incident_id = os.environ.get('PAGERDUTY_TEST_INCIDENT_ID', 'YOUR_INCIDENT_ID')
if incident_id == 'YOUR_INCIDENT_ID':
print("Please set PAGERDUTY_TEST_INCIDENT_ID environment variable or replace 'YOUR_INCIDENT_ID' in code.")
else:
incident = pypd.Incident.fetch(id=incident_id)
print(f"Fetched incident: {incident['title']} (ID: {incident['id']})")
# Example: Print a few details
print(f"Status: {incident['status']}")
print(f"Urgency: {incident['urgency']}")
except Exception as e:
print(f"Error fetching incident: {e}")
print("Ensure PD_API_KEY and PD_API_URL are set correctly and the incident ID is valid.")