PagerDuty Python Client

6.2.1 · active · verified Thu Apr 09

The `pagerduty` library provides lightweight Python clients for PagerDuty's REST API v2 and Events API v2, built on top of the `httpx` HTTP client. It is the official and actively maintained successor to the `pdpyras` library, offering features like connection pooling, authentication, pagination, and configurable retry logic. The current version is 6.2.1, with a regular release cadence addressing bug fixes, new features, and API endpoint support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the PagerDuty `RestApiV2Client` and perform a basic API call, such as listing users. It highlights the importance of setting the `PAGERDUTY_API_KEY` and, for account-level API keys, the `PAGERDUTY_FROM_EMAIL` header for certain write operations.

import os
from pagerduty import RestApiV2Client

# PagerDuty API Key (recommended: user-scoped or service-specific key)
PAGERDUTY_API_KEY = os.environ.get('PAGERDUTY_API_KEY', 'YOUR_PAGERDUTY_API_KEY')
# 'From' email header is required for some actions (e.g., resolving incidents) 
# when using an account-level API key.
PAGERDUTY_FROM_EMAIL = os.environ.get('PAGERDUTY_FROM_EMAIL', 'your_user@example.com')

if not PAGERDUTY_API_KEY or PAGERDUTY_API_KEY == 'YOUR_PAGERDUTY_API_KEY':
    print("Error: PAGERDUTY_API_KEY environment variable not set or placeholder used.")
    exit(1)

try:
    # Initialize the REST API client
    # For account-level API keys, pass default_from for actions requiring a 'From' header
    client = RestApiV2Client(PAGERDUTY_API_KEY, default_from=PAGERDUTY_FROM_EMAIL)

    # Example: List users
    print("Fetching PagerDuty users...")
    users_response = client.get('/users')

    if users_response.is_success:
        users = users_response.json().get('users', [])
        for user in users:
            print(f"User ID: {user['id']}, Name: {user['name']}, Email: {user['email']}")
        print(f"Successfully fetched {len(users)} users.")
    else:
        print(f"Error fetching users: {users_response.status_code} - {users_response.text}")
        
    # Example: Create an incident (requires a service API key, not account-level)
    # This example requires a valid `service_id` and `from` email and a user with permissions
    # incident_data = {
    #     "incident": {
    #         "type": "incident",
    #         "title": "Test Incident from Python Client",
    #         "service": {"id": "PXXXXXX", "type": "service_reference"},
    #         "priority": {"id": "PXXXXXX", "type": "priority_reference"}, # Optional
    #         "body": {"type": "text", "details": "This is a test incident created via the Python client."}
    #     }
    # }
    # create_incident_response = client.post('/incidents', json=incident_data, headers={'From': PAGERDUTY_FROM_EMAIL})
    # if create_incident_response.is_success:
    #     print(f"Incident created: {create_incident_response.json()['incident']['id']}")
    # else:
    #     print(f"Error creating incident: {create_incident_response.status_code} - {create_incident_response.text}")

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

view raw JSON →