0

Add subscription to a contact

by
Published Dec 20, 2024

You can add a specific subscription to a contact.

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
 * Add subscription to a contact
8
 * You can add a specific subscription to a contact.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	contact_id: string,
13
	body: { id: string; consent_type: string }
14
) {
15
	const url = new URL(`https://api.intercom.io/contacts/${contact_id}/subscriptions`)
16

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