Create SSL Configuration

Upload a new SSL certificate for a zone.

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 SSL Configuration
8
 * Upload a new SSL certificate for a zone.
9
 */
10
export async function main(
11
  auth: Cloudflare,
12
  zone_identifier: string,
13
  body: {
14
    bundle_method?: "ubiquitous" | "optimal" | "force";
15
    certificate: string;
16
    geo_restrictions?: {
17
      label?: "us" | "eu" | "highest_security";
18
      [k: string]: unknown;
19
    };
20
    policy?: string;
21
    private_key: string;
22
    type?: "legacy_custom" | "sni_custom";
23
    [k: string]: unknown;
24
  }
25
) {
26
  const url = new URL(
27
    `https://api.cloudflare.com/client/v4/zones/${zone_identifier}/custom_certificates`
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "X-AUTH-EMAIL": auth.email,
34
      "X-AUTH-KEY": auth.key,
35
      "Content-Type": "application/json",
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46