create incident using python

I’ve been tasked to writing a python script to validate database restores and, if the restore fails, to trigger a PagerDuty incident. The main body of the script is finished, but I am having trouble with the part that would trigger an incident.

I’ve looked at pdpyras and I’ve looked at the example script at The latter seems like it would meet my requirements better than pdpyras, but I have some questions about some of the variable values in the script.

“API_KEY” is self-explanatory, but are there specific values required for “SERVICE_ID” and “FROM”, or are those values that I can assign? I’m working in a GovCloud environment, so I want to be sure that the URL “” is appropriate for what I’m trying to do. “” is reachable from the server, from which the script will run:

$ nc -zv 443
  Connection to 443 port [tcp/https] succeeded!

I would also like to know whether the “incident_key” is an arbitrary MD5 hash that I can generate. Lastly, I would like to know if the example script actually triggers an incident. It looks like it simply prints out a status code and the information collected in the “payload” JSON block. If that is the case, how do I actually send the incident to my PD Incident dashboard?

SERVICE_ID will be the pagerduty service the incident will be opened for. FROM will be the email of the user associated with the action. incident_key is used to avoid the API from creating more than one incident with the same key (basic deduping logic). iirc, it can be any value up to 255 characters. Sending the payload to this endpoint creates an incident on the dashboard.

If I could suggest the alternative of using the event consumer instead. My teams have found sending alerts and letting PagerDuty do the logic of creating an incident to be the smoother path. They can control how PagerDuty handles the alert at the service level instead of changing the emitting code.

@preocts, in the example you provided, would the routing_key be the same as the api_key? Is there any way you could provide a working example script that I could use as a template?

routing_key is an integration or ruleset key. Different than an API key. Routing keys are used by the event enqueue endpoint. Overall an easier way to create incidents, in my opinion, unless you have reason to heavily manage an incident via the API.

Example script I’ve used over the year:
(Note, routing key can be for a service integration or a global ruleset)

@JCMiller, Thank you very much. I will experiment with this and try to integrate it into my existing script.