1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Events |
7 | * Get all events in an account |
8 |
|
9 | Requests can be sorted by the following fields: |
10 | `datetime`, `timestamp` |
11 |
|
12 | Returns a maximum of 200 events per page.*Rate limits*:Burst: `350/s`Steady: `3500/m` |
13 |
|
14 | */ |
15 | export async function main( |
16 | auth: Klaviyo, |
17 | fields_event_: string | undefined, |
18 | fields_metric_: string | undefined, |
19 | fields_profile_: string | undefined, |
20 | filter: string | undefined, |
21 | include: string | undefined, |
22 | page_cursor_: string | undefined, |
23 | sort: "datetime" | "-datetime" | "timestamp" | "-timestamp" | undefined, |
24 | revision: string, |
25 | ) { |
26 | const url = new URL(`https://a.klaviyo.com/api/events`); |
27 | for (const [k, v] of [ |
28 | ["fields[event]", fields_event_], |
29 | ["fields[metric]", fields_metric_], |
30 | ["fields[profile]", fields_profile_], |
31 | ["filter", filter], |
32 | ["include", include], |
33 | ["page[cursor]", page_cursor_], |
34 | ["sort", sort], |
35 | ]) { |
36 | if (v !== undefined && v !== "" && k !== undefined) { |
37 | url.searchParams.append(k, v); |
38 | } |
39 | } |
40 | const response = await fetch(url, { |
41 | method: "GET", |
42 | headers: { |
43 | revision: revision, |
44 | "Accept": "application/vnd.api+json", |
45 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
46 | }, |
47 | body: undefined, |
48 | }); |
49 | if (!response.ok) { |
50 | const text = await response.text(); |
51 | throw new Error(`${response.status} ${text}`); |
52 | } |
53 | return await response.json(); |
54 | } |
55 |
|