0

Bulk Create Coupon Codes

by
Published Apr 8, 2025

Create a coupon-code-bulk-create-job to bulk create a list of coupon codes. Max number of coupon codes per job we allow for is 1000. Max number of jobs queued at once we allow for is 100.*Rate limits*:Burst: `75/s`Steady: `700/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
 * Bulk Create Coupon Codes
7
 * Create a coupon-code-bulk-create-job to bulk create a list of coupon codes.
8

9
Max number of coupon codes per job we allow for is 1000.
10
Max number of jobs queued at once we allow for is 100.*Rate limits*:Burst: `75/s`Steady: `700/m`
11

12
 */
13
export async function main(
14
  auth: Klaviyo,
15
  revision: string,
16
  body: {
17
    data: {
18
      type: "coupon-code-bulk-create-job";
19
      attributes: {
20
        "coupon-codes": {
21
          data: {
22
            type: "coupon-code";
23
            attributes: { unique_code: string; expires_at?: string };
24
            relationships?: {
25
              coupon?: { data?: { type: "coupon"; id: string } };
26
            };
27
          }[];
28
        };
29
      };
30
    };
31
  },
32
) {
33
  const url = new URL(`https://a.klaviyo.com/api/coupon-code-bulk-create-jobs`);
34

35
  const response = await fetch(url, {
36
    method: "POST",
37
    headers: {
38
      revision: revision,
39
      "Accept": "application/vnd.api+json",
40
      "Content-Type": "application/vnd.api+json",
41
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
42
    },
43
    body: JSON.stringify(body),
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51