0

Update a contact

by
Published Dec 20, 2024

You can update an existing contact (ie. user or lead).

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 contact
8
 * You can update an existing contact (ie. user or lead).
9
 */
10
export async function main(
11
	auth: Intercom,
12
	id: string,
13
	body: {
14
		role?: string
15
		external_id?: string
16
		email?: string
17
		phone?: string
18
		name?: string
19
		avatar?: string
20
		signed_up_at?: number
21
		last_seen_at?: number
22
		owner_id?: number
23
		unsubscribed_from_emails?: false | true
24
		custom_attributes?: {}
25
	}
26
) {
27
	const url = new URL(`https://api.intercom.io/contacts/${id}`)
28

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