Create a Notification policy

Creates a new Notification policy.

Script cloudflare Verified

by hugo697 ยท 11/16/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 383 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create a Notification policy
8
 * Creates a new Notification policy.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_id: string,
13
  body: {
14
    alert_type:
15
      | "access_custom_certificate_expiration_type"
16
      | "advanced_ddos_attack_l4_alert"
17
      | "advanced_ddos_attack_l7_alert"
18
      | "advanced_http_alert_error"
19
      | "bgp_hijack_notification"
20
      | "billing_usage_alert"
21
      | "block_notification_block_removed"
22
      | "block_notification_new_block"
23
      | "block_notification_review_rejected"
24
      | "brand_protection_alert"
25
      | "brand_protection_digest"
26
      | "clickhouse_alert_fw_anomaly"
27
      | "clickhouse_alert_fw_ent_anomaly"
28
      | "custom_ssl_certificate_event_type"
29
      | "dedicated_ssl_certificate_event_type"
30
      | "dos_attack_l4"
31
      | "dos_attack_l7"
32
      | "expiring_service_token_alert"
33
      | "failing_logpush_job_disabled_alert"
34
      | "fbm_auto_advertisement"
35
      | "fbm_dosd_attack"
36
      | "fbm_volumetric_attack"
37
      | "health_check_status_notification"
38
      | "hostname_aop_custom_certificate_expiration_type"
39
      | "http_alert_edge_error"
40
      | "http_alert_origin_error"
41
      | "incident_alert"
42
      | "load_balancing_health_alert"
43
      | "load_balancing_pool_enablement_alert"
44
      | "logo_match_alert"
45
      | "magic_tunnel_health_check_event"
46
      | "maintenance_event_notification"
47
      | "mtls_certificate_store_certificate_expiration_type"
48
      | "pages_event_alert"
49
      | "radar_notification"
50
      | "real_origin_monitoring"
51
      | "scriptmonitor_alert_new_code_change_detections"
52
      | "scriptmonitor_alert_new_hosts"
53
      | "scriptmonitor_alert_new_malicious_hosts"
54
      | "scriptmonitor_alert_new_malicious_scripts"
55
      | "scriptmonitor_alert_new_malicious_url"
56
      | "scriptmonitor_alert_new_max_length_resource_url"
57
      | "scriptmonitor_alert_new_resources"
58
      | "secondary_dns_all_primaries_failing"
59
      | "secondary_dns_primaries_failing"
60
      | "secondary_dns_zone_successfully_updated"
61
      | "secondary_dns_zone_validation_warning"
62
      | "sentinel_alert"
63
      | "stream_live_notifications"
64
      | "tunnel_health_event"
65
      | "tunnel_update_event"
66
      | "universal_ssl_event_type"
67
      | "web_analytics_metrics_update"
68
      | "zone_aop_custom_certificate_expiration_type";
69
    description?: string;
70
    enabled: boolean;
71
    filters?: {
72
      actions?: string[];
73
      affected_asns?: string[];
74
      affected_components?: string[];
75
      affected_locations?: string[];
76
      airport_code?: string[];
77
      alert_trigger_preferences?: string[];
78
      alert_trigger_preferences_value?: ("99.0" | "98.0" | "97.0")[];
79
      enabled?: string[];
80
      environment?: string[];
81
      event?: string[];
82
      event_source?: string[];
83
      event_type?: string[];
84
      group_by?: string[];
85
      health_check_id?: string[];
86
      incident_impact?: (
87
        | "INCIDENT_IMPACT_NONE"
88
        | "INCIDENT_IMPACT_MINOR"
89
        | "INCIDENT_IMPACT_MAJOR"
90
        | "INCIDENT_IMPACT_CRITICAL"
91
      )[];
92
      input_id?: string[];
93
      limit?: string[];
94
      logo_tag?: string[];
95
      megabits_per_second?: string[];
96
      new_health?: string[];
97
      new_status?: string[];
98
      packets_per_second?: string[];
99
      pool_id?: string[];
100
      product?: string[];
101
      project_id?: string[];
102
      protocol?: string[];
103
      query_tag?: string[];
104
      requests_per_second?: string[];
105
      selectors?: string[];
106
      services?: string[];
107
      slo?: string[];
108
      status?: string[];
109
      target_hostname?: string[];
110
      target_ip?: string[];
111
      target_zone_name?: string[];
112
      traffic_exclusions?: "security_events"[];
113
      tunnel_id?: string[];
114
      tunnel_name?: string[];
115
      where?: string[];
116
      zones?: string[];
117
      [k: string]: unknown;
118
    };
119
    mechanisms: {
120
      [k: string]: { id?: string | string; [k: string]: unknown }[];
121
    };
122
    name: string;
123
    [k: string]: unknown;
124
  }
125
) {
126
  const url = new URL(
127
    `https://api.cloudflare.com/client/v4/accounts/${account_id}/alerting/v3/policies`
128
  );
129

130
  const response = await fetch(url, {
131
    method: "POST",
132
    headers: {
133
      "X-AUTH-EMAIL": auth.email,
134
      "X-AUTH-KEY": auth.key,
135
      "Content-Type": "application/json",
136
      Authorization: "Bearer " + auth.token,
137
    },
138
    body: JSON.stringify(body),
139
  });
140
  if (!response.ok) {
141
    const text = await response.text();
142
    throw new Error(`${response.status} ${text}`);
143
  }
144
  return await response.json();
145
}
146