Update catch-all rule

Enable or disable catch-all routing rule, or change action to forward to specific destination address.

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 catch-all rule
8
 * Enable or disable catch-all routing rule, or change action to forward to specific destination address.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    actions: {
15
      type: "drop" | "forward" | "worker";
16
      value?: string[];
17
      [k: string]: unknown;
18
    }[];
19
    enabled?: true | false;
20
    matchers: { type: "all"; [k: string]: unknown }[];
21
    name?: string;
22
    [k: string]: unknown;
23
  }
24
) {
25
  const url = new URL(
26
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/email/routing/rules/catch_all`
27
  );
28

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