0

Submit a data event

by
Published Dec 20, 2024

You will need an Access Token that has write permissions to send Events.

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
 * Submit a data event
8
 * 
9
You will need an Access Token that has write permissions to send Events.
10
 */
11
export async function main(
12
	auth: Intercom,
13
	body:
14
		| ({} & {
15
				event_name?: string
16
				created_at?: number
17
				user_id?: string
18
				id?: string
19
				email?: string
20
				metadata?: {}
21
		  })
22
		| ({} & {
23
				event_name?: string
24
				created_at?: number
25
				user_id?: string
26
				id?: string
27
				email?: string
28
				metadata?: {}
29
		  })
30
		| ({} & {
31
				event_name?: string
32
				created_at?: number
33
				user_id?: string
34
				id?: string
35
				email?: string
36
				metadata?: {}
37
		  })
38
) {
39
	const url = new URL(`https://api.intercom.io/events`)
40

41
	const response = await fetch(url, {
42
		method: 'POST',
43
		headers: {
44
			'Intercom-Version': auth.apiVersion,
45
			'Content-Type': 'application/json',
46
			Authorization: 'Bearer ' + auth.token
47
		},
48
		body: JSON.stringify(body)
49
	})
50
	if (!response.ok) {
51
		const text = await response.text()
52
		throw new Error(`${response.status} ${text}`)
53
	}
54
	return await response.text()
55
}
56