0

List subscriptions for a contact

by
Published Dec 20, 2024

You can fetch a list of subscription types that are attached to a contact. These can be subscriptions that a user has 'opted-in' to or has 'opted-out' from, depending on the subscription type. This will return a list of Subscription Type objects that the contact is associated with. The data property will show a combined list of: 1.Opt-out subscription types that the user has opted-out from. 2.Opt-in subscription types that the user has opted-in to receiving.

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
 * List subscriptions for a contact
8
 * You can fetch a list of subscription types that are attached to a contact. These can be subscriptions that a user has 'opted-in' to or has 'opted-out' from, depending on the subscription type.
9
This will return a list of Subscription Type objects that the contact is associated with.
10

11
The data property will show a combined list of:
12

13
  1.Opt-out subscription types that the user has opted-out from.
14
  2.Opt-in subscription types that the user has opted-in to receiving.
15

16
 */
17
export async function main(auth: Intercom, contact_id: string) {
18
	const url = new URL(`https://api.intercom.io/contacts/${contact_id}/subscriptions`)
19

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