0

Download document protected

by
Published Nov 5, 2024

Download a signed PDF of a completed document

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(auth: Pandadoc, id: string, separate_files: string | undefined) {
7
	const url = new URL(`https://api.pandadoc.com/public/v1/documents/${id}/download-protected`)
8

9
	for (const [k, v] of [['separate_files', separate_files]]) {
10
		if (v !== undefined && v !== '' && k !== undefined) {
11
			url.searchParams.append(k, v)
12
		}
13
	}
14

15
	const response = await fetch(url, {
16
		method: 'GET',
17
		headers: {
18
			Authorization: `API-Key ${auth.apiKey}`
19
		},
20
		body: undefined
21
	})
22

23
	if (!response.ok) {
24
		const text = await response.text()
25
		throw new Error(`${response.status} ${text}`)
26
	}
27

28
	return await response.text()
29
}
30