0
Get All Bounces
One script reply has been approved by the moderators Verified
Created by sebastienarredeordix 688 days ago Viewed 4817 times
0
Submitted by adam186 Deno
Verified 510 days ago
1
import sendgrid from "npm:@sendgrid/client@^7.7.0";
2

3
/**
4
 * @param on_behalf_of The subuser's username. This header generates the
5
 * API call as if the subuser account was making the call.
6
 *
7
 * @param start_time The start of the time range in unix timestamp
8
 * when a bounce was created (inclusive).
9
 *
10
 * @param end_time The end of the time range in unix timestamp
11
 * when a bounce was created (inclusive).
12
 *
13
 * @param limit Limit the number of returned elements. Default is 500.
14
 *
15
 * @param offset Pagination.
16
 */
17
type Sendgrid = {
18
  token: string;
19
};
20
export async function main(
21
  api_token: Sendgrid,
22
  on_behalf_of?: string,
23
  start_time?: number,
24
  end_time?: number,
25
  limit?: number,
26
  offset?: number,
27
) {
28
  sendgrid.setApiKey(api_token.token);
29
  const headers: Record<string, string> = { Accept: "application/json" };
30
  if (on_behalf_of) {
31
    headers["on-behalf-of"] = on_behalf_of;
32
  }
33

34
  const request = {
35
    url: `/v3/suppression/bounces`,
36
    method: "GET",
37
    headers,
38
    qs: {
39
      start_time,
40
      end_time,
41
      limit,
42
      offset,
43
    },
44
  };
45

46
  try {
47
    const [_, body] = await sendgrid.request(request);
48
    return body;
49
  } catch (error) {
50
    throw Error("\n" + JSON.stringify(error?.response?.body || error));
51
  }
52
}
53