1 | |
2 | type Klaviyo = { |
3 | apiKey: string; |
4 | }; |
5 | |
6 | * Get Coupon Codes |
7 | * Gets a list of coupon codes associated with a coupon/coupons or a profile/profiles. |
8 |
|
9 | A coupon/coupons or a profile/profiles must be provided as required filter params.*Rate limits*:Burst: `350/s`Steady: `3500/m` |
10 |
|
11 | */ |
12 | export async function main( |
13 | auth: Klaviyo, |
14 | fields_coupon_code_: string | undefined, |
15 | fields_coupon_: string | undefined, |
16 | filter: string | undefined, |
17 | include: string | undefined, |
18 | page_cursor_: string | undefined, |
19 | revision: string, |
20 | ) { |
21 | const url = new URL(`https://a.klaviyo.com/api/coupon-codes`); |
22 | for (const [k, v] of [ |
23 | ["fields[coupon-code]", fields_coupon_code_], |
24 | ["fields[coupon]", fields_coupon_], |
25 | ["filter", filter], |
26 | ["include", include], |
27 | ["page[cursor]", page_cursor_], |
28 | ]) { |
29 | if (v !== undefined && v !== "" && k !== undefined) { |
30 | url.searchParams.append(k, v); |
31 | } |
32 | } |
33 | const response = await fetch(url, { |
34 | method: "GET", |
35 | headers: { |
36 | revision: revision, |
37 | "Accept": "application/vnd.api+json", |
38 | Authorization: "Klaviyo-API-Key " + auth.apiKey, |
39 | }, |
40 | body: undefined, |
41 | }); |
42 | if (!response.ok) { |
43 | const text = await response.text(); |
44 | throw new Error(`${response.status} ${text}`); |
45 | } |
46 | return await response.json(); |
47 | } |
48 |
|