0

List Backups for a Droplet

by
Published Dec 20, 2024

To retrieve any backups associated with a Droplet, send a GET request to `/v2/droplets/$DROPLET_ID/backups`. You will get back a JSON object that has a `backups` key. This will be set to an array of backup objects, each of which contain the standard Droplet backup 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 Backups for a Droplet
7
 * To retrieve any backups associated with a Droplet, send a GET request to
8
`/v2/droplets/$DROPLET_ID/backups`.
9

10
You will get back a JSON object that has a `backups` key. This will be set to
11
an array of backup objects, each of which contain the standard
12
Droplet backup 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}/backups`,
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