0

List accounts with access to an audience owned by an ad account

by
Published Dec 20, 2024

List all ad accounts and/or businesses that have access to a specific audience. The audience must be owned by the requesting ad account.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * List accounts with access to an audience owned by an ad account
7
 * List all ad accounts and/or businesses that have access to a specific audience. The audience must be owned by the requesting ad account.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  audience_id: string | undefined,
13
  account_type: "AD_ACCOUNT" | "BUSINESS_ACCOUNT" | undefined,
14
  page_size: string | undefined,
15
  bookmark: string | undefined,
16
) {
17
  const url = new URL(
18
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audiences/shared/accounts`,
19
  );
20
  for (const [k, v] of [
21
    ["audience_id", audience_id],
22
    ["account_type", account_type],
23
    ["page_size", page_size],
24
    ["bookmark", bookmark],
25
  ]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "GET",
32
    headers: {
33
      Authorization: "Bearer " + auth.token,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43