0
Search Organization
One script reply has been approved by the moderators Verified

Searches for an organization by vanity name or email domain. See the docs here

Created by hugo697 158 days ago Viewed 197 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 158 days ago
1
// Requires apiVersion 202311 or lower.
2
type Linkedin = {
3
	token: string
4
	apiVersion: string
5
}
6

7
export async function main(
8
	resource: Linkedin,
9
	searchBy: 'vanity' | 'email',
10
	searchQuery: string, // must be either vanity name or email domain
11
	maxResults: number = 100
12
) {
13
	const count = 50 // Number of results per request
14
	const results: any[] = []
15

16
	const params = new URLSearchParams()
17

18
	if (searchBy === 'vanity') {
19
		params.append('q', 'vanityName')
20
		params.append('vanityName', searchQuery)
21
	} else {
22
		params.append('q', 'emailDomain')
23
		params.append('emailDomain', searchQuery)
24
	}
25

26
	params.append('count', count.toString())
27
	params.append('start', '0')
28

29
	let done = false
30
	do {
31
		const endpoint = `https://api.linkedin.com/rest/organizations?${params.toString()}`
32

33
		const response = await fetch(endpoint, {
34
			method: 'GET',
35
			headers: {
36
				Authorization: `Bearer ${resource.token}`,
37
				'LinkedIn-Version': `${resource.apiVersion}`,
38
				'X-Restli-Protocol-Version': '2.0.0'
39
			}
40
		})
41

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

46
		const data = await response.json()
47
		results.push(...data.elements)
48

49
		params.set('start', (parseInt(params.get('start')!) + count).toString())
50
		if (data.elements.length < count || results.length >= maxResults) {
51
			done = true
52
		}
53
	} while (!done)
54

55
	return results.slice(0, maxResults)
56
}
57