0

Create a webhook

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
 * Create a webhook
7
 *
8
 */
9
export async function main(
10
  auth: Brevo,
11
  body: {
12
    url: string;
13
    description?: string;
14
    events:
15
      | "sent"
16
      | "hardBounce"
17
      | "softBounce"
18
      | "blocked"
19
      | "spam"
20
      | "delivered"
21
      | "request"
22
      | "click"
23
      | "invalid"
24
      | "deferred"
25
      | "opened"
26
      | "uniqueOpened"
27
      | "unsubscribed"
28
      | "listAddition"
29
      | "contactUpdated"
30
      | "contactDeleted"
31
      | "inboundEmailProcessed"[];
32
    type?: "transactional" | "marketing" | "inbound";
33
    channel?: "sms" | "email";
34
    domain?: string;
35
    batched?: false | true;
36
    auth?: {};
37
    headers?: {}[];
38
  },
39
) {
40
  const url = new URL(`https://api.brevo.com/v3/webhooks`);
41

42
  const response = await fetch(url, {
43
    method: "POST",
44
    headers: {
45
      "Content-Type": "application/json",
46
      "api-key": auth.apiKey,
47
    },
48
    body: JSON.stringify(body),
49
  });
50
  if (!response.ok) {
51
    const text = await response.text();
52
    throw new Error(`${response.status} ${text}`);
53
  }
54
  return await response.json();
55
}
56