0

Create Webhook

by
Published Apr 8, 2025

Create a new Webhook*Rate limits*:Burst: `1/s`Steady: `15/m` **Scopes:** `webhooks:write`

Script klaviyo Verified

The script

Submitted by hugo697 Bun
Verified 428 days ago
1
//native
2
type Klaviyo = {
3
  apiKey: string;
4
};
5
/**
6
 * Create Webhook
7
 * Create a new Webhook*Rate limits*:Burst: `1/s`Steady: `15/m`
8

9
 */
10
export async function main(
11
  auth: Klaviyo,
12
  revision: string,
13
  body: {
14
    data: {
15
      type: "webhook";
16
      attributes: {
17
        name: string;
18
        description?: string;
19
        endpoint_url: string;
20
        secret_key: string;
21
      };
22
      relationships?: {
23
        "webhook-topics"?: { data?: { type: "webhook-topic"; id: string }[] };
24
      };
25
    };
26
  },
27
) {
28
  const url = new URL(`https://a.klaviyo.com/api/webhooks`);
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      revision: revision,
34
      "Accept": "application/vnd.api+json",
35
      "Content-Type": "application/vnd.api+json",
36
      Authorization: "Klaviyo-API-Key " + auth.apiKey,
37
    },
38
    body: JSON.stringify(body),
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46