0

Create a data attribute

by
Published Dec 20, 2024

You can create a data attributes for a `contact` or a `company`.

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
 * Create a data attribute
8
 * You can create a data attributes for a `contact` or a `company`.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	body: {
13
		name: string
14
		model: 'contact' | 'company'
15
		data_type: 'string' | 'boolean' | 'integer' | 'float' | 'datetime' | 'date'
16
		description?: string
17
		options?: string[]
18
		messenger_writable?: false | true
19
	}
20
) {
21
	const url = new URL(`https://api.intercom.io/data_attributes`)
22

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