{"_id":"5673125858c4890d00bb537c","user":"5668c7855d13810d0060961d","parentDoc":null,"project":"564e5930c3553e0d003e53d0","__v":6,"category":{"_id":"56c77749b935671700ff0304","__v":6,"pages":["56c777aec2f82e0d00d7cfc4","56cf5f668629f91300fd97ce","56d00c374a03c00b00a07c03","56d3b713d3f4650b00749658","56d3d74fd3f4650b00749677","56d3d9d20b39260b008da47b"],"project":"564e5930c3553e0d003e53d0","version":"564e5a9b1560880d008d30dc","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2016-02-19T20:12:57.931Z","from_sync":false,"order":2,"slug":"events-api","title":"Events API v1"},"version":{"_id":"564e5a9b1560880d008d30dc","project":"564e5930c3553e0d003e53d0","__v":22,"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"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"Foundation","version_clean":"2.0.0","version":"2"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2015-12-17T19:51:52.977Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":2,"body":"Triggering an incident manually within PagerDuty can happen several ways: through the dashboard, via email, or via [the Events API](doc:events-api) . The first two methods are covered extensively in our [support documentation](https://support.pagerduty.com/hc/en-us/articles/202829230-How-to-Trigger-an-Incident-Through-the-Dashboard-Email-Or-The-API), so we'll go over using the [Events API](doc:events-api) .\n\nTo trigger an incident via the API, you'll need an `integration_key`. You can find this information on a service's page in the web app or via [the services API](/v2/page/api-reference#!/Services/get_services_id). The following code sample will use [PDJS](https://github.com/eurica/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 the Events API needs a `service_key` to be present in the payload. This is the same as the `integration_key` you acquired earlier.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Set up the library\\nPDJS = new PDJSobj()\\n\\n\\n// Trigger via the Events API\\nPDJS.trigger({\\n  service_key: \\\"YOUR_SERVICE_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 a service key on this [JSFiddle](http://jsfiddle.net/PagerDuty/azh132ch/8/) to manually trigger an incident. Open your browser's JavaScript console to view the API's response.","excerpt":"","slug":"manually-trigger-an-incident","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, or via [the Events API](doc:events-api) . The first two methods are covered extensively in our [support documentation](https://support.pagerduty.com/hc/en-us/articles/202829230-How-to-Trigger-an-Incident-Through-the-Dashboard-Email-Or-The-API), so we'll go over using the [Events API](doc:events-api) . To trigger an incident via the API, you'll need an `integration_key`. You can find this information on a service's page in the web app or via [the services API](/v2/page/api-reference#!/Services/get_services_id). The following code sample will use [PDJS](https://github.com/eurica/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 the Events API needs a `service_key` to be present in the payload. This is the same as the `integration_key` you acquired earlier. [block:code] { "codes": [ { "code": "// Set up the library\nPDJS = new PDJSobj()\n\n\n// Trigger via the Events API\nPDJS.trigger({\n service_key: \"YOUR_SERVICE_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 a service key on this [JSFiddle](http://jsfiddle.net/PagerDuty/azh132ch/8/) to manually trigger an incident. Open your browser's JavaScript console to view the API's response.