0

Create Coupon

by
Published Apr 8, 2025

Creates a new coupon.*Rate limits*:Burst: `3/s`Steady: `60/m` **Scopes:** `coupons:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Create Coupon
7
 * Creates a new coupon.*Rate limits*:Burst: `3/s`Steady: `60/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  revision: string,
13
  body: {
14
    data: {
15
      type: "coupon";
16
      attributes: { external_id: string; description?: string };
17
    };
18
  },
19
) {
20
  const url = new URL(`https://a.klaviyo.com/api/coupons`);
21

22
  const response = await fetch(url, {
23
    method: "POST",
24
    headers: {
25
      revision: revision,
26
      "Accept": "application/vnd.api+json",
27
      "Content-Type": "application/vnd.api+json",
28
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
29
    },
30
    body: JSON.stringify(body),
31
  });
32
  if (!response.ok) {
33
    const text = await response.text();
34
    throw new Error(`${response.status} ${text}`);
35
  }
36
  return await response.json();
37
}
38