0

Add Profiles to List

by
Published Apr 8, 2025

Add a profile to a list with the given list ID. It is recommended that you use the [Subscribe Profiles endpoint](https://developers.klaviyo.com/en/reference/subscribe_profiles) if you're trying to give a profile [consent](https://developers.klaviyo.com/en/docs/collect_email_and_sms_consent_via_api) to receive email marketing, SMS marketing, or both. This endpoint accepts a maximum of 1000 profiles per call.*Rate limits*:Burst: `10/s`Steady: `150/m` **Scopes:** `lists:write` `profiles: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
 * Add Profiles to List
7
 * Add a profile to a list with the given list ID.
8

9
It is recommended that you use the [Subscribe Profiles endpoint](https://developers.klaviyo.com/en/reference/subscribe_profiles) if you're trying to give a profile [consent](https://developers.klaviyo.com/en/docs/collect_email_and_sms_consent_via_api) to receive email marketing, SMS marketing, or both.
10

11
This endpoint accepts a maximum of 1000 profiles per call.*Rate limits*:Burst: `10/s`Steady: `150/m`
12

13
 */
14
export async function main(
15
  auth: Klaviyo,
16
  id: string,
17
  revision: string,
18
  body: { data: { type: "profile"; id: string }[] },
19
) {
20
  const url = new URL(
21
    `https://a.klaviyo.com/api/lists/${id}/relationships/profiles`,
22
  );
23

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