0

List all segments

by
Published Dec 20, 2024

You can fetch a list of all segments.

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
 * List all segments
8
 * You can fetch a list of all segments.
9
 */
10
export async function main(auth: Intercom, include_count: string | undefined) {
11
	const url = new URL(`https://api.intercom.io/segments`)
12
	for (const [k, v] of [['include_count', include_count]]) {
13
		if (v !== undefined && v !== '' && k !== undefined) {
14
			url.searchParams.append(k, v)
15
		}
16
	}
17
	const response = await fetch(url, {
18
		method: 'GET',
19
		headers: {
20
			'Intercom-Version': auth.apiVersion,
21
			Authorization: 'Bearer ' + auth.token
22
		},
23
		body: undefined
24
	})
25
	if (!response.ok) {
26
		const text = await response.text()
27
		throw new Error(`${response.status} ${text}`)
28
	}
29
	return await response.json()
30
}
31