0

Get your transactional email activity aggregated per day

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
 * Get your transactional email activity aggregated per day
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  limit: string | undefined,
12
  offset: string | undefined,
13
  startDate: string | undefined,
14
  endDate: string | undefined,
15
  days: string | undefined,
16
  tag: string | undefined,
17
  sort: "asc" | "desc" | undefined,
18
) {
19
  const url = new URL(`https://api.brevo.com/v3/smtp/statistics/reports`);
20
  for (const [k, v] of [
21
    ["limit", limit],
22
    ["offset", offset],
23
    ["startDate", startDate],
24
    ["endDate", endDate],
25
    ["days", days],
26
    ["tag", tag],
27
    ["sort", sort],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      "api-key": auth.apiKey,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46