{"_id":"5673125858c4890d00bb537c","user":"5668c7855d13810d0060961d","parentDoc":null,"project":"564e5930c3553e0d003e53d0","__v":7,"category":{"_id":"5c4783ef523219027055513a","project":"564e5930c3553e0d003e53d0","version":"564e5a9b1560880d008d30dc","__v":0,"sync":{"url":"","isSync":false},"reference":false,"createdAt":"2019-01-22T20:58:23.001Z","from_sync":false,"order":1,"slug":"tools-libraries","title":"Libraries, Tools, and Examples"},"version":{"_id":"564e5a9b1560880d008d30dc","project":"564e5930c3553e0d003e53d0","__v":27,"createdAt":"2015-11-19T23:26:19.166Z","releaseDate":"2015-11-19T23:26:19.166Z","categories":["564e5a9b1560880d008d30dd","566318e1f5ca460d00f41896","56631d08cd54d50d005015fa","56631d2a81ad7417006a202c","5668ba19fbd7680d009375f4","5668cb8b10bda80d00797ed9","5668cb9d10bda80d00797eda","56830d8a3f94e00d004e2a7a","56830d9072bb720d0091f594","56830d94cb4d190d0027698e","56830dc44aecbd0d00a464c5","569e90f3c9b43e0d00c4bab1","56a96d338791090d00113bab","56b12d8336d2580d00247877","56c36bf0a869d017002ea55b","56c36bf93d30210d00ea84bb","56c77749b935671700ff0304","56c7ab9e5652c217008e091a","56cb8bdad5c6241d00ef5e61","58aefce02470660f00b54539","58aefd0bebd7370f0078b954","59ca65ca4337830026edf24f","5c33cd9eb47ba20051ac8d64","5c33df728bec1d0063431c34","5c4783ef523219027055513a","5c4f35033400f3010203a999","5d1d0c9f19c3a0003aeb525a"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"Foundation","version_clean":"2.0.0","version":"2"},"githubsync":"","metadata":{"title":"","description":"","image":[]},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-17T19:51:52.977Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":5,"body":"Triggering an incident manually within PagerDuty can happen several ways: through the dashboard, via email, via the Events APIs ([v2](doc:events-api-v2) or [v1](doc:events-api)), or with the [Incident Creation API](doc:incident-creation-api). The first two methods are covered extensively in our [support documentation](https://support.pagerduty.com/docs/incidents#section-triggering-an-incident-with-web-ui-email-or-api). In this guide, we'll go over using [Events API v1](doc:events-api) with [PDJS](https://github.com/PagerDuty/pdjs).\n\nTo trigger an incident via Events API v1, you'll need a `service_key`. You can find this information on a service's page in the web app or via [the REST API](https://api-reference.pagerduty.com/#!/Services/get_services). The following code sample will use [PDJS](https://github.com/pagerduty/pdjs), a simple JavaScript SDK, but you can trigger incidents via any library or language that can work with HTTP and JSON.\n\nBe aware that Events API v1 needs a `service_key` to be present in the payload. This is the same as the **Integration Key** acquired from the web UI, or `integration_key` acquired from the REST API.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Set up the library\\nPDJS = new PDJSobj(\\n  api_version: 'v1'\\n)\\n\\n\\n// Trigger via the Events API\\nPDJS.trigger({\\n  service_key: \\\"YOUR_INTEGRATION_KEY_HERE\\\",\\n  incident_key: \\\"srv01/HTTP\\\",\\n  event_type: \\\"trigger\\\",\\n  description: \\\"FAILURE for production/HTTP on machine srv01.acme.com\\\",\\n  client: \\\"Sample Monitoring Service\\\",\\n  client_url: \\\"https://monitoring.service.com\\\",\\n  details: {\\n    \\\"ping time\\\": \\\"1500ms\\\",\\n    \\\"load avg\\\": 0.75\\n  },\\n  contexts: [\\n    {\\n      type: \\\"link\\\",\\n      href: \\\"http://acme.pagerduty.com\\\"\\n    },{\\n      type: \\\"link\\\",\\n      href: \\\"http://acme.pagerduty.com\\\",\\n      text: \\\"View the incident on PagerDuty\\\"\\n    },{\\n      type: \\\"image\\\",\\n      src: \\\"https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1\\\"\\n    },{\\n      type: \\\"image\\\",\\n      src: \\\"https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1\\\",\\n      href: \\\"https://google.com\\\"\\n    }\\n  ]\\n});\",\n      \"language\": \"javascript\",\n      \"name\": \"JavaScript with PDJS\"\n    }\n  ]\n}\n[/block]\nThe `incident_key` field may be any string that identifies this incident. Submitting multiple events for the same `incident_key` will result in the incident being triggered again. This event will be attached to the existing incident with this key and will alert via the service's escalation policy. Using an `incident_key` from a closed incident will result in a new incident being created.\n\nIf you want to create a new incident, even if one already exists, omitting the `incident_key` will cause a new key to be generated for you.\n\nTo play with this sample code, you can enter an integration key on this [JSFiddle](http://jsfiddle.net/webdemo/rj7a29qn/) to manually trigger an incident. Open your browser's JavaScript console to view the API's response.","excerpt":"","slug":"pdjs-example","type":"basic","title":"Trigger an Incident in JavaScript"}

Trigger an Incident in JavaScript


Triggering an incident manually within PagerDuty can happen several ways: through the dashboard, via email, via the Events APIs ([v2](doc:events-api-v2) or [v1](doc:events-api)), or with the [Incident Creation API](doc:incident-creation-api). The first two methods are covered extensively in our [support documentation](https://support.pagerduty.com/docs/incidents#section-triggering-an-incident-with-web-ui-email-or-api). In this guide, we'll go over using [Events API v1](doc:events-api) with [PDJS](https://github.com/PagerDuty/pdjs). To trigger an incident via Events API v1, you'll need a `service_key`. You can find this information on a service's page in the web app or via [the REST API](https://api-reference.pagerduty.com/#!/Services/get_services). The following code sample will use [PDJS](https://github.com/pagerduty/pdjs), a simple JavaScript SDK, but you can trigger incidents via any library or language that can work with HTTP and JSON. Be aware that Events API v1 needs a `service_key` to be present in the payload. This is the same as the **Integration Key** acquired from the web UI, or `integration_key` acquired from the REST API. [block:code] { "codes": [ { "code": "// Set up the library\nPDJS = new PDJSobj(\n api_version: 'v1'\n)\n\n\n// Trigger via the Events API\nPDJS.trigger({\n service_key: \"YOUR_INTEGRATION_KEY_HERE\",\n incident_key: \"srv01/HTTP\",\n event_type: \"trigger\",\n description: \"FAILURE for production/HTTP on machine srv01.acme.com\",\n client: \"Sample Monitoring Service\",\n client_url: \"https://monitoring.service.com\",\n details: {\n \"ping time\": \"1500ms\",\n \"load avg\": 0.75\n },\n contexts: [\n {\n type: \"link\",\n href: \"http://acme.pagerduty.com\"\n },{\n type: \"link\",\n href: \"http://acme.pagerduty.com\",\n text: \"View the incident on PagerDuty\"\n },{\n type: \"image\",\n src: \"https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1\"\n },{\n type: \"image\",\n src: \"https://chart.googleapis.com/chart?chs=600x400&chd=t:6,2,9,5,2,5,7,4,8,2,1&cht=lc&chds=a&chxt=y&chm=D,0033FF,0,0,5,1\",\n href: \"https://google.com\"\n }\n ]\n});", "language": "javascript", "name": "JavaScript with PDJS" } ] } [/block] The `incident_key` field may be any string that identifies this incident. Submitting multiple events for the same `incident_key` will result in the incident being triggered again. This event will be attached to the existing incident with this key and will alert via the service's escalation policy. Using an `incident_key` from a closed incident will result in a new incident being created. If you want to create a new incident, even if one already exists, omitting the `incident_key` will cause a new key to be generated for you. To play with this sample code, you can enter an integration key on this [JSFiddle](http://jsfiddle.net/webdemo/rj7a29qn/) to manually trigger an incident. Open your browser's JavaScript console to view the API's response.