0

List All Available Kernels for a Droplet

by
Published Dec 20, 2024

To retrieve a list of all kernels available to a Droplet, send a GET request to `/v2/droplets/$DROPLET_ID/kernels` The response will be a JSON object that has a key called `kernels`. This will be set to an array of `kernel` objects, each of which contain the standard `kernel` 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 All Available Kernels for a Droplet
7
 * To retrieve a list of all kernels available to a Droplet, send a GET request
8
to `/v2/droplets/$DROPLET_ID/kernels`
9

10
The response will be a JSON object that has a key called `kernels`. This will
11
be set to an array of `kernel` objects, each of which contain the standard
12
`kernel` 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}/kernels`,
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