List Custom Hostnames

List, search, sort, and filter all of your custom hostnames.

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 Custom Hostnames
8
 * List, search, sort, and filter all of your custom hostnames.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  hostname: string | undefined,
14
  id: string | undefined,
15
  page: string | undefined,
16
  per_page: string | undefined,
17
  order: "ssl" | "ssl_status" | undefined,
18
  direction: "asc" | "desc" | undefined,
19
  ssl: "0" | "1" | undefined
20
) {
21
  const url = new URL(
22
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/custom_hostnames`
23
  );
24
  for (const [k, v] of [
25
    ["hostname", hostname],
26
    ["id", id],
27
    ["page", page],
28
    ["per_page", per_page],
29
    ["order", order],
30
    ["direction", direction],
31
    ["ssl", ssl],
32
  ]) {
33
    if (v !== undefined && v !== "") {
34
      url.searchParams.append(k, v);
35
    }
36
  }
37
  const response = await fetch(url, {
38
    method: "GET",
39
    headers: {
40
      "X-AUTH-EMAIL": auth.email,
41
      "X-AUTH-KEY": auth.key,
42
      Authorization: "Bearer " + auth.token,
43
    },
44
    body: undefined,
45
  });
46
  if (!response.ok) {
47
    const text = await response.text();
48
    throw new Error(`${response.status} ${text}`);
49
  }
50
  return await response.json();
51
}
52