0
Add Webhook
One script reply has been approved by the moderators Verified

Create a webhook automation

Created by hugo697 81 days ago Viewed 44 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 81 days ago
1
type Convertkit = {
2
	apiSecret: string
3
}
4

5
export async function main(
6
	resource: Convertkit,
7
	payload: {
8
		targetUrl: string
9
		event: {
10
			name: string
11
			[key: string]: any
12
		}
13
	}
14
) {
15
	const endpoint = `https://api.convertkit.com/v3/automations/hooks`
16

17
	const body = {
18
		api_secret: resource.apiSecret,
19
		target_url: payload.targetUrl,
20
		event: payload.event
21
	}
22

23
	const response = await fetch(endpoint, {
24
		method: 'POST',
25
		headers: {
26
			'Content-Type': 'application/json'
27
		},
28
		body: JSON.stringify(body)
29
	})
30

31
	if (!response.ok) {
32
		throw new Error(`HTTP error! status: ${response.status}`)
33
	}
34

35
	const data = await response.json()
36
	return data.rule
37
}
38