Opsgenie Node.js API

The Opsgenie SDK for Node.js provides a set of Node.js API for Opsgenie services, making it easier for Node.js developers to build applications that integrate with Opsgenie. Developers can build Node.js applications on top of APIs that take the complexity out of coding directly against a web service interface. The library provides APIs that hide much of the lower-level plumbing, including authentication, request retries, and error handling.

Before you begin, you must sign up for Opsgenie service, create an API Integration and get your API key. In order to use the Opsgenie Node.js SDK, you will need the API key from the API Integration you created. The API key is used to authenticate requests to the service and identify yourself as the sender of a request.

Node.js SDK now supports new Alert API v2 as of v0.4.0. Please consider upgrading your sdk to have new functionality of Alert API v2. Existing Alert methods are deprecated and will not get further support. Examples below are updated using new Alert API.

Node.js SDK Installation

Opsgenie Node.js SDK requires Node.js (version > 0.6) to be installed. You can install from Node.js website. Following that the API can be installed using npm install opsgenie-sdk (add --save option if you want it to be added to package.json automatically). The command will automatically download and install necessary package files and dependencies.

Opsgenie Node.js Sdk source code is available at GitHub Opsgenie Node.js Sdk repository.

Client Initialization

Opsgenie client is the basic data structure on which all operations and actions are taken. A client variable can be created as following.

var opsgenie = require('opsgenie-sdk');

    opsgenie.configure({
        'api_key': 'your_api_key'
    });

It is required to set the API key parameter to execute Opsgenie Web Api calls using client. Otherwise the authentication will fail and the API is going to raise an exception.

Get Request Status

var requestId = "yourRequestId";

            opsgenie.alertV2.getRequestStatus(requestId, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Request Status" );
                    console.log(JSON.parse(result).data);
                }
            });

Create Alert

var create_alert_json = {
            "message": "Hello world my alert :)"
        };

        opsgenie.alertV2.create(create_alert_json, function (error, alert) {
            if (error) {
                console.error(error);
            } else {
                console.log(alert);
            }
        });

Close Alert

var close_alert_identifier = {
                identifier: "alert_id",
                identifierType: "id"
            };

            var close_alert_data = {
                note: "alert close note",
                user: "[email protected]",
                source: "source of the alert close action"
            };

            opsgenie.alertV2.close(close_alert_identifier, close_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Close Alert Response");
                    console.log(result);
                }
            });

Delete Alert

var delete_alert_identifier = {
                identifier: "alert_id",
                identifierType:"id"
            };

            opsgenie.alertV2.delete(delete_alert_identifier, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Delete Alert Response");
                    console.log(result);
                }
            });

Get Alert

var get_alert_identifier = {
                identifier: "alert_id",
                identifierType: "id"
            };

            opsgenie.alertV2.get(get_alert_identifier, function (error, alert) {
                if (error) {
                    console.log(error);
                } else {
                    console.log("Get Alert Response");
                    console.log(alert);
                }
            });

List Alerts

var list_alert_json = {
                query : "status : open",
                offset : 0,
                limit : 10,
                sort : "alias",
                oder : "desc"
            };

            opsgenie.alertV2.list(list_alert_json, function (error, alerts) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("List Alert Response");
                    console.log(alerts);
                }
            });

List Alert Logs

var list_alert_logs_identifier = {
                identifier: "alert_id",
                identifierType: "id"
            };

            var list_params = {
                offset : "1496838477430_1498839315423029794",
                direction : "next",
                limit : "5",
                order : "desc"
            };

            opsgenie.alertV2.listAlertLogs(list_alert_logs_identifier, list_params, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("List Alert Logs Response");
                    console.log(result);
                }
            });

List Alert Notes

var list_alert_notes_identifier = {
                identifier: "alert_id",
                identifierType: "id"
            };

            var list_params = {
                offset : "1496838477430_1496839315423029794",
                direction : "next",
                limit : "5",
                order : "desc"
            };

            opsgenie.alertV2.listAlertNotes(list_alert_notes_identifier, list_params, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("List Alert Notes Response");
                    console.log(result);
                }
            });

List Alert Recipients

var list_recipients_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };
            opsgenie.alertV2.listAlertRecipients(list_recipients_identifier, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("List Alert Recipient Response");
                    console.log(result);
                }
            });

Acknowledge

var acknowledge_alert_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var acknowledge_alert_data = {
                note : "alert acknowledge note",
                user : "[email protected]",
                source : "source of the acknowledge action"
            };
            opsgenie.alertV2.acknowledge(acknowledge_alert_identifier, acknowledge_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Acknowledge Response");
                    console.log(result);
                }
            });

Unacknowledge

var unacknowledge_alert_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var unacknowledge_alert_data = {
                note : "some note for unacknowledge action",
                user : "[email protected]",
                source : "source of the unacknowledge request"
            };

            opsgenie.alertV2.unacknowledge(unacknowledge_alert_identifier, unacknowledge_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Unacknowledge Alert Response");
                    console.log(result);
                }
            });

Snooze

var snooze_alert_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var snooze_alert_data = {
                note : "some note for snooze action",
                user : "[email protected]",
                source : "source of the snooze request",
                endTime : "2017-06-09T08:30:50.894Z"
            };

            opsgenie.alertV2.snooze(snooze_alert_identifier, snooze_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Snooze Response");
                    console.log(result);
                }
            });

Assign Owner

var assign_alert_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var assign_alert_data = {
                note : "some note for assign action",
                user : "[email protected]",
                source : "source of the assign request",
                owner : {
                    username : "[email protected]"
                }
            };

            opsgenie.alertV2.assign(assign_alert_identifier, assign_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Assign Response");
                    console.log(result);
                }
            });

Add Team

var addTeam_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var addTeam_data = {
                note : "some note for add team action",
                user : "[email protected]",
                source : "source of the add team request",
                team : {
                    name : "ops_team"
                }
            };

            opsgenie.alertV2.addTeam(addTeam_request_identifier, addTeam_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Add Team Response");
                    console.log(result);
                }
            });

Add Note

var addNote_alert_identifier = {
                identifier: "alert_id",
                identifierType: "id"
            };

            var addNote_alert_data = {
                note : "some note to add",
                user : "[email protected]",
                source : "source of the request"
            };

            opsgenie.alertV2.addNote(addNote_alert_identifier, addNote_alert_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Add Note Response");
                    console.log(result);
                }
            });

Add Tags

var addTags_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var addTags_data = {
                note : "some note for add tags action",
                user : "[email protected]",
                source : "source of the add tags request",
                tags : ["tag1", "tag2"]
            };

            opsgenie.alertV2.addTags(addTags_request_identifier, addTags_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Add Tags Response");
                    console.log(result);
                }
            });

Remove Tags

var removeTags_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var removeTags_data = {
                note : "some note for remove tags action",
                user : "[email protected]",
                source : "source of the remove tags request",
                tags : "tag1, tag2"
            };

            opsgenie.alertV2.removeTags(removeTags_request_identifier, removeTags_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Remove Tags Response");
                    console.log(result);
                }
            });

Add Details

var addDetails_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var addDetails_data = {
                note : "some note for add details action",
                user : "[email protected]",
                source : "source of the add details request",
                details : {
                    key1 : "value1",
                    key2 : "value2"
                }
            };

            opsgenie.alertV2.addDetails(addDetails_request_identifier, addDetails_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Add Details Response");
                    console.log(result);
                }
            });

Remove Details

var removeDetails_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var removeDetails_data = {
                note : "some note for remove details action",
                user : "[email protected]",
                source : "source of the remove details request",
                keys : "key1, key2"
            };

            opsgenie.alertV2.removeDetails(removeDetails_request_identifier, removeDetails_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Remove Details Response");
                    console.log(result);
                }
            });

Escalate To Next

var escalateToNext_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var escalateToNext_data = {
                note : "some note for escalate to next action",
                user : "[email protected]",
                source : "source of the escalate to next request",
                escalation : {
                    name : "ops_team_escalation"
                }
            };

            opsgenie.alertV2.escalateToNext(escalateToNext_identifier, escalateToNext_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Escalate To Next Response");
                    console.log(result);
                }
            });

Execute Custom Action

var custom_action_request_identifier = {
                identifier: "alert_id",
                identifierType : "id"
            };

            var custom_action_data = {
                note : "some note for custom action request",
                user : "[email protected]",
                source : "source of the custom action request",
                action : "newCustomAction"
            };

            opsgenie.alertV2.executeAction(custom_action_request_identifier, custom_action_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Execute Action Success Response");
                    console.log(result);
                }
            });

Create Saved Search

var create_savedSearch_data = {
                name : "open_and_acked",
                query : "status: open and acknowledged: true",
                owner : {
                    username : "[email protected]"
                },
                description : "Will be used to display the alerts that are closed before someone acknowledges.",
                teams : [
                    {
                        name : "ops_team"
                    }
                ]
            };

            opsgenie.alertV2.createSavedSearch(create_savedSearch_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Create Saved Search Response");
                    console.log(result);
                }
            });

Get Saved Search

var get_saved_search_identifier = {
                identifier: "open_and_acked",
                identifierType : "name"
            };

            opsgenie.alertV2.getSavedSearch(get_saved_search_identifier, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Get Saved Search Response");
                    console.log(result);
                }
            });

Update Saved Search

var update_saved_search_identifier = {
                identifier: "open_and_acked",
                identifierType : "name"
            };

            var update_savedSearch_data = {
                name : "open_and_acked",
                query : "status: open and acknowledged: true",
                description : "Will be used to display the alerts that are open and acknowledged."
            };

            opsgenie.alertV2.updateSavedSearch(update_saved_search_identifier, update_savedSearch_data, function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("Update Saved Search Response");
                    console.log(result);
                }
            });

List Saved Search

opsgenie.alertV2.listSavedSearches(function (error, result) {
                if (error) {
                    console.error(error);
                } else {
                    console.log("List Saved Searches Response");
                    console.log(result);
                }
            });

User API

Available actions:

  • opsgenie.user.create(json, error, success)
  • opsgenie.user.get(json, error, success)
  • opsgenie.user.update(json, error, success)
  • opsgenie.user.list(json, error, success)
  • opsgenie.user.delete(json, error, success)
  • opsgenie.user.deleteById(json, error, success)

Group API

Available actions:

  • opsgenie.group.create(json, error, success)
  • opsgenie.group.get(json, error, success)
  • opsgenie.group.update(json, error, success)
  • opsgenie.group.list(json, error, success)
  • opsgenie.group.delete(json, error, success)
  • opsgenie.group.deleteById(json, error, success)
  • opsgenie.group.addMember(json, error, success)
  • opsgenie.group.removeMember(json, error, success)

Team API

Available actions:

  • opsgenie.teams.create(json, error, success)
  • opsgenie.teams.get(json, error, success)
  • opsgenie.teams.update(json, error, success)
  • opsgenie.teams.list(json, error, success)
  • opsgenie.teams.delete(json, error, success)
  • opsgenie.teams.deleteById(json, error, success)
  • opsgenie.teams.addMember(json, error, success)
  • opsgenie.teams.removeMember(json, error, success)
  • opsgenie.teams.listLogs(json, error, success)