0

Retrieve companies

by
Published Dec 20, 2024

You can fetch a single company by passing in `company_id` or `name`. `https://api.intercom.io/companies?name={name}` `https://api.intercom.io/companies?company_id={company_id}` You can fetch all companies and filter by `segment_id` or `tag_id` as a query parameter. `https://api.intercom.io/companies?tag_id={tag_id}` `https://api.intercom.io/companies?segment_id={segment_id}`

Script intercom Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Intercom = {
3
	apiVersion: string
4
	token: string
5
}
6
/**
7
 * Retrieve companies
8
 * You can fetch a single company by passing in `company_id` or `name`.
9

10
  `https://api.intercom.io/companies?name={name}`
11

12
  `https://api.intercom.io/companies?company_id={company_id}`
13

14
You can fetch all companies and filter by `segment_id` or `tag_id` as a query parameter.
15

16
  `https://api.intercom.io/companies?tag_id={tag_id}`
17

18
  `https://api.intercom.io/companies?segment_id={segment_id}`
19

20
 */
21
export async function main(
22
	auth: Intercom,
23
	name: string | undefined,
24
	company_id: string | undefined,
25
	tag_id: string | undefined,
26
	segment_id: string | undefined,
27
	page: string | undefined,
28
	per_page: string | undefined
29
) {
30
	const url = new URL(`https://api.intercom.io/companies`)
31
	for (const [k, v] of [
32
		['name', name],
33
		['company_id', company_id],
34
		['tag_id', tag_id],
35
		['segment_id', segment_id],
36
		['page', page],
37
		['per_page', per_page]
38
	]) {
39
		if (v !== undefined && v !== '' && k !== undefined) {
40
			url.searchParams.append(k, v)
41
		}
42
	}
43
	const response = await fetch(url, {
44
		method: 'GET',
45
		headers: {
46
			'Intercom-Version': auth.apiVersion,
47
			Authorization: 'Bearer ' + auth.token
48
		},
49
		body: undefined
50
	})
51
	if (!response.ok) {
52
		const text = await response.text()
53
		throw new Error(`${response.status} ${text}`)
54
	}
55
	return await response.json()
56
}
57