Import DNS Records

You can upload your [BIND config](https://en.wikipedia.org/wiki/Zone_file "Zone file") through this endpoint. It assumes that cURL is called from a location with bind_config.txt (valid BIND config) present. See [the documentation](https://developers.cloudflare.com/dns/manage-dns-records/how-to/import-and-export/ "Import and export records") for more information.

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
 * Import DNS Records
8
 * You can upload your [BIND config](https://en.wikipedia.org/wiki/Zone_file "Zone file") through this endpoint. It assumes that cURL is called from a location with bind_config.txt (valid BIND config) present.
9

10
See [the documentation](https://developers.cloudflare.com/dns/manage-dns-records/how-to/import-and-export/ "Import and export records") for more information.
11
 */
12
export async function main(
13
  auth: Cloudflare,
14
  zone_identifier: string,
15
  body: { file: string; proxied?: string; [k: string]: unknown }
16
) {
17
  const url = new URL(
18
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/dns_records/import`
19
  );
20

21
  const formData = new FormData();
22
  for (const [k, v] of Object.entries(body)) {
23
    if (v !== undefined && v !== "") {
24
      formData.append(k, String(v));
25
    }
26
  }
27
  const response = await fetch(url, {
28
    method: "POST",
29
    headers: {
30
      "X-AUTH-EMAIL": auth.email,
31
      "X-AUTH-KEY": auth.key,
32
      Authorization: "Bearer " + auth.token,
33
    },
34
    body: formData,
35
  });
36
  if (!response.ok) {
37
    const text = await response.text();
38
    throw new Error(`${response.status} ${text}`);
39
  }
40
  return await response.json();
41
}
42