0

Trigger Event (Events API v2)

by
Published 5 days ago

Trigger an alert/incident on the routing key's service via the Events API v2. summary, source and severity are required. Pass a stable dedup_key to deduplicate and to later acknowledge or resolve this alert; if omitted, PagerDuty generates one and returns it.

Script pagerduty
  • Submitted by hugo989 Typescript (fetch-only)
    Created 6 days ago
    1
    //native
    2
    
    
    3
    /**
    4
     * Trigger Event (Events API v2)
    5
     * Trigger an alert/incident on the routing key's service via the Events API v2. summary, source and severity are required. Pass a stable dedup_key to deduplicate and to later acknowledge or resolve this alert; if omitted, PagerDuty generates one and returns it.
    6
     */
    7
    export async function main(
    8
      auth: RT.PagerdutyEvents,
    9
      summary: string,
    10
      source: string,
    11
      severity: "critical" | "error" | "warning" | "info",
    12
      dedup_key: string | undefined,
    13
      component: string | undefined,
    14
      group: string | undefined,
    15
      event_class: string | undefined,
    16
      custom_details: { [key: string]: any } | undefined,
    17
      client: string | undefined,
    18
      client_url: string | undefined,
    19
    ) {
    20
      const payload: { [key: string]: any } = { summary, source, severity }
    21
      if (component !== undefined && component !== "") payload.component = component
    22
      if (group !== undefined && group !== "") payload.group = group
    23
      if (event_class !== undefined && event_class !== "")
    24
        payload.class = event_class
    25
      if (custom_details !== undefined) payload.custom_details = custom_details
    26
    
    
    27
      const event: { [key: string]: any } = {
    28
        routing_key: auth.routing_key,
    29
        event_action: "trigger",
    30
        payload,
    31
      }
    32
      if (dedup_key !== undefined && dedup_key !== "") event.dedup_key = dedup_key
    33
      if (client !== undefined && client !== "") event.client = client
    34
      if (client_url !== undefined && client_url !== "") event.client_url = client_url
    35
    
    
    36
      const response = await fetch("https://events.pagerduty.com/v2/enqueue", {
    37
        method: "POST",
    38
        headers: {
    39
          "Content-Type": "application/json",
    40
          Accept: "application/json",
    41
        },
    42
        body: JSON.stringify(event),
    43
      })
    44
    
    
    45
      if (!response.ok) {
    46
        throw new Error(`${response.status} ${await response.text()}`)
    47
      }
    48
    
    
    49
      return await response.json()
    50
    }
    51