Create Webhook

Create a new Webhook, to be notified when Webflow resources change. Limit of 75 registrations per `triggerType`, per site. Access to this endpoint requires a bearer token from a Data Client App. Required scope | `sites:write`

Script webflow Verified

by hugo697 ยท 11/5/2024

The script

Submitted by hugo697 Bun
Verified 564 days ago
1
//native
2
type Webflow = {
3
	token: string
4
}
5

6
export async function main(
7
	auth: Webflow,
8
	site_id: string,
9
	body: {
10
		id?: string
11
		triggerType?:
12
			| 'form_submission'
13
			| 'site_publish'
14
			| 'page_created'
15
			| 'page_metadata_updated'
16
			| 'page_deleted'
17
			| 'ecomm_new_order'
18
			| 'ecomm_order_changed'
19
			| 'ecomm_inventory_changed'
20
			| 'user_account_added'
21
			| 'user_account_updated'
22
			| 'user_account_deleted'
23
			| 'collection_item_created'
24
			| 'collection_item_changed'
25
			| 'collection_item_deleted'
26
			| 'collection_item_unpublished'
27
		url?: string
28
		workspaceId?: string
29
		siteId?: string
30
		filter?: { name?: string }
31
		lastTriggered?: string
32
		createdOn?: string
33
	}
34
) {
35
	const url = new URL(`https://api.webflow.com/v2/sites/${site_id}/webhooks`)
36

37
	const response = await fetch(url, {
38
		method: 'POST',
39
		headers: {
40
			'Content-Type': 'application/json',
41
			Authorization: 'Bearer ' + auth.token
42
		},
43
		body: JSON.stringify(body)
44
	})
45

46
	if (!response.ok) {
47
		const text = await response.text()
48
		throw new Error(`${response.status} ${text}`)
49
	}
50

51
	return await response.json()
52
}
53