0
Create Zone
One script reply has been approved by the moderators Verified
Created by hugo697 174 days ago Viewed 5854 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 174 days ago
1
type Cloudflare = {
2
  token: string;
3
  email: string;
4
  key: string;
5
};
6
/**
7
 * Create Zone
8
 *
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  body: {
13
    account: { id?: string; [k: string]: unknown };
14
    name: string;
15
    type?: "full" | "partial" | "secondary";
16
    [k: string]: unknown;
17
  }
18
) {
19
  const url = new URL(`https://api.cloudflare.com/client/v4/zones`);
20

21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      "X-AUTH-EMAIL": auth.email,
25
      "X-AUTH-KEY": auth.key,
26
      "Content-Type": "application/json",
27
      Authorization: "Bearer " + auth.token,
28
    },
29
    body: JSON.stringify(body),
30
  });
31
  if (!response.ok) {
32
    const text = await response.text();
33
    throw new Error(`${response.status} ${text}`);
34
  }
35
  return await response.json();
36
}
37