Verified Environment

Version
OSUbuntu 20.04.5 LTS
bashGNU bash, version 5.0.17(1)-release (aarch64-unknown-linux-gnu)
jqjq-1.6
curlcurl 7.68.0
PagerDuty APIV2 2023-02-14

What to do

Note

It uses 2 APIs:

Examples

Here is an example of getting the email of the primary on-caller:

#!/bin/bash

if [ "${PAGERDUTY_API_KEY}" = "" ]; then
    echo "\$PAGERDUTY_API_KEY is required."
    exit 1
fi

get_primary_oncallers() {
    local escalation_policy_id="${1}"

    curl -s --request GET \
         --url "https://api.pagerduty.com/oncalls?escalation_policy_ids%5B%5D=${escalation_policy_id}" \
         --header 'Accept: application/vnd.pagerduty+json;version=2' \
         --header "Authorization: Token token=${PAGERDUTY_API_KEY}" \
         --header 'Content-Type: application/json' | jq -r '.oncalls[] | select(.escalation_level == 1).user.id'
}

get_user_email() {
    local user_id="${1}"

    curl -s --request GET \
         --url "https://api.pagerduty.com/users/${user_id}" \
         --header 'Accept: application/vnd.pagerduty+json;version=2' \
         --header "Authorization: Token token=${PAGERDUTY_API_KEY}" \
         --header 'Content-Type: application/json' | jq -r '.user.email'
}
#!/bin/bash

source "pagerduty.sh"

escalation_policy_id="${1}"

if [ "${escalation_policy_id}" = "" ]; then
    echo "\$1 (escalation policy ID) is required."
    exit 1
fi

for user_id in $(get_primary_oncallers "${escalation_policy_id}"); do
    get_user_email "${user_id}"
done
$ bash main.sh ESCALATION_POLICY_ID