0

Get Coupon Code

by
Published Apr 8, 2025

Returns a Coupon Code specified by the given identifier.*Rate limits*:Burst: `350/s`Steady: `3500/m` **Scopes:** `coupon-codes: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 Coupon Code
7
 * Returns a Coupon Code specified by the given identifier.*Rate limits*:Burst: `350/s`Steady: `3500/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  id: string,
13
  fields_coupon_code_: string | undefined,
14
  fields_coupon_: string | undefined,
15
  include: string | undefined,
16
  revision: string,
17
) {
18
  const url = new URL(`https://a.klaviyo.com/api/coupon-codes/${id}`);
19
  for (const [k, v] of [
20
    ["fields[coupon-code]", fields_coupon_code_],
21
    ["fields[coupon]", fields_coupon_],
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