0

Return all your created email campaigns

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
 * Return all your created email campaigns
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  type: "classic" | "trigger" | undefined,
12
  status:
13
    | "suspended"
14
    | "archive"
15
    | "sent"
16
    | "queued"
17
    | "draft"
18
    | "inProcess"
19
    | undefined,
20
  statistics: "globalStats" | "linksStats" | "statsByDomain" | undefined,
21
  startDate: string | undefined,
22
  endDate: string | undefined,
23
  limit: string | undefined,
24
  offset: string | undefined,
25
  sort: "asc" | "desc" | undefined,
26
  excludeHtmlContent: "true" | "false" | undefined,
27
) {
28
  const url = new URL(`https://api.brevo.com/v3/emailCampaigns`);
29
  for (const [k, v] of [
30
    ["type", type],
31
    ["status", status],
32
    ["statistics", statistics],
33
    ["startDate", startDate],
34
    ["endDate", endDate],
35
    ["limit", limit],
36
    ["offset", offset],
37
    ["sort", sort],
38
    ["excludeHtmlContent", excludeHtmlContent],
39
  ]) {
40
    if (v !== undefined && v !== "" && k !== undefined) {
41
      url.searchParams.append(k, v);
42
    }
43
  }
44
  const response = await fetch(url, {
45
    method: "GET",
46
    headers: {
47
      "api-key": auth.apiKey,
48
    },
49
    body: undefined,
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