0

Get Coupons

by
Published Apr 8, 2025

Get all coupons in an account. To learn more, see our [Coupons API guide](https://developers.klaviyo.com/en/docs/use_klaviyos_coupons_api).*Rate limits*:Burst: `75/s`Steady: `700/m` **Scopes:** `coupons: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 Coupons
7
 * Get all coupons in an account.
8

9
To learn more, see our [Coupons API guide](https://developers.klaviyo.com/en/docs/use_klaviyos_coupons_api).*Rate limits*:Burst: `75/s`Steady: `700/m`
10

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