Retrieve the Body of an Email Using the API

Follow

This process requires the use of our Log Entries API. This is part of our v1 REST API.

To get started you may manually create an incident to retrieve or use one that already exists. In this example a trigger email was sent to an email service named "California."

Next, download the scripts available here; you'll need them both. They will be used in this order:

These scripts are written in Ruby, mostly as wrappers for curl calls to the API. While the curl calls work just as well, packaging them this way makes them reusable and easy to adapt to other purposes, such as saving the output to a CSV file. 

We'll be using these two endpoints: GET incidents/:incident_id/log_entries and GET log_entries/:id.

As noted on our Log Entries API page:

PagerDuty keeps a log of all the events that happen to an incident. The following APIs provide fine-grained access to this incident log entry data to give you more insight into how your team or organization is handling your incidents. Log entry data includes details about the event(s) that triggered the incident, who was notified and when, how they were notified, and who acknowledged or resolved it, amongst a few other things.

For the purposes of this explanation, everything has an ID. Each service has an ID; each incident created within a service also has an ID. A log entry is created at every step of the incident lifecycle - upon triggering, acknowledging, resolving, etc. In other words, each step in the history of an incident also has an ID. By traversing this tree, we can find the details about the history of each incident, at the level of granularity that we desire.

In this example, we'll be using GET incidents/:incident_id/log_entries to get all the log entries from the incident trigged by email, in summary form. From this, we'll grab the ID of the log entry created by the triggering email. We'll then use GET log_entries/:id to find the details of that log entry, including the body of the email.

Now fill in the values you'll need to make the "incident event summary" script work.

File: pd-log-entries.rb

  • subdomain - the name of your account; the name part in name.pagerduty.com, present in the address bar for your account in the web UI.

  • service_id - the ID of your service, available from the address bar on the service's individual page in our web UI, afterpagerduty.com/services/. In our example, this is PZR20EU. Your service ID should be of similar length and appearance.

  • api_key - the API key you'll need to interact with our API; if you haven't yet, you'll need to create one.

  • incident_id - the ID of the incident you want to retrieve, available from the address bar on the incident's individual page. In our example this is PJ8VS9G. Your incident ID should be of similar length and appearance.

Run this script from the command line by issuing the command:

ruby pdlogentries.rb

After a successful tun, this will appear in your terminal at the bottom of your output:

{
"id"=>"PABLHO8",
"type"=>"trigger",
"created_at"=>"2014-01-22T22:07:29Z",
"agent"=>{
"id"=>"PJBWSMD",
"name"=>"Julian Martinez",
"email"=>"fakeuser@notgmail.com",
"time_zone"=>"Pacific Time (US & Canada)",
"color"=>"purple",
"role"=>"owner",
"avatar_url"=>"https://secure.gravatar.com/avatar/f0201u1cab0gaf766a28a2a3d2718949.png?d=mm&r=PT",
"user_url"=>"/users/PJBWSMD",
"invitation_sent"=>false,
"marketing_opt_out"=>false,
"type"=>"user"
},
"channel"=>{
"summary"=>"Performance has slowed by 25 %",
"type"=>"web_trigger"
}
}

The summary field confirms that this is the right ID. We can now feed this ID, PABLHO8, into the "log entry" script, to retrieve the body of the email. 

You'll need to run the script and examine the part of the output to find the log entry ID to plug into your script. Now fill in the values you'll need to make the "log entry details" script work.

File: pd-log-entry-detail.rb

  • subdomain 

  • log_id - the ID of the log entry you want to retrieve. In our case this is PABLHO8. Your log ID should be of similar length and appearance.

  • api_key - the same API key generated to run the first script

Run this script from the command line by issuing the command:

ruby pdlogentryinfo.rb

After a successful tun, this will appear in your terminal at the bottom of your output:

{
"id"=>"PABLHO8",
"type"=>"trigger",
"created_at"=>"2014-01-22T22:07:29Z",
"agent"=>{
"id"=>"PJBWSMD",
"name"=>"Julian Martinez",
"email"=>"fakeuser@notgmail.com",
"time_zone"=>"Pacific Time (US & Canada)",
"color"=>"purple",
"role"=>"owner",
"avatar_url"=>"https://secure.gravatar.com/avatar/f0201u1cab0gaf766a28a2a3d2718949.png?d=mm&r=PT",
"user_url"=>"/users/PJBWSMD",
"invitation_sent"=>false,
"marketing_opt_out"=>false,
"type"=>"user"
},
"channel"=>{
"subject"=>"Performance has slowed by 25%",
"details"=>"Services affected. Raphel: 2983. Mai: 8924. Ameche: 1502. Zabi: 6360. Almi: 4582.",
"summary"=>"Performance has slowed by 25%",
"type"=>"web_trigger"
}
}

As you can see, the body of the email is included in details:

Services affected. Raphel: 2983. Mai: 8924. Ameche: 1502. Zabi: 6360. Almi: 4582.

Your output should show a similar result for the incident you targeted.

Have more questions? Submit a request

Comments