0

Get Contact from ID

by
Published Aug 26, 2024

Get contacts details from Freshdesk using ID number.

Script freshdesk Verified

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 652 days ago
1
type Freshdesk = {
2
	apiKey: string
3
	baseUrl: string
4
}
5

6
export async function main(resource: Freshdesk, contactId: string) {
7
	// Remove trailing slash from base URL
8
	const baseUrl = resource.baseUrl.replace(/\/$/, '')
9

10
	// Construct the URL for the API request
11
	const url = `${baseUrl}/api/v2/contacts/${contactId}`
12

13
	// Base64 encode the API key with a colon and 'X'
14
	const encodedKey = btoa(`${resource.apiKey}:X`)
15

16
	// Make the API request
17
	const response = await fetch(url, {
18
		method: 'GET',
19
		headers: {
20
			Authorization: `Basic ${encodedKey}`,
21
			'Content-Type': 'application/json'
22
		}
23
	})
24

25
	// Handle the response
26
	if (!response.ok) {
27
		throw new Error(`HTTP error! status: ${response.status}`)
28
	}
29

30
	// Parse and return the response data
31
	const data = await response.json()
32

33
	return data
34
}
35