0

List All Snapshots

by
Published Dec 20, 2024

To list all of the snapshots available on your account, send a GET request to `/v2/snapshots`.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * List All Snapshots
7
 * To list all of the snapshots available on your account, send a GET request to
8
`/v2/snapshots`.
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  per_page: string | undefined,
13
  page: string | undefined,
14
  resource_type: "droplet" | "volume" | undefined,
15
) {
16
  const url = new URL(`https://api.digitalocean.com/v2/snapshots`);
17
  for (const [k, v] of [
18
    ["per_page", per_page],
19
    ["page", page],
20
    ["resource_type", resource_type],
21
  ]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39