0

Get customer lists

by
Published Dec 20, 2024

Get a set of customer lists including id and name based on the filters provided. (Customer lists are a type of audience.) For more information, see Audience targeting or the Audiences section of the ads management guide.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Get customer lists
7
 * Get a set of customer lists including id and name based on the filters provided.
8
(Customer lists are a type of audience.) For more information, see
9
Audience targeting
10
 or the Audiences
11
section of the ads management guide.
12
 */
13
export async function main(
14
  auth: Pinterest,
15
  ad_account_id: string,
16
  page_size: string | undefined,
17
  order: "ASCENDING" | "DESCENDING" | undefined,
18
  bookmark: string | undefined,
19
) {
20
  const url = new URL(
21
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/customer_lists`,
22
  );
23
  for (const [k, v] of [
24
    ["page_size", page_size],
25
    ["order", order],
26
    ["bookmark", bookmark],
27
  ]) {
28
    if (v !== undefined && v !== "" && k !== undefined) {
29
      url.searchParams.append(k, v);
30
    }
31
  }
32
  const response = await fetch(url, {
33
    method: "GET",
34
    headers: {
35
      Authorization: "Bearer " + auth.token,
36
    },
37
    body: undefined,
38
  });
39
  if (!response.ok) {
40
    const text = await response.text();
41
    throw new Error(`${response.status} ${text}`);
42
  }
43
  return await response.json();
44
}
45