Edit Custom Hostname

Modify SSL configuration for a custom hostname. When sent with SSL config that matches existing config, used to indicate that hostname should pass domain control validation (DCV). Can also be used to change validation type, e.g., from 'http' to 'email'.

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
 * Edit Custom Hostname
8
 * Modify SSL configuration for a custom hostname. When sent with SSL config that matches existing config, used to indicate that hostname should pass domain control validation (DCV). Can also be used to change validation type, e.g., from 'http' to 'email'.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  identifier: string,
13
  zone_identifier: string,
14
  body: {
15
    custom_metadata?: { key?: string; [k: string]: unknown };
16
    custom_origin_server?: string;
17
    custom_origin_sni?: string;
18
    ssl?: {
19
      bundle_method?: "ubiquitous" | "optimal" | "force";
20
      certificate_authority?: "digicert" | "google" | "lets_encrypt";
21
      custom_certificate?: string;
22
      custom_key?: string;
23
      method?: "http" | "txt" | "email";
24
      settings?: {
25
        ciphers?: string[];
26
        early_hints?: "on" | "off";
27
        http2?: "on" | "off";
28
        min_tls_version?: "1.0" | "1.1" | "1.2" | "1.3";
29
        tls_1_3?: "on" | "off";
30
        [k: string]: unknown;
31
      };
32
      type?: "dv";
33
      wildcard?: boolean;
34
      [k: string]: unknown;
35
    };
36
    [k: string]: unknown;
37
  }
38
) {
39
  const url = new URL(
40
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/custom_hostnames/${identifier}`
41
  );
42

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