0

Get Tickets

by
Published Aug 26, 2024

Get Ticket instances from Freshdesk.

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) {
7
	// Remove the trailing slash from base URL.
8
	const baseUrl = resource.baseUrl.replace(/\/$/, '')
9
	const endpoint = `${baseUrl}/api/v2/tickets/`
10

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

14
	const response = await fetch(endpoint, {
15
		method: 'GET',
16
		headers: {
17
			Authorization: `Basic ${encodedKey}`,
18
			'Content-Type': 'application/json'
19
		}
20
	})
21

22
	if (!response.ok) {
23
		throw new Error(`HTTP error! status: ${response.status}`)
24
	}
25

26
	const data = await response.json()
27

28
	return data
29
}
30