PagerDuty Python API Client

1.1.0 · maintenance · verified Wed Apr 15

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the pypd client using environment variables for authentication and then fetch a PagerDuty incident by its ID. It highlights the recommended practice of configuring API keys securely via environment variables.

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

view raw JSON →