PagerDuty Blog

PagerDuty integration for JavaScript

As part of our push to treat our API as a first class part of the Product, we’ve enabled CORS headers so you can embed information about your PagerDuty account in an intranet page with only client side scripting.

This means, for instance, that you can embed the status of a service inside a wiki page with only JavaScript. I’ll try to roll out some more sample code soon, but the following snippet works in all modern browsers (assuming the page already has jQuery):

jQuery.ajax({
  url: "https://[subdomain].pagerduty.com/api/v1/incidents/count",
  type: "GET",
  contentType: 'application/json',
  headers: {
    Authorization: 'Token token=[token]'
  },
  success: function(json) {
    alert(json);
  }
});

Click here to test it out.

An authentication token needs to be sent with every request, so you’ll need to generate an API key. A word of caution: since anyone can view the source of your JavaScript, do not publish your script outside your organization. You may also want to use a read-only API key. (I’ve used a demo account for these examples. Click here to test it out.)

Since CORS has now been enabled for all API’s, you can also trigger events and do everything with JavaScript that is possible through any our API’s. Click here for more information.