0

Search Community Posts

by
Published today

Search Help Center community posts by text, optionally within one topic. Dates are `YYYY-MM-DD`. Offset-paged (`page`, up to 100 per page, first 100 pages only).

Script zendesk Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 2 hours ago
1
//native
2

3
/**
4
 * Search Community Posts
5
 * Search Help Center community posts by text, optionally within one topic. Dates are `YYYY-MM-DD`. Offset-paged (`page`, up to 100 per page, first 100 pages only).
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  query: string,
10
  topic_id: number | undefined,
11
  created_after: string | undefined,
12
  created_before: string | undefined,
13
  updated_after: string | undefined,
14
  updated_before: string | undefined,
15
  sort_by: "created_at" | "updated_at" | undefined,
16
  sort_order: "asc" | "desc" | undefined,
17
  page: number | undefined,
18
  per_page: number | undefined
19
) {
20
  const url = new URL(
21
    `https://${auth.subdomain}.zendesk.com/api/v2/help_center/community_posts/search`
22
  )
23
  const params = {
24
    query,
25
    topic: topic_id,
26
    created_after,
27
    created_before,
28
    updated_after,
29
    updated_before,
30
    sort_by,
31
    sort_order,
32
    page: page || 1,
33
    per_page: per_page || 25,
34
  }
35
  for (const [key, value] of Object.entries(params)) {
36
    if (value != null && value !== "") {
37
      url.searchParams.append(key, String(value))
38
    }
39
  }
40

41
  const response = await fetch(url, {
42
    headers: {
43
      Authorization: auth.token
44
        ? `Bearer ${auth.token}`
45
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
46
      Accept: "application/json",
47
    },
48
  })
49

50
  if (!response.ok) {
51
    throw new Error(`${response.status} ${await response.text()}`)
52
  }
53

54
  return await response.json()
55
}
56