0

Create a New Certificate

by
Published Dec 20, 2024

To upload new SSL certificate which you have previously generated, send a POST request to `/v2/certificates`. When uploading a user-generated certificate, the `private_key`, `leaf_certificate`, and optionally the `certificate_chain` attributes should be provided. The type must be set to `custom`. When using Let's Encrypt to create a certificate, the `dns_names` attribute must be provided, and the type must be set to `lets_encrypt`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Create a New Certificate
7
 * To upload new SSL certificate which you have previously generated, send a POST
8
request to `/v2/certificates`.
9

10
When uploading a user-generated certificate, the `private_key`,
11
`leaf_certificate`, and optionally the `certificate_chain` attributes should
12
be provided. The type must be set to `custom`.
13

14
When using Let's Encrypt to create a certificate, the `dns_names` attribute
15
must be provided, and the type must be set to `lets_encrypt`.
16

17
 */
18
export async function main(
19
  auth: Digitalocean,
20
  body:
21
    | ({ name: string; type?: "custom" | "lets_encrypt" } & {
22
        dns_names: string[];
23
      })
24
    | ({ name: string; type?: "custom" | "lets_encrypt" } & {
25
        private_key: string;
26
        leaf_certificate: string;
27
        certificate_chain?: string;
28
      }),
29
) {
30
  const url = new URL(`https://api.digitalocean.com/v2/certificates`);
31

32
  const response = await fetch(url, {
33
    method: "POST",
34
    headers: {
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