0

Get Metric

by
Published Apr 8, 2025

Get a metric with the given metric ID.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `metrics:read`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Get Metric
7
 * Get a metric with the given metric ID.*Rate limits*:Burst: `10/s`Steady: `150/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  id: string,
13
  fields_flow_: string | undefined,
14
  fields_metric_: string | undefined,
15
  include: string | undefined,
16
  revision: string,
17
) {
18
  const url = new URL(`https://a.klaviyo.com/api/metrics/${id}`);
19
  for (const [k, v] of [
20
    ["fields[flow]", fields_flow_],
21
    ["fields[metric]", fields_metric_],
22
    ["include", include],
23
  ]) {
24
    if (v !== undefined && v !== "" && k !== undefined) {
25
      url.searchParams.append(k, v);
26
    }
27
  }
28
  const response = await fetch(url, {
29
    method: "GET",
30
    headers: {
31
      revision: revision,
32
      "Accept": "application/vnd.api+json",
33
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43