0

Initiate an Image Action

by
Published Dec 20, 2024

The following actions are available on an Image. ## Convert an Image to a Snapshot To convert an image, for example, a backup to a snapshot, send a POST request to `/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `convert`. ## Transfer an Image To transfer an image to another region, send a POST request to `/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `transfer` and set `region` attribute to the slug identifier of the region you wish to transfer to.

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Initiate an Image Action
7
 * The following actions are available on an Image.
8

9
## Convert an Image to a Snapshot
10

11
To convert an image, for example, a backup to a snapshot, send a POST request
12
to `/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `convert`.
13

14
## Transfer an Image
15

16
To transfer an image to another region, send a POST request to
17
`/v2/images/$IMAGE_ID/actions`. Set the `type` attribute to `transfer` and set
18
`region` attribute to the slug identifier of the region you wish to transfer
19
to.
20

21
 */
22
export async function main(
23
  auth: Digitalocean,
24
  image_id: string,
25
  body:
26
    | { type: "convert" | "transfer" }
27
    | ({ type: "convert" | "transfer" } & {
28
        region:
29
          | "ams1"
30
          | "ams2"
31
          | "ams3"
32
          | "blr1"
33
          | "fra1"
34
          | "lon1"
35
          | "nyc1"
36
          | "nyc2"
37
          | "nyc3"
38
          | "sfo1"
39
          | "sfo2"
40
          | "sfo3"
41
          | "sgp1"
42
          | "tor1"
43
          | "syd1";
44
      }),
45
) {
46
  const url = new URL(
47
    `https://api.digitalocean.com/v2/images/${image_id}/actions`,
48
  );
49

50
  const response = await fetch(url, {
51
    method: "POST",
52
    headers: {
53
      "Content-Type": "application/json",
54
      Authorization: "Bearer " + auth.token,
55
    },
56
    body: JSON.stringify(body),
57
  });
58
  if (!response.ok) {
59
    const text = await response.text();
60
    throw new Error(`${response.status} ${text}`);
61
  }
62
  return await response.json();
63
}
64