List an account ruleset version's rules by tag

Fetches the rules of a managed account ruleset version for a given tag.

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
 * List an account ruleset version's rules by tag
8
 * Fetches the rules of a managed account ruleset version for a given tag.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  rule_tag: string,
13
  ruleset_version: string,
14
  ruleset_id: string,
15
  account_id: string
16
) {
17
  const url = new URL(
18
    `https://api.cloudflare.com/client/v4/accounts/${account_id}/rulesets/${ruleset_id}/versions/${ruleset_version}/by_tag/${rule_tag}`
19
  );
20

21
  const response = await fetch(url, {
22
    method: "GET",
23
    headers: {
24
      "X-AUTH-EMAIL": auth.email,
25
      "X-AUTH-KEY": auth.key,
26
      Authorization: "Bearer " + auth.token,
27
    },
28
    body: undefined,
29
  });
30
  if (!response.ok) {
31
    const text = await response.text();
32
    throw new Error(`${response.status} ${text}`);
33
  }
34
  return await response.json();
35
}
36