0

Create a New Tag

by
Published Dec 20, 2024

To create a tag you can send a POST request to `/v2/tags` with a `name` attribute.

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 Tag
7
 * To create a tag you can send a POST request to `/v2/tags` with a `name` attribute.
8
 */
9
export async function main(
10
  auth: Digitalocean,
11
  body: {
12
    name?: string;
13
    resources?: { count?: number; last_tagged_uri?: string } & {
14
      droplets?: { count?: number; last_tagged_uri?: string };
15
      imgages?: { count?: number; last_tagged_uri?: string };
16
      volumes?: { count?: number; last_tagged_uri?: string };
17
      volume_snapshots?: { count?: number; last_tagged_uri?: string };
18
      databases?: { count?: number; last_tagged_uri?: string };
19
    };
20
  },
21
) {
22
  const url = new URL(`https://api.digitalocean.com/v2/tags`);
23

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