0
Create Contact
One script reply has been approved by the moderators Verified

Creates a contact. See docs here

Created by hugo697 199 days ago Viewed 3235 times
0
Submitted by hugo697 Bun
Verified 199 days ago
1
type Accelo = {
2
	clientId: string
3
	clientSecret: string
4
	deployment: string
5
}
6

7
export async function main(
8
	resource: Accelo,
9
	data: {
10
		firstname: string
11
		surname: string
12
		company_id: number
13
		middlename?: string
14
		username?: string
15
		password?: string
16
		title?: string
17
		comments?: string
18
		status?: number
19
		standing?: string
20
		email?: string
21
		country_id?: number
22
		physical_address_id?: number
23
		postal_address_id?: number
24
		phone?: string
25
		fax?: string
26
		position?: string
27
		communication?: string
28
		invoice_method?: string
29
	}
30
) {
31
	// Fetch the access token
32
	const accessTokenResponse = (await (
33
		await fetch(`https://${resource.deployment}.api.accelo.com/oauth2/v0/token`, {
34
			method: 'POST',
35
			headers: {
36
				'Content-Type': 'application/x-www-form-urlencoded',
37
				Authorization: `Basic ${Buffer.from(
38
					`${resource.clientId}:${resource.clientSecret}`
39
				).toString('base64')}`
40
			},
41
			body: new URLSearchParams({
42
				grant_type: 'client_credentials',
43
				scope: 'write(all)'
44
			})
45
		})
46
	).json()) as any
47
	const accessToken = accessTokenResponse.access_token
48

49
	const form = new URLSearchParams()
50
	Object.entries(data).forEach(([key, value]) => value && form.append(key, value + ''))
51

52
	return (
53
		await fetch(`https://${resource.deployment}.api.accelo.com/api/v0/contacts`, {
54
			method: 'POST',
55
			headers: {
56
				'Content-Type': 'application/x-www-form-urlencoded',
57
				Authorization: `Bearer ${accessToken}`
58
			},
59
			body: form
60
		})
61
	).json()
62
}
63