0

Update webhook subscription

by
Published Nov 5, 2024
Script pandadoc Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Pandadoc = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	auth: Pandadoc,
8
	id: string,
9
	body: {
10
		name?: string
11
		url?: string
12
		active?: false | true
13
		payload?: 'metadata' | 'fields' | 'products' | 'tokens' | 'pricing'[]
14
		triggers?:
15
			| 'recipient_completed'
16
			| 'document_updated'
17
			| 'document_deleted'
18
			| 'document_state_changed'
19
			| 'document_creation_failed'
20
			| 'quote_updated'[]
21
	}
22
) {
23
	const url = new URL(`https://api.pandadoc.com/public/v1/webhook-subscriptions/${id}`)
24

25
	const response = await fetch(url, {
26
		method: 'PATCH',
27
		headers: {
28
			'Content-Type': 'application/json',
29
			Authorization: `API-Key ${auth.apiKey}`
30
		},
31
		body: JSON.stringify(body)
32
	})
33

34
	if (!response.ok) {
35
		const text = await response.text()
36
		throw new Error(`${response.status} ${text}`)
37
	}
38

39
	return await response.json()
40
}
41