ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software

How do I use PagerDuty with C#?

Updated on June 7, 2015

What is PagerDuty?

PagerDuty is an alerting system that allows you to set up groups of people for alerts, escalations, and allows for multiple alerting methods. The purpose here is not to go into detail about PagerDuty itself, but give you a way to add a PagerDuty alert to an existing application using C#. This will allow you to take advantage of your company’s PagerDuty implementation from a wide variety of other applications.

Why alert?

The purpose of alerting us to notify someone that they need to do something. Usually this is a critical operation for the company. There are many ways to do alerting, probably the most common method is by email. With PagerDuty, there is an application, SMS text messaging option, or an email option. In addition, you can personally set multiple notification schemes. These can all go off at the same time, or you can set in interval between which each will go to you. This system also allows you to create escalations. For critical items. This means that someone else will be notified if you do not respond in a set period of time. That period of time is to find with in the web-based application.

What is required?

For the implementation I am going to discuss, I used the NuGet package RestSharp. I will discuss NuGet later on. RestSharp is a package that allows you to use rest API calls necessary in this case for dealing with PagerDuty.

From the PagerDuty side, you will need to create a service and have it give you an API key. If you’re familiar with PagerDuty, you probably know that you have a specific URL to use in order to look at your incidents. When you use the API call, however, you call a generic URL and it is this key that associates the incident with your system.

The Program

Before we get into the actual code, let’s take a look at what we are going to do.

  1. We need to set up the rest call by setting the client, the base URL, the response, and the data format, in this case, JSON.

  2. We then set the resource, which is the remainder of the URL.

  3. We then add the body of the request. In this case we will use the following pieces:

    1. The service key, which is the API key that you created at PagerDuty.

    2. The event type, which is always trigger.

    3. The description, what text you want to show.

    4. The incident key, which is the subject.

With these four pieces of information, we now have enough to create a request.

  1. We now set the method to post.

  2. And create our call.

  3. I also split off the incident key from the response. This allows me to return that value.

Now for the code, I will note that this is generic code available online as a demonstration on how to use RestSharp. I included here only to illustrate how to use it for PagerDuty.

This is also been put as a subroutine, to allow you to use it in multiple places.

public static string PagerDutySend(string APIKey, string Description, string Incident)
var client = new RestClient(); //("URL/create_event.json");

client.BaseUrl = "URL";

var request = new RestRequest();

string returnVal = "";

request.RequestFormat = DataFormat.Json;

request.Resource = "generic/2010-04-15/create_event.json";




service_key = APIKey,

event_type = "trigger",

description = Description,

incident_key = Incident


request.Method = Method.POST;

var response = client.Execute(request) as RestResponse;

if (response != null && ((response.StatusCode == HttpStatusCode.OK) &&

(response.ResponseStatus == ResponseStatus.Completed)))

// It's probably not necessary to test both


var content = response.Content;

string[] values = Regex.Split(content, "incident_key\":\"");

returnVal = values[1].Replace("\"}", "");


else if (response != null)


returnVal = string.Format

("Status code is {0} ({1}); response status is {2}",

response.StatusCode, response.StatusDescription, response.ResponseStatus);


return returnVal;



NuGet and RestSharp

NuGet is a repository of packages that you can use within Visual Studio. Within Visual Studio itself, is a manage NuGet packages. This makes it very easy to search for and add the proper resources to your project. If you do a search for RestSharp at that site, on the left is an ability to look at the project site. When you go to the project site, you will see sample code. You will notice that this sample code differs from the code that I used. I did start off attempting to use that code, I found it only by putting it together as a body would it reliably go to PagerDuty. The code I shared above is the code that I’m using in actual practice.


As you can see, you can easily add a PagerDuty module to your application. This will allow you to quickly and easily wrap alerting into any application. This will allow you to notify someone when they need to take action.


    0 of 8192 characters used
    Post Comment

    No comments yet.