0

Get all your transactional email activity (unaggregated events)

by
Published Apr 8, 2025

This endpoint will show the aggregated stats for past 30 days by default if `startDate` and `endDate` OR `days` is not passed. The date range can not exceed 90 days

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 all your transactional email activity (unaggregated events)
7
 * This endpoint will show the aggregated stats for past 30 days by default if `startDate` and `endDate` OR `days` is not passed. The date range can not exceed 90 days
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
  email: string | undefined,
17
  event:
18
    | "bounces"
19
    | "hardBounces"
20
    | "softBounces"
21
    | "delivered"
22
    | "spam"
23
    | "requests"
24
    | "opened"
25
    | "clicks"
26
    | "invalid"
27
    | "deferred"
28
    | "blocked"
29
    | "unsubscribed"
30
    | "error"
31
    | "loadedByProxy"
32
    | undefined,
33
  tags: string | undefined,
34
  messageId: string | undefined,
35
  templateId: string | undefined,
36
  sort: "asc" | "desc" | undefined,
37
) {
38
  const url = new URL(`https://api.brevo.com/v3/smtp/statistics/events`);
39
  for (const [k, v] of [
40
    ["limit", limit],
41
    ["offset", offset],
42
    ["startDate", startDate],
43
    ["endDate", endDate],
44
    ["days", days],
45
    ["email", email],
46
    ["event", event],
47
    ["tags", tags],
48
    ["messageId", messageId],
49
    ["templateId", templateId],
50
    ["sort", sort],
51
  ]) {
52
    if (v !== undefined && v !== "" && k !== undefined) {
53
      url.searchParams.append(k, v);
54
    }
55
  }
56
  const response = await fetch(url, {
57
    method: "GET",
58
    headers: {
59
      "api-key": auth.apiKey,
60
    },
61
    body: undefined,
62
  });
63
  if (!response.ok) {
64
    const text = await response.text();
65
    throw new Error(`${response.status} ${text}`);
66
  }
67
  return await response.json();
68
}
69