0

Get Droplet Bandwidth Metrics

by
Published Dec 20, 2024

To retrieve bandwidth metrics for a given Droplet, send a GET request to `/v2/monitoring/metrics/droplet/bandwidth`. Use the `interface` query parameter to specify if the results should be for the `private` or `public` interface. Use the `direction` query parameter to specify if the results should be for `inbound` or `outbound` traffic. The metrics in the response body are in megabits per second (Mbps).

Script digitalocean Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Digitalocean = {
3
  token: string;
4
};
5
/**
6
 * Get Droplet Bandwidth Metrics
7
 * To retrieve bandwidth metrics for a given Droplet, send a GET request to `/v2/monitoring/metrics/droplet/bandwidth`. Use the `interface` query parameter to specify if the results should be for the `private` or `public` interface. Use the `direction` query parameter to specify if the results should be for `inbound` or `outbound` traffic.
8
The metrics in the response body are in megabits per second (Mbps).
9
 */
10
export async function main(
11
  auth: Digitalocean,
12
  host_id: string | undefined,
13
  interfaceType: "private" | "public" | undefined,
14
  direction: "inbound" | "outbound" | undefined,
15
  start: string | undefined,
16
  end: string | undefined,
17
) {
18
  const url = new URL(
19
    `https://api.digitalocean.com/v2/monitoring/metrics/droplet/bandwidth`,
20
  );
21
  for (const [k, v] of [
22
    ["host_id", host_id],
23
    ["interface", interfaceType],
24
    ["direction", direction],
25
    ["start", start],
26
    ["end", end],
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