List IP Access rules

Fetches IP Access rules of an account. These rules apply to all the zones in the account. You can filter the results using several optional parameters.

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 IP Access rules
8
 * Fetches IP Access rules of an account. These rules apply to all the zones in the account. You can filter the results using several optional parameters.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  account_identifier: string,
13
  filters: string | undefined,
14
  egs_pagination_json: string | undefined,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  order: "configuration.target" | "configuration.value" | "mode" | undefined,
18
  direction: "asc" | "desc" | undefined
19
) {
20
  const url = new URL(
21
    `https://api.cloudflare.com/client/v4/accounts/${account_identifier}/firewall/access_rules/rules`
22
  );
23
  for (const [k, v] of [
24
    ["filters", filters],
25
    ["egs-pagination.json", egs_pagination_json],
26
    ["page", page],
27
    ["per_page", per_page],
28
    ["order", order],
29
    ["direction", direction],
30
  ]) {
31
    if (v !== undefined && v !== "") {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "GET",
37
    headers: {
38
      "X-AUTH-EMAIL": auth.email,
39
      "X-AUTH-KEY": auth.key,
40
      Authorization: "Bearer " + auth.token,
41
    },
42
    body: undefined,
43
  });
44
  if (!response.ok) {
45
    const text = await response.text();
46
    throw new Error(`${response.status} ${text}`);
47
  }
48
  return await response.json();
49
}
50