0

Search Articles

by
Published today

Search Help Center articles by text, category, section and/or labels (at least one is required). Dates are `YYYY-MM-DD`; `locale` takes `*` for all locales. Label filtering needs a Professional or Enterprise plan. Offset-paged (`page`, up to 100 per page).

Script zendesk Verified

The script

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

3
/**
4
 * Search Articles
5
 * Search Help Center articles by text, category, section and/or labels (at least one is required). Dates are `YYYY-MM-DD`; `locale` takes `*` for all locales. Label filtering needs a Professional or Enterprise plan. Offset-paged (`page`, up to 100 per page).
6
 */
7
export async function main(
8
  auth: RT.Zendesk,
9
  query: string | undefined,
10
  locale: string | undefined,
11
  category_ids: number[] | undefined,
12
  section_ids: number[] | undefined,
13
  label_names: string[] | undefined,
14
  brand_id: number | undefined,
15
  multibrand: boolean,
16
  created_after: string | undefined,
17
  created_before: string | undefined,
18
  updated_after: string | undefined,
19
  updated_before: string | undefined,
20
  sort_by: "created_at" | "updated_at" | undefined,
21
  sort_order: "asc" | "desc" | undefined,
22
  page: number | undefined,
23
  per_page: number | undefined
24
) {
25
  const url = new URL(
26
    `https://${auth.subdomain}.zendesk.com/api/v2/help_center/articles/search`
27
  )
28
  const params = {
29
    query,
30
    locale,
31
    category: category_ids?.join(","),
32
    section: section_ids?.join(","),
33
    label_names: label_names?.join(","),
34
    brand_id,
35
    multibrand: multibrand ? "true" : undefined,
36
    created_after,
37
    created_before,
38
    updated_after,
39
    updated_before,
40
    sort_by,
41
    sort_order,
42
    page: page || 1,
43
    per_page: per_page || 25,
44
  }
45
  for (const [key, value] of Object.entries(params)) {
46
    if (value != null && value !== "") {
47
      url.searchParams.append(key, String(value))
48
    }
49
  }
50

51
  const response = await fetch(url, {
52
    headers: {
53
      Authorization: auth.token
54
        ? `Bearer ${auth.token}`
55
        : `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
56
      Accept: "application/json",
57
    },
58
  })
59

60
  if (!response.ok) {
61
    throw new Error(`${response.status} ${await response.text()}`)
62
  }
63

64
  return await response.json()
65
}
66