Opsgenie Python Alert API

To use the Alert functionalities of Opsgenie's API via the Python SDK you will first have to import the SDK library and (configure)(doc:opsgenie-python-api-v2-1#section-client-initialization) it with at least the API Key that you procured from your Opsgenie Integrations.

# Importing relevant Opsgenie SDK libraries including the Alert API client
import opsgenie_sdk

class Example:
    def __init__(self, opsgenie_api_key):
        self.conf = self.conf = opsgenie_sdk.configuration.Configuration()
        self.conf.api_key['Authorization'] = '<Your-API-Key>'

        self.api_client = opsgenie_sdk.api_client.ApiClient(configuration=self.conf)
        self.alert_api = opsgenie_sdk.AlertApi(api_client=self.api_client)

Create Alert

You can create a new Alert using the Alert client that is initialized prior to performing any other Opsgenie API functionality.

In creating the new alert, please ensure you pass the required and right values in, as a CreateAlertPayload object, to the create_alert function via the create_alert_payload parameter. To know which values are required and can be used, please visit the Create Alert section in the Opsgenie REST API documentation here.

def create(self):
  body = opsgenie_sdk.CreateAlertPayload(
    message='Sample',
    alias='python_sample',
    description='Sample of SDK v2',
    responders=[{
        'name': 'SampleTeam',
        'type': 'team'
      }],
    visible_to=[
      {'name': 'Sample',
       'type': 'team'}],
    actions=['Restart', 'AnExampleAction'],
    tags=['OverwriteQuietHours'],
    details={'key1': 'value1',
             'key2': 'value2'},
    entity='An example entity',
    priority='P3'
  )
  try:
    create_response = self.alert_api.create_alert(create_alert_payload=body)
    print(create_response)
    return create_response
  except opsgenie_sdk.ApiException as err:
    print("Exception when calling AlertApi->create_alert: %s\n" % err)

Get Request Status

Alert creation, deletion, and action requests are processed asynchronously to provide higher availability and scalability, therefore valid requests for those endpoints are responded to with HTTP status 202 - Accepted. The Get Request Status endpoint is used to track the status and alert details (if any) of the request whose identifier is given.

You can get the request status using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In getting a request's status, please ensure you pass the required and right values into the get_request_status function. To know which values are required and can be used, please visit the Get Request Status section in the Opsgenie REST API documentation here.

def get_request_status(self):
  try:
    response = self.alert_api.get_request_status(request_id=self._request_id)
    print(response)
    return response
   except ApiException as err:
      print("Exception when calling AlertApi->get request status: %s\n" % err)

Get Alert

You can get an Alert using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In getting an Alert, please ensure you pass the required and right values into the get_alert function. To know which values are required and can be used, please visit the Get Alert section in the Opsgenie REST API documentation here.

def get_alert(self, alert_id):
  try:
    get_response = self.alert_api.get_alert(identifier=alert_id, identifier_type='id')
    print(get_response)
    return get_response
  except ApiException as err:
    print("Exception when calling AlertApi->get_alert: %s\n" % err)

Delete Alert

You can delete an alert using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In deleting an alert, please ensure you pass the required and right values into the delete_alert function. To know which values are required and can be used, please visit the Delete Alert section in the Opsgenie REST API documentation here.

def delete_alert(self, alert_id):
  try:
    delete_response = self.alert_api.delete_alert(identifier=alert_id, identifier_type='id')
    return delete_response
  except ApiException as err:
    print("Exception when calling AlertApi->delete_response: %s\n" % err)

Acknowledge

You can acknowledge an alert using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In acknowledging an alert, please ensure you pass the required and right values as an AcknowledgeAlertPayload object to the acknowledge_alert function via the acknowledge_alert_payload parameter. To know which values are required and can be used, please visit the Acknowledge Alert section in the Opsgenie REST API documentation here.

def acknowledge(self):
  body = opsgenie_client.AcknowledgeAlertPayload(user='Sample User', note='ack', source='python sdk')
  try:
    awknowledge_response = self.alert_api.acknowledge_alert(identifier=self._alert_id, acknowledge_alert_payload=body)
    print(awknowledge_response)
    return awknowledge_response
   except ApiException as err:
      print("Exception when calling AlertApi->acknowledge: %s\n" % err)

List Alerts

You can list alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In listing alerts, please ensure you pass the required and right values into the list_alerts function. To know which values are required and can be used, please visit the List Alerts section in the Opsgenie REST API documentation here.

def list_alerts(self):
  query = 'status=open'
  try:
    list_response = self.alert_api.list_alerts(limit=3, offset=5, sort='updatedAt', order='asc', search_identifier_type='name', query=query)
    print(list_response)
    return list_response
  except ApiException as err:
    print("Exception when calling AlertApi->list_alerts: %s\n" % err)

Count Alerts

You can count alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

The function does not need any parameters passed to it.

def count_alerts(self):
  try:
    count_response = self.alert_api.count_alerts()
    print(count_response)
    return count_response
  except ApiException as err:
    print("Exception when calling AlertApi->count__alerts: %s\n" % err)

Close Alerts

You can close alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In closing an alert, please ensure you pass the required and right values as a CloseAlertPayload object to the close_alert function via the close_alert_payload parameter . To know which values are required and can be used, please visit the Close Alert section in the Opsgenie REST API documentation here.

def close_alert(self, alert_id):
  body = opsgenie_sdk.CloseAlertPayload(user='ghastly', note='ghastly was here', source='python sdk')
  try:
    close_response = self.alert_api.close_alert(identifier=alert_id, close_alert_payload=body)
    print(close_response)
    return close_response
  except ApiException as err:
    print("Exception when calling AlertApi->close_alerts: %s\n" % err)

Add Note to Alert

You can add notes to alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding a note to an alert, please ensure you pass the required and right values as an AddNoteToAlertPayload object to the add_note function via the add_note_to_alert_payload parameter. To know which values are required and can be used, please visit the Add Note to Alert section in the Opsgenie REST API documentation here.

def add_note(self):
  body = opsgenie_client.AddNoteToAlertPayload(user='pikachu', note='thunder bolt', source='python sdk')
  try:
    note_response = self.alert_api.add_note(identifier=self._alert_id, add_note_to_alert_payload=body)
    print(note_response)
    return note_response
    except ApiException as err:
      print("Exception when calling AlertApi->add note: %s\n" % err)

Execute Custom Alert Action

You can execute custom actions using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In executing an action, please ensure you pass the required and right values in as an ExecuteCustomAlertActionPayload object to the execute_custom_alert_action function via the execute_custom_alert_action_payload parameter. To know which values are required and can be used, please visit the Execute Custom Actions section in the Opsgenie REST API documentation here.

def execute_custom_action(self):
  body = opsgenie_client.ExecuteCustomAlertActionPayload(user='charizard', note='breathes fire', source='python sdk')
  try:
    action_response = self.alert_api.execute_custom_alert_action(identifier=self._alert_id, execute_custom_alert_action_payload=body,action_name='test')
    print(action_response)
    return action_response
    except ApiException as err:
      print("Exception when calling AlertApi->execute custom action: %s\n" % err)

Unacknowledge Alert

You can unacknowledge alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In unacknowledge an alert, please ensure you pass the required and right values in as an UnAcknowledgeAlertPayload object to the un_acknowledge_alert function via the un_acknowledge_alert parameter. To know which values are required and can be used, please visit the Unacknowledge Alert section in the Opsgenie REST API documentation here.

def unacknowledge(self):
  body = opsgenie_client.UnAcknowledgeAlertPayload(user='mewtwo', note='legendary', source='python sdk')
  try:
    unacknowledge_response = self.alert_api.un_acknowledge_alert(identifier=self._alert_id, un_acknowledge_alert=body)
    print(unacknowledge_response)
    return unacknowledge_response
    except ApiException as err:
      print("Exception when calling AlertApi->unacknowledge alert: %s\n" % err)

Snooze Alert

You can snooze alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In snoozing an alert, please ensure you pass the required and right values in as a SnoozeAlertPayload object to the snooze_alert function via the snooze_alert_payload parameter. To know which values are required and can be used, please visit the Snooze Alert section in the Opsgenie REST API documentation here.

def snooze(self):
  body = opsgenie_client.SnoozeAlertPayload(user='squirtle', note='water gun', source='python sdk',end_time="2020-04-03T20:05:50.894Z")
  try:
    snooze_response = self.self.alert_api.snooze_alert(identifier=alert_id, snooze_alert_payload=body)
    print(snooze_response)
    return snooze_response
    except ApiException as err:
      print("Exception when calling AlertApi->snooze: %s\n" % err

Escalate Alert

You can escalate alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In escalating an alert, please ensure you pass the required and right values in an EscalateAlertToNextPayload object to the escalate_alert function via the escalate_alert_to_next_payload parameter. To know which values are required and can be used, please visit the Escalate Alert section in the Opsgenie REST API documentation here.

def escalate(self):
  body = opsgenie_client.EscalateAlertToNextPayload(escalation={'name': 'sample_escalation'})
  try:
    escalate_response = self.alert_api.escalate_alert(escalate_alert_to_next_payload=body, identifier=self._alert_id)
    print(escalate_response)
    return escalate_response
    except ApiException as err:
      print("Exception when calling AlertApi->escalate: %s\n" % err)

Assign Alert

You can assign alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In assigning an alert, please ensure you pass the required and right values in the AssignAlertPayload object to the assign_alert function via the assign_alert_payload parameter. To know which values are required and can be used, please visit the Assign Alert section in the Opsgenie REST API documentation here.

def assign(self):
  body = opsgenie_client.AssignAlertPayload(owner={'username': '[email protected]'})
  try:
    response = self.alert_api.assign_alert(assign_alert_payload=body, identifier=self._alert_id)
    print(response)
    except ApiException as err:
      print("Exception when calling AlertApi->assign: %s\n" % err)

Add Team to Alert

You can add teams to alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding a team to an alert, please ensure you pass the required and right values in as an AddTeamToAlertPayload object to the add_team function via the add_team_to_alert_payload parameter. To know which values are required and can be used, please visit the Add Team to Alert section in the Opsgenie REST API documentation here.

def add_team(self):
  body = opsgenie_client.AddTeamToAlertPayload(team={'name': 'sampleName'})
  try:
    add_team_response = self.alert_api.add_team(add_team_to_alert_payload=body, identifier=self._alert_id)
    print(add_team_response)
    return add_team_response
    except ApiException as err:
      print("Exception when calling AlertApi->add team: %s\n" % err)

Add Responder to Alert

You can add responder to alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding a responder to an alert, please ensure you pass the required and right values in as an AddResponderToAlertPayload object to the add_responder function via the add_responder_to_alert_payload parameter. To know which values are required and can be used, please visit the Add Responder to Alert section in the Opsgenie REST API documentation here.

def add_responder(self):
  body = opsgenie_client.AddResponderToAlertPayload(
    responder={'type': 'user', 'username': '[email protected]'})
  try:
    add_responder_response = self.alert_api.add_responder(add_responder_to_alert_payload=body, identifier=self._alert_id)
    print(add_responder_response)
    return add_responder_response
    except ApiException as err:
      print("Exception when calling AlertApi->add responder: %s\n" % err)

Add Tags to Alert

You can add tags to alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding tags to an alert, please ensure you pass the required and right values in as an AddTagsToAlertPayload object to the add_tags function via the add_tags_to_alert_payload parameter. To know which values are required and can be used, please visit the Add Tags to Alert section in the Opsgenie REST API documentation here.

def add_tags(self):
  body = opsgenie_client.AddTagsToAlertPayload(tags=['test1', 'test2', 'tag3'])
  try:
    add_tags_response = self.alert_api.add_tags(add_tags_to_alert_payload=body, identifier=self._alert_id)
    print(add_tags_response)
    return add_tags_response
    except ApiException as err:
      print("Exception when calling AlertApi->add tags: %s\n" % err)

Remove Tags from Alert

You can add tags from alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In removing tags from an alert, please ensure you pass the required and right values to the remove_tags function. To know which values are required and can be used, please visit the Remove Tags from Alert section in the Opsgenie REST API documentation here.

def remove_tags(self):
  try:
    remove_tage_response = self.alert_api.remove_tags(identifier=self._alert_id, tags=['OverwriteQuietHours'], user='userName', source='python sdk', note='testing')
    print(remove_tage_response)
    return remove_tage_response
    except ApiException as err:
      print("Exception when calling AlertApi->remove tags: %s\n" % err)

Add Details (Custom Properties) to Alert

You can add details to alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding details to an alert, please ensure you pass the required and right values in as an AddDetailsToAlertPayload object to the add_details function via the add_details_to_alert_payload. To know which values are required and can be used, please visit the Add Details (Custom Properties) to Alert section in the Opsgenie REST API documentation here.

def add_details(self):
  body = opsgenie_client.AddDetailsToAlertPayload(details={'serverName': 'Zion', 'region': 'Oregon'})
  try:
    add_details_response = self.alert_api.add_details(identifier=self._alert_id, add_details_to_alert_payload=body)
    print(add_details_response)
    return add_details_response
    except ApiException as err:
      print("Exception when calling AlertApi->add details: %s\n" % err)

Remove Details (Custom Properties) from Alert

You can remove details from alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In removing details from an alert, please ensure you pass the required and right values to the remove_details function. To know which values are required and can be used, please visit the Remove Details (Custom Properties) from Alert section in the Opsgenie REST API documentation here.

def remove_details(self):
  try:
    remove_details_response = self.alert_api.remove_details(identifier=self._alert_id, keys=['serverName'], user='ghastly')
    print(remove_details_response)
    return remove_details_response
    except ApiException as err:
      print("Exception when calling AlertApi->remove details: %s\n" % err)

Update Alert Priority

You can update the priorities of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In updating the priority of an alert, please ensure you pass the required and right values in as an UpdateAlertPriorityPayload object to the update_alert_priority function via the update_alert_priority_payload parameter. To know which values are required and can be used, please visit the Update Alert Priority section in the Opsgenie REST API documentation here.

def update_priority(self):
  body = opsgenie_client.UpdateAlertPriorityPayload(priority='P2')
  try:
    update_priority_response = self.alert_api.update_alert_priority(update_alert_priority_payload=body, identifier=self._alert_id)
    print(update_priority_response)
    return update_priority_response
    except ApiException as err:
      print("Exception when calling AlertApi->update priority %s\n" % err)

Update Alert Message

You can update the messages of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In updating the message of an alert, please ensure you pass the required and right values in as an UpdateAlertMessagePayload object to the update_alert_message function via the update_alert_message_payload parameter. To know which values are required and can be used, please visit the Update Alert Message section in the Opsgenie REST API documentation here.

def update_message(self):
  body = opsgenie_client.UpdateAlertMessagePayload(message='UpdatedMessage')
  try:
    update_message_response = self.alert_api.update_alert_message(update_alert_message_payload=body, identifier=self._alert_id)
    print(update_message_response)
    return update_message_response
    except ApiException as err:
      print("Exception when calling AlertApi->update message %s\n" % err)

Update Alert Description

You can update the descriptions of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In updating the description of an alert, please ensure you pass the required and right values in as an UpdateAlertDescriptionPayload object to the update_alert_description function via the update_alert_description_payload parameter. To know which values are required and can be used, please visit the Update Alert Description section in the Opsgenie REST API documentation here.

def update_description(self):
  body = opsgenie_client.UpdateAlertDescriptionPayload(description='UpdatedDescription')
  try:
    update_description_response = self.alert_api.update_alert_description(update_alert_description_payload=body, identifier=self._alert_id)
    print(update_description_response)
    return update_description_response
    except ApiException as err:
      print("Exception when calling AlertApi->update description %s\n" % err)

List Alert Recipients

You can update the recipients of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In updating the recipients of an alert, please ensure you pass the required and right values in the list_recipients function. To know which values are required and can be used, please visit the List Alert Recipients section in the Opsgenie REST API documentation here.

def list_recipients(self):
  try:
    list_recipients_response = self.alert_api.list_recipients(identifier=self._alert_id)
    print(list_recipients_response)
    return list_recipients_response
    except ApiException as err:
      print("Exception when calling AlertApi->list recipients %s\n" % err)

List Alert Logs

You can list the logs of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In listing the logs of an alert, please ensure you pass the required and right values in the list_logs function. To know which values are required and can be used, please visit the List Alert Logs section in the Opsgenie REST API documentation here.

def list_logs(self):
  try:
    list_logs_response = self.alert_api.list_logs(identifier=self._alert_id, offset=5, direction='next', limit=3, order='desc')
    print(list_logs_response)
    return list_logs_response
    except ApiException as err:
      print("Exception when calling AlertApi->list logs %s\n" % err)

List Alert Notes

You can list the notes of alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In listing the notes of an alert, please ensure you pass the required and right values in the list_notes function. To know which values are required and can be used, please visit the List Alert Notes section in the Opsgenie REST API documentation here.

def list_notes(self):
  try:
    list_notes_response = self.alert_api.list_notes(identifier=self._alert_id)
    print(list_notes_response)
    return list_notes_response
    except ApiException as err:
      print("Exception when calling AlertApi->list notes %s\n" % err)

Create a Saved Search

You can create saved searches to search your alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In creating a saved search, please ensure you pass the required and right values in as a CreateSavedSearchPayload object to the create_saved_searches function via the create_saved_search_payload. To know which values are required and can be used, please visit the Create a Saved Search section in the Opsgenie REST API documentation here.

def create_saved_search(self):
  body = opsgenie_client.CreateSavedSearchPayload(name='Closed but Unacked Alerts', description='viaPythonSdk', query='status: Open and acknowledged: false', owner={'username': '[email protected]'}, teams=[{'name': 'sampleName'}])
  try:
    sacved_search_response = self.alert_api.create_saved_searches(body=body)
    print(sacved_search_response)

    data = sacved_search_response.data
    self._saved_search_id = data.id
    return sacved_search_response
    except ApiException as err:
      print("Exception when calling AlertApi->create saved search %s\n" % err)

List Saved Searches

You can list your saved searches using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In listing the saved searches, please ensure you pass the required and right values in the list_saved_searches function. To know which values are required and can be used, please visit the List Saved Searches section in the Opsgenie REST API documentation here.

def list_saved_search(self):
  try:
    list_save_searches_response = self.alert_api.list_saved_searches()
    print(list_save_searches_response)
    return list_save_searches_response
    except ApiException as err:
      print("Exception when calling AlertApi->list saved search %s\n" % err)

Update Saved Search

You can update your saved searches using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In updating a saved search, please ensure you pass the required and right values in as an UpdateSavedSearchPayload object to the update_saved_search function. To know which values are required and can be used, please visit the Update Saved Search section in the Opsgenie REST API documentation here.

def update_saved_search(self):
  body = opsgenie_client.UpdateSavedSearchPayload(name='Updated Closed but Unacked Alerts', description='viaPythonSdk', query='status: Open and acknowledged: false', owner={'username': '[email protected]'}, teams=[{'name': 'sampleName'}])
  try:
    update_saved_search_response = self.alert_api.update_saved_search(update_saved_search_payload=body, identifier=self._saved_search_id)
    print(update_saved_search_response)
    return update_saved_search_response
    except ApiException as err:
      print("Exception when calling AlertApi->update saved search %s\n" % err)

Get Saved Searches

You can get your saved searches using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In getting a saved search, please ensure you pass the required and right values in the get_saved_search function. To know which values are required and can be used, please visit the Get Saved Search section in the Opsgenie REST API documentation here.

def get_saved_search(self):
  try:
    saved_searches_response = self.alert_api.get_saved_search(identifier=self._saved_search_id)
    print(saved_searches_response)
    return saved_searches_response
    except ApiException as err:
      print("Exception when calling AlertApi->get saved search %s\n" % err)

Delete Saved Searches

You can delete your saved searches using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In deleting a saved search, please ensure you pass the required and right values in the delete_saved_search function. To know which values are required and can be used, please visit the Delete Saved Search section in the Opsgenie REST API documentation here.

def delete_saved_search(self):
  try:
    delete_saved_searches_response = self.alert_api.delete_saved_search(identifier=self._saved_search_id)
    print(delete_saved_searches_response)
    return delete_saved_searches_response
    except ApiException as err:
      print("Exception when calling AlertApi->delete saved search %s\n" % err)

Add Alert Attachment

You can add attachments to your alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In adding an attachment to your alert, please ensure you pass the required and right values to the add_attachment function. To know which values are required and can be used, please visit the Create Alert Attachment section in the Opsgenie REST API documentation here.

Furthermore, you can access and save the id of your newly created attachment by accessing the id property of the data object in the returned success_response object.

def add_attachment(self):
  try:
    success_response = self.alert_api.add_attachment(identifier=self._alert_id,
                                             file='testAttachment.txt')
    print(success_response)

    self._attachment_id = success_response.data.id
    except ApiException as err:
      print("Exception when calling AlertApi->add attachment %s\n" % err)

Get Alert Attachment

You can get the attachments from your alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In getting an attachment from an alert, please ensure you pass the required and right values in the get_attachment function. To know which values are required and can be used, please visit the Get Alert Attachment section in the Opsgenie REST API documentation here.

def get_attachment(self):
  try:
    get_attachment_response = self.alert_api.get_attachment(identifier=self._alert_id, attachment_id=self._attachment_id)
    print(get_attachment_response)
    return get_attachment_response
    except ApiException as err:
      print("Exception when calling AlertApi->get attachment %s\n" % err)

List Alert Attachments

You can list the attachments from your alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In listing an attachment from an alert, please ensure you pass the required and right values in the list_attachments function. To know which values are required and can be used, please visit the List Alert Attachments section in the Opsgenie REST API documentation here.

def list_attachments(self):
  try:
    response = self.alert_api.list_attachments(identifier=self._alert_id)
    print(list_attachments_response)
    return list_attachments_response
    except ApiException as err:
      print("Exception when calling AlertApi->list attachments %s\n" % err)

Delete Alert Attachment

You can delete the attachments from your alerts using the Alert client that is initialized prior to performing any Opsgenie API functionality.

In deleting an attachment from an alert, please ensure you pass the required and right values in the remove_attachment function. To know which values are required and can be used, please visit the Delete Alert Attachment section in the Opsgenie REST API documentation here.

def delete_attachment(self):
  try:
    delete_attachement_response = self.alert_api.remove_attachment(identifier=self._alert_id, attachment_id=self._attachment_id)
    print(delete_attachement_response)
    return delete_attachement_response
    except ApiException as err:
      print("Exception when calling AlertApi->delete attachment %s\n" % err)