0

Search for articles

by
Published Dec 20, 2024

You can search for articles by making a GET request to `https://api.intercom.io/articles/search`.

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
 * Search for articles
8
 * You can search for articles by making a GET request to `https://api.intercom.io/articles/search`.
9
 */
10
export async function main(
11
	auth: Intercom,
12
	phrase: string | undefined,
13
	state: string | undefined,
14
	help_center_id: string | undefined,
15
	highlight: string | undefined
16
) {
17
	const url = new URL(`https://api.intercom.io/articles/search`)
18
	for (const [k, v] of [
19
		['phrase', phrase],
20
		['state', state],
21
		['help_center_id', help_center_id],
22
		['highlight', highlight]
23
	]) {
24
		if (v !== undefined && v !== '' && k !== undefined) {
25
			url.searchParams.append(k, v)
26
		}
27
	}
28
	const response = await fetch(url, {
29
		method: 'GET',
30
		headers: {
31
			'Intercom-Version': auth.apiVersion,
32
			Authorization: 'Bearer ' + auth.token
33
		},
34
		body: undefined
35
	})
36
	if (!response.ok) {
37
		const text = await response.text()
38
		throw new Error(`${response.status} ${text}`)
39
	}
40
	return await response.json()
41
}
42