0

List Zones

by
Published today

List the zones (domains) the API token can access, optionally filtered by name or status.

Script cloudflare Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 3 hours ago
1
//native
2

3
/**
4
 * List Zones
5
 * List the zones (domains) the API token can access, optionally filtered by name or status.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  name: string | undefined,
10
  status: "initializing" | "pending" | "active" | "moved" | undefined,
11
  page: number | undefined,
12
  per_page: number | undefined
13
) {
14
  const url = new URL("https://api.cloudflare.com/client/v4/zones")
15
  for (const [k, v] of Object.entries({ name, status, page, per_page })) {
16
    if (v != null && v !== "") url.searchParams.append(k, String(v))
17
  }
18

19
  const response = await fetch(url, {
20
    headers: {
21
      Authorization: `Bearer ${auth.token}`,
22
      Accept: "application/json",
23
    },
24
  })
25

26
  if (!response.ok) {
27
    throw new Error(`${response.status} ${await response.text()}`)
28
  }
29

30
  return await response.json()
31
}
32