0

Retrieve Location Reviews

by
Published Nov 5, 2024

Retrieves reviews for a specific location.

Script tripadvisor Verified

The script

Submitted by hugo697 Bun
Verified 581 days ago
1
//native
2
type Tripadvisor = {
3
	apiKey: string
4
}
5

6
export async function main(
7
	resource: Tripadvisor,
8
	locationId: string,
9
	options?: {
10
		language: string
11
		limit: number
12
		offset: number
13
	}
14
) {
15
	const queryParams = new URLSearchParams({
16
		key: resource.apiKey
17
	})
18

19
	if (options?.language) {
20
		queryParams.append('language', options.language)
21
	}
22

23
	if (options?.limit) {
24
		queryParams.append('limit', options.limit.toString())
25
	}
26

27
	if (options?.offset) {
28
		queryParams.append('offset', options.offset.toString())
29
	}
30

31
	const endpoint = `https://api.content.tripadvisor.com/api/v1/location/${locationId}/reviews?${queryParams.toString()}`
32

33
	const response = await fetch(endpoint, {
34
		method: 'GET',
35
		headers: {
36
			accept: 'application/json'
37
		}
38
	})
39

40
	if (!response.ok) {
41
		throw new Error(`HTTP error! status: ${response.status}`)
42
	}
43

44
	const data = await response.json()
45

46
	return data
47
}
48