0

Search Locations

Published Nov 5, 2024

Retrieves up to 10 locations found by the given search query.

Script tripadvisor Verified

The script

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

6
export async function main(
7
	resource: Tripadvisor,
8
	searchQuery: string,
9
	options?: {
10
		category?: 'hotels' | 'attractions' | 'restaurants' | 'geos'
11
		phone?: string
12
		address?: string
13
		latLong?: string
14
		radius?: number
15
		radiusUnit?: 'km' | 'mi' | 'm'
16
		language?: string
17
	}
18
) {
19
	const queryParams = new URLSearchParams({
20
		key: resource.apiKey,
21
		searchQuery
22
	})
23

24
	if (options?.category) {
25
		queryParams.append('category', options.category)
26
	}
27

28
	if (options?.phone) {
29
		queryParams.append('phone', options.phone)
30
	}
31

32
	if (options?.address) {
33
		queryParams.append('address', options.address)
34
	}
35

36
	if (options?.latLong) {
37
		queryParams.append('latLong', options.latLong)
38
	}
39

40
	if (options?.radius) {
41
		queryParams.append('radius', options.radius.toString())
42
	}
43

44
	if (options?.radiusUnit) {
45
		queryParams.append('radiusUnit', options.radiusUnit)
46
	}
47

48
	if (options?.language) {
49
		queryParams.append('language', options.language)
50
	}
51

52
	const endpoint = `https://api.content.tripadvisor.com/api/v1/location/search?${queryParams.toString()}`
53

54
	const response = await fetch(endpoint, {
55
		method: 'GET',
56
		headers: {
57
			accept: 'application/json'
58
		}
59
	})
60

61
	if (!response.ok) {
62
		throw new Error(`HTTP error! status: ${response.status}`)
63
	}
64

65
	const data = await response.json()
66

67
	return data
68
}
69