List DNS Records

List, search, sort, and filter a zones' DNS records.

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 DNS Records
8
 * List, search, sort, and filter a zones' DNS records.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  name: string | undefined,
14
  type:
15
    | "A"
16
    | "AAAA"
17
    | "CAA"
18
    | "CERT"
19
    | "CNAME"
20
    | "DNSKEY"
21
    | "DS"
22
    | "HTTPS"
23
    | "LOC"
24
    | "MX"
25
    | "NAPTR"
26
    | "NS"
27
    | "PTR"
28
    | "SMIMEA"
29
    | "SRV"
30
    | "SSHFP"
31
    | "SVCB"
32
    | "TLSA"
33
    | "TXT"
34
    | "URI"
35
    | undefined,
36
  content: string | undefined,
37
  proxied: string | undefined,
38
  match: "any" | "all" | undefined,
39
  comment: string | undefined,
40
  comment_present: string | undefined,
41
  comment_absent: string | undefined,
42
  comment_exact: string | undefined,
43
  comment_contains: string | undefined,
44
  comment_startswith: string | undefined,
45
  comment_endswith: string | undefined,
46
  tag: string | undefined,
47
  tag_present: string | undefined,
48
  tag_absent: string | undefined,
49
  tag_exact: string | undefined,
50
  tag_contains: string | undefined,
51
  tag_startswith: string | undefined,
52
  tag_endswith: string | undefined,
53
  search: string | undefined,
54
  tag_match: "any" | "all" | undefined,
55
  page: string | undefined,
56
  per_page: string | undefined,
57
  order: "type" | "name" | "content" | "ttl" | "proxied" | undefined,
58
  direction: "asc" | "desc" | undefined
59
) {
60
  const url = new URL(
61
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/dns_records`
62
  );
63
  for (const [k, v] of [
64
    ["name", name],
65
    ["type", type],
66
    ["content", content],
67
    ["proxied", proxied],
68
    ["match", match],
69
    ["comment", comment],
70
    ["comment.present", comment_present],
71
    ["comment.absent", comment_absent],
72
    ["comment.exact", comment_exact],
73
    ["comment.contains", comment_contains],
74
    ["comment.startswith", comment_startswith],
75
    ["comment.endswith", comment_endswith],
76
    ["tag", tag],
77
    ["tag.present", tag_present],
78
    ["tag.absent", tag_absent],
79
    ["tag.exact", tag_exact],
80
    ["tag.contains", tag_contains],
81
    ["tag.startswith", tag_startswith],
82
    ["tag.endswith", tag_endswith],
83
    ["search", search],
84
    ["tag_match", tag_match],
85
    ["page", page],
86
    ["per_page", per_page],
87
    ["order", order],
88
    ["direction", direction],
89
  ]) {
90
    if (v !== undefined && v !== "") {
91
      url.searchParams.append(k, v);
92
    }
93
  }
94
  const response = await fetch(url, {
95
    method: "GET",
96
    headers: {
97
      "X-AUTH-EMAIL": auth.email,
98
      "X-AUTH-KEY": auth.key,
99
      Authorization: "Bearer " + auth.token,
100
    },
101
    body: undefined,
102
  });
103
  if (!response.ok) {
104
    const text = await response.text();
105
    throw new Error(`${response.status} ${text}`);
106
  }
107
  return await response.json();
108
}
109