0

Query Segment Values

by
Published Apr 8, 2025

Returns the requested segment analytics values data.*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d` **Scopes:** `segments: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
 * Query Segment Values
7
 * Returns the requested segment analytics values data.*Rate limits*:Burst: `1/s`Steady: `2/m`Daily: `225/d`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  revision: string,
13
  body: {
14
    data: {
15
      type: "segment-values-report";
16
      attributes: {
17
        statistics:
18
          | "members_added"
19
          | "members_removed"
20
          | "net_members_changed"
21
          | "total_members"[];
22
        timeframe:
23
          | {
24
              key:
25
                | "last_12_months"
26
                | "last_30_days"
27
                | "last_365_days"
28
                | "last_3_months"
29
                | "last_7_days"
30
                | "last_90_days"
31
                | "last_month"
32
                | "last_week"
33
                | "last_year"
34
                | "this_month"
35
                | "this_week"
36
                | "this_year"
37
                | "today"
38
                | "yesterday";
39
            }
40
          | { start: string; end: string };
41
        filter?: string;
42
      };
43
    };
44
  },
45
) {
46
  const url = new URL(`https://a.klaviyo.com/api/segment-values-reports`);
47

48
  const response = await fetch(url, {
49
    method: "POST",
50
    headers: {
51
      revision: revision,
52
      "Accept": "application/vnd.api+json",
53
      "Content-Type": "application/vnd.api+json",
54
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
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