0

List Snapshots for a Droplet

by
Published Dec 20, 2024

To retrieve the snapshots that have been created from a Droplet, send a GET request to `/v2/droplets/$DROPLET_ID/snapshots`. You will get back a JSON object that has a `snapshots` key. This will be set to an array of snapshot objects, each of which contain the standard Droplet snapshot 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
 * List Snapshots for a Droplet
7
 * To retrieve the snapshots that have been created from a Droplet, send a GET
8
request to `/v2/droplets/$DROPLET_ID/snapshots`.
9

10
You will get back a JSON object that has a `snapshots` key. This will be set
11
to an array of snapshot objects, each of which contain the standard Droplet
12
snapshot attributes.
13

14
 */
15
export async function main(
16
  auth: Digitalocean,
17
  droplet_id: string,
18
  per_page: string | undefined,
19
  page: string | undefined,
20
) {
21
  const url = new URL(
22
    `https://api.digitalocean.com/v2/droplets/${droplet_id}/snapshots`,
23
  );
24
  for (const [k, v] of [
25
    ["per_page", per_page],
26
    ["page", page],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45