0

Search pins by a given search term

by
Published Dec 20, 2024

This endpoint is currently in beta and not available to all apps. Learn more. Get the top 10 Pins by a given search term.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Search pins by a given search term
7
 * This endpoint is currently in beta and not available to all apps. Learn more.
8

9
Get the top 10 Pins by a given search term.
10
 */
11
export async function main(
12
  auth: Pinterest,
13
  term: string | undefined,
14
  country_code: string | undefined,
15
  bookmark: string | undefined,
16
  locale: string | undefined,
17
  limit: string | undefined,
18
) {
19
  const url = new URL(`https://api.pinterest.com/v5/search/partner/pins`);
20
  for (const [k, v] of [
21
    ["term", term],
22
    ["country_code", country_code],
23
    ["bookmark", bookmark],
24
    ["locale", locale],
25
    ["limit", limit],
26
  ]) {
27
    if (v !== undefined && v !== "" && k !== undefined) {
28
      url.searchParams.append(k, v);
29
    }
30
  }
31
  const response = await fetch(url, {
32
    method: "GET",
33
    headers: {
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: undefined,
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44