Opsgenie Python SDK

2.1.5 · active · verified Thu Apr 09

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

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the Opsgenie SDK, configure API key authentication, and create a basic alert. Ensure you have an Opsgenie API key set as an environment variable `OG_API_KEY` or replace 'YOUR_API_KEY' directly in the code.

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

view raw JSON →