0

Export the recipients of an email campaign

by
Published Apr 8, 2025
Script brevo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Brevo = {
3
  apiKey: string;
4
};
5
/**
6
 * Export the recipients of an email campaign
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  campaignId: string,
12
  body: {
13
    notifyURL?: string;
14
    recipientsType:
15
      | "all"
16
      | "nonClickers"
17
      | "nonOpeners"
18
      | "clickers"
19
      | "openers"
20
      | "softBounces"
21
      | "hardBounces"
22
      | "unsubscribed";
23
  },
24
) {
25
  const url = new URL(
26
    `https://api.brevo.com/v3/emailCampaigns/${campaignId}/exportRecipients`,
27
  );
28

29
  const response = await fetch(url, {
30
    method: "POST",
31
    headers: {
32
      "Content-Type": "application/json",
33
      "api-key": auth.apiKey,
34
    },
35
    body: JSON.stringify(body),
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43