0

Get the list of transactional emails on the basis of allowed filters

by
Published Apr 8, 2025

This endpoint will show the list of emails for past 30 days by default. To retrieve emails before that time, please pass startDate and endDate in query filters.

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 the list of transactional emails on the basis of allowed filters
7
 * This endpoint will show the list of emails for past 30 days by default. To retrieve emails before that time, please pass startDate and endDate in query filters.
8
 */
9
export async function main(
10
  auth: Brevo,
11
  email: string | undefined,
12
  templateId: string | undefined,
13
  messageId: string | undefined,
14
  startDate: string | undefined,
15
  endDate: string | undefined,
16
  sort: "asc" | "desc" | undefined,
17
  limit: string | undefined,
18
  offset: string | undefined,
19
) {
20
  const url = new URL(`https://api.brevo.com/v3/smtp/emails`);
21
  for (const [k, v] of [
22
    ["email", email],
23
    ["templateId", templateId],
24
    ["messageId", messageId],
25
    ["startDate", startDate],
26
    ["endDate", endDate],
27
    ["sort", sort],
28
    ["limit", limit],
29
    ["offset", offset],
30
  ]) {
31
    if (v !== undefined && v !== "" && k !== undefined) {
32
      url.searchParams.append(k, v);
33
    }
34
  }
35
  const response = await fetch(url, {
36
    method: "GET",
37
    headers: {
38
      "api-key": auth.apiKey,
39
    },
40
    body: undefined,
41
  });
42
  if (!response.ok) {
43
    const text = await response.text();
44
    throw new Error(`${response.status} ${text}`);
45
  }
46
  return await response.json();
47
}
48