0

Create IP Access Rule

by
Published 16 days ago

Block, challenge or allow an IP, IP range, ASN or country across every zone in the account.

Script cloudflare Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 17 days ago
1
//native
2

3
/**
4
 * Create IP Access Rule
5
 * Block, challenge or allow an IP, IP range, ASN or country across every zone in the account.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  mode:
10
    "block" | "challenge" | "js_challenge" | "managed_challenge" | "whitelist",
11
  target: "ip" | "ip6" | "ip_range" | "asn" | "country",
12
  value: string,
13
  notes: string | undefined
14
) {
15
  const response = await fetch(
16
    `https://api.cloudflare.com/client/v4/accounts/${auth.account_id}/firewall/access_rules/rules`,
17
    {
18
      method: "POST",
19
      headers: {
20
        Authorization: `Bearer ${auth.token}`,
21
        "Content-Type": "application/json",
22
        Accept: "application/json",
23
      },
24
      body: JSON.stringify({
25
        mode,
26
        configuration: { target, value },
27
        notes: notes ?? undefined,
28
      }),
29
    }
30
  )
31

32
  if (!response.ok) {
33
    throw new Error(`${response.status} ${await response.text()}`)
34
  }
35

36
  return await response.json()
37
}
38