'how to regenerate pagerduty integration key programmatically

I have integrated Jenkins CI with pagerduty. Once I do that, I can see intergration key generated. enter image description here

That will be used in jenkins to send the events to pagerduty.

The requirement is to rotate the keys after some time. I want to automate this. Is there any api to regenerate the intergration key and return the key in response to be stored in jenkins?



Solution 1:[1]

I think the simplest solution here is to use the REST API -- it isn't possible to regenerate the integration key directly, but you can delete the integration and create a new one programmatically.

First fetch the service details:

curl --location --request GET 'https://api.pagerduty.com/services/<service_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'

This will include all of the integrations on the service -- make note of the integration_id and the vendor_id.

The delete endpoint isn't documented but it does seem to exist:

curl --location --request DELETE 'https://api.pagerduty.com/services/<service_id>/integrations/<integration_id>' \
--header 'Accept: application/vnd.pagerduty+json;version=2' \
--header 'Authorization: Bearer <bearer_token>'

And finally you can create the new integration, using the vendor_id from the GET request:

curl --request POST \
  --url https://api.pagerduty.com/services/id/integrations \
  --header 'Accept: application/vnd.pagerduty+json;version=2' \
  --header 'Authorization: Bearer <bearer_token>' \
  --header 'Content-Type: application/json' \
  --data '{
  "integration": {
    "type": "generic_email_inbound_integration",
    "name": "Email",
    "service": {
      "id": "<service_id>",
      "type": "service_reference"
    },
    "integration_email": "[email protected]",
    "vendor": {
      "type": "vendor_reference",
      "id": "<vendor_id>"
    }
  }

Solution 2:[2]

On doing inspect element to UI button UI button to regenerate key

Its executing POST API: https://xxxxxxx.pagerduty.com/api/v1/services/XXXXXXX/integrations/XXXXXXX/regenerate_key But cannot find it in API Docs.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hannele
Solution 2 Akhil Jain