0

Create Zone

by
Published today

Add a domain to the Cloudflare account. The zone stays pending until its nameservers point to Cloudflare.

Script cloudflare Verified

The script

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

3
/**
4
 * Create Zone
5
 * Add a domain to the Cloudflare account. The zone stays pending until its nameservers point to Cloudflare.
6
 */
7
export async function main(
8
  auth: RT.Cloudflare,
9
  name: string,
10
  type: "full" | "partial" | "secondary" | undefined,
11
  jump_start: boolean | undefined
12
) {
13
  const response = await fetch("https://api.cloudflare.com/client/v4/zones", {
14
    method: "POST",
15
    headers: {
16
      Authorization: `Bearer ${auth.token}`,
17
      "Content-Type": "application/json",
18
      Accept: "application/json",
19
    },
20
    body: JSON.stringify({
21
      name,
22
      account: { id: auth.account_id },
23
      type: type || "full",
24
      jump_start: jump_start ?? undefined,
25
    }),
26
  })
27

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

32
  return await response.json()
33
}
34