0

Update a visitor

by
Published Dec 20, 2024

Sending a PUT request to `/visitors` will result in an update of an existing Visitor. **Option 1.** You can update a visitor by passing in the `user_id` of the visitor in the Request body. **Option 2.** You can update a visitor by passing in the `id` of the visitor in the Request body.

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
 * Update a visitor
8
 * Sending a PUT request to `/visitors` will result in an update of an existing Visitor.
9

10
**Option 1.** You can update a visitor by passing in the `user_id` of the visitor in the Request body.
11

12
**Option 2.** You can update a visitor by passing in the `id` of the visitor in the Request body.
13

14
 */
15
export async function main(
16
	auth: Intercom,
17
	body: {} & {
18
		id?: string
19
		user_id?: string
20
		name?: string
21
		custom_attributes?: {}
22
	}
23
) {
24
	const url = new URL(`https://api.intercom.io/visitors`)
25

26
	const response = await fetch(url, {
27
		method: 'PUT',
28
		headers: {
29
			'Intercom-Version': auth.apiVersion,
30
			'Content-Type': 'application/json',
31
			Authorization: 'Bearer ' + auth.token
32
		},
33
		body: JSON.stringify(body)
34
	})
35
	if (!response.ok) {
36
		const text = await response.text()
37
		throw new Error(`${response.status} ${text}`)
38
	}
39
	return await response.json()
40
}
41