0

Create Coupon Code

by
Published Apr 8, 2025

Synchronously creates a coupon code for the given coupon.*Rate limits*:Burst: `350/s`Steady: `3500/m` **Scopes:** `coupon-codes: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 Code
7
 * Synchronously creates a coupon code for the given coupon.*Rate limits*:Burst: `350/s`Steady: `3500/m`
8

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

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