Opsgenie Python SDK
The Opsgenie Python SDK provides a client for interacting with the Opsgenie REST API, enabling programmatic management of alerts, incidents, heartbeats, and account settings. The current version is 2.1.5, and the library generally maintains a frequent release cadence for bug fixes and dependency updates, especially within the 2.x series.
Warnings
- breaking The SDK underwent a complete re-implementation with version 2.0.1. All imports, class names, and API interaction patterns changed significantly from the 0.x.x series.
- gotcha Older versions of the SDK (prior to 2.1.5) may depend on vulnerable versions of `urllib3` (below 1.26.5), which is susceptible to Denial Of Service (DoS) issues. The SDK itself fixed this by updating its `urllib3` dependency.
- gotcha When working with the Incident API in early 2.x versions (e.g., 2.0.x), there were inconsistencies in field names. For example, `teamId` was required to be `ownerTeam`, and `details` should be `extraProperties` in some contexts.
- gotcha The `numpy` dependency was removed in version 2.1.1. While this is generally a positive change for reducing dependencies, users upgrading from earlier `2.x.x` versions might notice `numpy` is no longer installed automatically.
Install
-
pip install opsgenie-sdk
Imports
- Configuration
from opsgenie_sdk import Configuration
- ApiClient
from opsgenie_sdk import ApiClient
- AlertApi
from opsgenie_sdk import AlertApi
- CreateAlertRequest
from opsgenie_sdk import CreateAlertRequest
- IncidentApi
from opsgenie_sdk import IncidentApi
- CreateIncidentRequest
from opsgenie_sdk import CreateIncidentRequest
- AlertResponder
from opsgenie_sdk import AlertResponder
- ApiException
from opsgenie_sdk.rest import ApiException
Quickstart
import opsgenie_sdk
import os
configuration = opsgenie_sdk.Configuration()
# Configure API key authorization: OpsgenieAPIKey
# Replace 'YOUR_API_KEY' or set OG_API_KEY environment variable
configuration.api_key['Authorization'] = os.environ.get('OG_API_KEY', 'YOUR_API_KEY')
configuration.api_key_prefix['Authorization'] = 'GenieKey'
# create an instance of the API client
api_client = opsgenie_sdk.ApiClient(configuration)
alert_api = opsgenie_sdk.AlertApi(api_client)
# Create an alert request body
body = opsgenie_sdk.CreateAlertRequest(
message='Test Alert from Python SDK Checklist.day',
description='This is a test alert created via the Python SDK quickstart.',
priority='P3',
alias='checklist_sdk_test_alert',
entity='Test Service',
tags=['checklist', 'sdk'],
details={'source': 'python-sdk-quickstart'},
source='Python SDK Quickstart'
)
try:
api_response = alert_api.create_alert(body=body)
print('Alert created successfully:')
print(f' ID: {api_response.alert.id}')
print(f' Alias: {api_response.alert.alias}')
print(f' Status: {api_response.status}')
except opsgenie_sdk.rest.ApiException as e:
print(f"Exception when calling AlertApi->create_alert: {e}")