0

Create/Refresh Access Token

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
	body:
9
		| {
10
				grant_type: string
11
				client_id: string
12
				client_secret: string
13
				code: string
14
				scope?: string
15
		  }
16
		| {
17
				grant_type: string
18
				client_id: string
19
				client_secret: string
20
				refresh_token: string
21
				scope?: string
22
		  }
23
) {
24
	const url = new URL(`https://api.pandadoc.com/oauth2/access_token`)
25

26
	const response = await fetch(url, {
27
		method: 'POST',
28
		headers: {
29
			'Content-Type': 'application/x-www-form-urlencoded',
30
			Authorization: `API-Key ${auth.apiKey}`
31
		},
32
		body: new URLSearchParams(body as Record<string, string>)
33
	})
34

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

40
	return await response.json()
41
}
42