0

Create a New CDN Endpoint

by
Published Dec 20, 2024

To create a new CDN endpoint, send a POST request to `/v2/cdn/endpoints`. The origin attribute must be set to the fully qualified domain name (FQDN) of a DigitalOcean Space. Optionally, the TTL may be configured by setting the `ttl` attribute. A custom subdomain may be configured by specifying the `custom_domain` and `certificate_id` attributes.

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 CDN Endpoint
7
 * To create a new CDN endpoint, send a POST request to `/v2/cdn/endpoints`. The
8
origin attribute must be set to the fully qualified domain name (FQDN) of a
9
DigitalOcean Space. Optionally, the TTL may be configured by setting the `ttl`
10
attribute.
11

12
A custom subdomain may be configured by specifying the `custom_domain` and
13
`certificate_id` attributes.
14

15
 */
16
export async function main(
17
  auth: Digitalocean,
18
  body: {
19
    id?: string;
20
    origin: string;
21
    endpoint?: string;
22
    ttl?: 60 | 600 | 3600 | 86400 | 604800;
23
    certificate_id?: string;
24
    custom_domain?: string;
25
    created_at?: string;
26
  },
27
) {
28
  const url = new URL(`https://api.digitalocean.com/v2/cdn/endpoints`);
29

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