0

Bulk Unsubscribe Profiles

by
Published Apr 8, 2025

Unsubscribe one or more profiles to email marketing, SMS marketing, or both.

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 Unsubscribe Profiles
7
 * Unsubscribe one or more profiles to email marketing, SMS marketing, or both.
8
 */
9
export async function main(
10
  auth: Klaviyo,
11
  revision: string,
12
  body: {
13
    data: {
14
      type: "profile-subscription-bulk-delete-job";
15
      attributes: {
16
        profiles: {
17
          data: {
18
            type: "profile";
19
            attributes: {
20
              email?: string;
21
              phone_number?: string;
22
              subscriptions?: {
23
                email?: { marketing: { consent: "UNSUBSCRIBED" } };
24
                sms?: {
25
                  marketing?: { consent: "UNSUBSCRIBED" };
26
                  transactional?: { consent: "UNSUBSCRIBED" };
27
                };
28
              };
29
            };
30
          }[];
31
        };
32
      };
33
      relationships?: { list?: { data?: { type: "list"; id: string } } };
34
    };
35
  },
36
) {
37
  const url = new URL(
38
    `https://a.klaviyo.com/api/profile-subscription-bulk-delete-jobs`,
39
  );
40

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