0

Retrieve a visitor with User ID

by
Published Dec 20, 2024

You can fetch the details of a single visitor.

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 a visitor with User ID
8
 * You can fetch the details of a single visitor.
9
 */
10
export async function main(auth: Intercom, user_id: string | undefined) {
11
	const url = new URL(`https://api.intercom.io/visitors`)
12
	for (const [k, v] of [['user_id', user_id]]) {
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