0

Update Coupon Code

by
Published Apr 8, 2025

Updates a coupon code specified by the given identifier synchronously. We allow updating the 'status' and 'expires_at' of coupon codes.*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
 * Update Coupon Code
7
 * Updates a coupon code specified by the given identifier synchronously. We allow updating the 'status' and
8
'expires_at' of coupon codes.*Rate limits*:Burst: `350/s`Steady: `3500/m`
9

10
 */
11
export async function main(
12
  auth: Klaviyo,
13
  id: string,
14
  revision: string,
15
  body: {
16
    data: {
17
      type: "coupon-code";
18
      id: string;
19
      attributes: {
20
        status?:
21
          | "ASSIGNED_TO_PROFILE"
22
          | "DELETING"
23
          | "PROCESSING"
24
          | "UNASSIGNED"
25
          | "USED"
26
          | "VERSION_NOT_ACTIVE";
27
        expires_at?: string;
28
      };
29
    };
30
  },
31
) {
32
  const url = new URL(`https://a.klaviyo.com/api/coupon-codes/${id}`);
33

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