Update a WAF rule

Updates a WAF rule. You can only update the mode/action of the rule. **Note:** Applies only to the [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).

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
 * Update a WAF rule
8
 * Updates a WAF rule. You can only update the mode/action of the rule.
9

10
**Note:** Applies only to the [previous version of WAF managed rules](https://developers.cloudflare.com/support/firewall/managed-rules-web-application-firewall-waf/understanding-waf-managed-rules-web-application-firewall/).
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  identifier: string,
15
  package_id: string,
16
  zone_id: string,
17
  body: {
18
    mode?:
19
      | "default"
20
      | "disable"
21
      | "simulate"
22
      | "block"
23
      | "challenge"
24
      | "on"
25
      | "off";
26
    [k: string]: unknown;
27
  }
28
) {
29
  const url = new URL(
30
    `https://api.cloudflare.com/client/v4/zones/${zone_id}/firewall/waf/packages/${package_id}/rules/${identifier}`
31
  );
32

33
  const response = await fetch(url, {
34
    method: "PATCH",
35
    headers: {
36
      "X-AUTH-EMAIL": auth.email,
37
      "X-AUTH-KEY": auth.key,
38
      "Content-Type": "application/json",
39
      Authorization: "Bearer " + auth.token,
40
    },
41
    body: JSON.stringify(body),
42
  });
43
  if (!response.ok) {
44
    const text = await response.text();
45
    throw new Error(`${response.status} ${text}`);
46
  }
47
  return await response.json();
48
}
49