0

Perform a search using a specific field from an item type

by
Published Oct 17, 2025

Performs a search from the values of a specific field. Results can either be the distinct values of the field (useful for searching autocomplete field values), or the IDs of actual items (deals, leads, persons, organizations or products).

Script pipedrive Verified

The script

Submitted by hugo697 Bun
Verified 235 days ago
1
//native
2
type Pipedrive = {
3
  apiToken: string;
4
};
5
/**
6
 * Perform a search using a specific field from an item type
7
 * Performs a search from the values of a specific field. Results can either be the distinct values of the field (useful for searching autocomplete field values), or the IDs of actual items (deals, leads, persons, organizations or products).
8
 */
9
export async function main(
10
  auth: Pipedrive,
11
  term: string | undefined,
12
  entity_type:
13
    | "deal"
14
    | "lead"
15
    | "person"
16
    | "organization"
17
    | "product"
18
    | "project"
19
    | undefined,
20
  match: "exact" | "beginning" | "middle" | undefined,
21
  field: string | undefined,
22
  limit: string | undefined,
23
  cursor: string | undefined,
24
) {
25
  const url = new URL(`https://api.pipedrive.com/api/v2/itemSearch/field`);
26
  for (const [k, v] of [
27
    ["term", term],
28
    ["entity_type", entity_type],
29
    ["match", match],
30
    ["field", field],
31
    ["limit", limit],
32
    ["cursor", cursor],
33
  ]) {
34
    if (v !== undefined && v !== "" && k !== undefined) {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      "x-api-token": auth.apiToken,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51