Update routing rule

Update actions and matches, or enable/disable specific routing rules.

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 routing rule
8
 * Update actions and matches, or enable/disable specific routing rules.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  rule_identifier: string,
13
  zone_identifier: string,
14
  body: {
15
    actions: {
16
      type: "drop" | "forward" | "worker";
17
      value: string[];
18
      [k: string]: unknown;
19
    }[];
20
    enabled?: true | false;
21
    matchers: {
22
      field: "to";
23
      type: "literal";
24
      value: string;
25
      [k: string]: unknown;
26
    }[];
27
    name?: string;
28
    priority?: number;
29
    [k: string]: unknown;
30
  }
31
) {
32
  const url = new URL(
33
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/email/routing/rules/${rule_identifier}`
34
  );
35

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