Create Custom Hostname

Add a new custom hostname and request that an SSL certificate be issued for it.

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
 * Create Custom Hostname
8
 * Add a new custom hostname and request that an SSL certificate be issued for it.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    custom_metadata?: { key?: string; [k: string]: unknown };
15
    hostname: string;
16
    ssl: {
17
      bundle_method?: "ubiquitous" | "optimal" | "force";
18
      certificate_authority?: "digicert" | "google" | "lets_encrypt";
19
      custom_certificate?: string;
20
      custom_key?: string;
21
      method?: "http" | "txt" | "email";
22
      settings?: {
23
        ciphers?: string[];
24
        early_hints?: "on" | "off";
25
        http2?: "on" | "off";
26
        min_tls_version?: "1.0" | "1.1" | "1.2" | "1.3";
27
        tls_1_3?: "on" | "off";
28
        [k: string]: unknown;
29
      };
30
      type?: "dv";
31
      wildcard?: boolean;
32
      [k: string]: unknown;
33
    };
34
    [k: string]: unknown;
35
  }
36
) {
37
  const url = new URL(
38
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/custom_hostnames`
39
  );
40

41
  const response = await fetch(url, {
42
    method: "POST",
43
    headers: {
44
      "X-AUTH-EMAIL": auth.email,
45
      "X-AUTH-KEY": auth.key,
46
      "Content-Type": "application/json",
47
      Authorization: "Bearer " + auth.token,
48
    },
49
    body: JSON.stringify(body),
50
  });
51
  if (!response.ok) {
52
    const text = await response.text();
53
    throw new Error(`${response.status} ${text}`);
54
  }
55
  return await response.json();
56
}
57