0

Create document

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
	editor_ver: string | undefined,
9
	body: {
10
		name?: string
11
		detect_title_variables?: false | true
12
		template_uuid?: string
13
		folder_uuid?: string
14
		owner?: {}
15
		recipients?: {
16
			email: string
17
			first_name?: string
18
			last_name?: string
19
			role?: string
20
			signing_order?: number
21
		}[]
22
		tokens?: { name: string; value: string }[]
23
		fields?: {}
24
		metadata?: {}
25
		tags?: string[]
26
		images?: { urls: string[]; name: string }[]
27
		pricing_tables?: {
28
			name: string
29
			data_merge?: false | true
30
			options?: {}
31
			sections?: {
32
				title: string
33
				default?: false | true
34
				multichoice_enabled?: false | true
35
				rows?: {
36
					options?: {
37
						qty_editable?: false | true
38
						optional_selected?: false | true
39
						optional?: false | true
40
					}
41
					data?: {}
42
					custom_fields?: {}
43
				}[]
44
			}[]
45
		}[]
46
		content_placeholders?: {
47
			block_id?: string
48
			content_library_items?: {
49
				id: string
50
				pricing_tables?: {
51
					name: string
52
					data_merge?: false | true
53
					options?: {}
54
					sections?: {
55
						title: string
56
						default?: false | true
57
						multichoice_enabled?: false | true
58
						rows?: {
59
							options?: {
60
								qty_editable?: false | true
61
								optional_selected?: false | true
62
								optional?: false | true
63
							}
64
							data?: {}
65
							custom_fields?: {}
66
						}[]
67
					}[]
68
				}[]
69
				fields?: {}
70
				recipients?: {
71
					email: string
72
					first_name?: string
73
					last_name?: string
74
					role?: string
75
					signing_order?: number
76
				}[]
77
			}[]
78
		}[]
79
		url?: string
80
		parse_form_fields?: false | true
81
	}
82
) {
83
	const url = new URL(`https://api.pandadoc.com/public/v1/documents`)
84

85
	for (const [k, v] of [['editor_ver', editor_ver]]) {
86
		if (v !== undefined && v !== '' && k !== undefined) {
87
			url.searchParams.append(k, v)
88
		}
89
	}
90

91
	const formData = new FormData()
92

93
	for (const [k, v] of Object.entries(body)) {
94
		if (v !== undefined && v !== '') {
95
			formData.append(k, String(v))
96
		}
97
	}
98

99
	const response = await fetch(url, {
100
		method: 'POST',
101
		headers: {
102
			'Content-Type': 'application/json',
103
			Authorization: `API-Key ${auth.apiKey}`
104
		},
105
		body: JSON.stringify(body)
106
	})
107

108
	if (!response.ok) {
109
		const text = await response.text()
110
		throw new Error(`${response.status} ${text}`)
111
	}
112

113
	return await response.json()
114
}
115