0

Convert a visitor

by
Published Dec 20, 2024

You can merge a Visitor to a Contact of role type `lead` or `user`. > 📘 What happens upon a visitor being converted? > > If the User exists, then the Visitor will be merged into it, the Visitor deleted and the User returned. If the User does not exist, the Visitor will be converted to a User, with the User identifiers replacing it's Visitor identifiers.

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
 * Convert a visitor
8
 * You can merge a Visitor to a Contact of role type `lead` or `user`.
9

10
> 📘 What happens upon a visitor being converted?
11
>
12
> If the User exists, then the Visitor will be merged into it, the Visitor deleted and the User returned. If the User does not exist, the Visitor will be converted to a User, with the User identifiers replacing it's Visitor identifiers.
13

14
 */
15
export async function main(auth: Intercom, body: { type: string; user: {}; visitor: {} }) {
16
	const url = new URL(`https://api.intercom.io/visitors/convert`)
17

18
	const response = await fetch(url, {
19
		method: 'POST',
20
		headers: {
21
			'Intercom-Version': auth.apiVersion,
22
			'Content-Type': 'application/json',
23
			Authorization: 'Bearer ' + auth.token
24
		},
25
		body: JSON.stringify(body)
26
	})
27
	if (!response.ok) {
28
		const text = await response.text()
29
		throw new Error(`${response.status} ${text}`)
30
	}
31
	return await response.json()
32
}
33