0

List accounts with access to an audience owned by a business

by
Published Dec 20, 2024

List all ad accounts and/or businesses that have access to a specific audience. The audience must either be owned by an ad account in the requesting business, or it must have been shared with the requesting business. If the requesting business is not the owner of the audience, only ad accounts owned by the requesting business will be returned.

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 a business
7
 * List all ad accounts and/or businesses that have access to a specific audience.
8
The audience must either be owned by an ad account in the requesting business, or it must have been shared with the requesting business.
9
If the requesting business is not the owner of the audience, only ad accounts owned by the requesting business will be returned.
10
 */
11
export async function main(
12
  auth: Pinterest,
13
  business_id: string,
14
  audience_id: string | undefined,
15
  account_type: "AD_ACCOUNT" | "BUSINESS_ACCOUNT" | undefined,
16
  page_size: string | undefined,
17
  bookmark: string | undefined,
18
) {
19
  const url = new URL(
20
    `https://api.pinterest.com/v5/businesses/${business_id}/audiences/shared/accounts`,
21
  );
22
  for (const [k, v] of [
23
    ["audience_id", audience_id],
24
    ["account_type", account_type],
25
    ["page_size", page_size],
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