0

Create Back In Stock Subscription

by
Published Apr 8, 2025

Subscribe a profile to receive back in stock notifications.

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 Back In Stock Subscription
7
 * Subscribe a profile to receive back in stock notifications.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  revision: string,
12
  body: {
13
    data: {
14
      type: "back-in-stock-subscription";
15
      attributes: {
16
        channels: "EMAIL" | "PUSH" | "SMS"[];
17
        profile?: {
18
          data: {
19
            type: "profile";
20
            id?: string;
21
            attributes: {
22
              email?: string;
23
              phone_number?: string;
24
              external_id?: string;
25
              anonymous_id?: string;
26
            };
27
          };
28
        };
29
      };
30
      relationships?: {
31
        variant?: { data?: { type: "catalog-variant"; id: string } };
32
      };
33
    };
34
  },
35
) {
36
  const url = new URL(`https://a.klaviyo.com/api/back-in-stock-subscriptions`);
37

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