0

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