0

List ad accounts

by
Published Dec 20, 2024

Get a list of the ad_accounts that the "operation user_account" has access to. - This includes ad_accounts they own and ad_accounts that are owned by others who have granted them Business Access.

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 ad accounts
7
 * Get a list of the ad_accounts that the "operation user_account" has access to.
8
- This includes ad_accounts they own and ad_accounts that are owned by others who have granted them Business Access.
9
 */
10
export async function main(
11
  auth: Pinterest,
12
  bookmark: string | undefined,
13
  page_size: string | undefined,
14
  include_shared_accounts: string | undefined,
15
) {
16
  const url = new URL(`https://api.pinterest.com/v5/ad_accounts`);
17
  for (const [k, v] of [
18
    ["bookmark", bookmark],
19
    ["page_size", page_size],
20
    ["include_shared_accounts", include_shared_accounts],
21
  ]) {
22
    if (v !== undefined && v !== "" && k !== undefined) {
23
      url.searchParams.append(k, v);
24
    }
25
  }
26
  const response = await fetch(url, {
27
    method: "GET",
28
    headers: {
29
      Authorization: "Bearer " + auth.token,
30
    },
31
    body: undefined,
32
  });
33
  if (!response.ok) {
34
    const text = await response.text();
35
    throw new Error(`${response.status} ${text}`);
36
  }
37
  return await response.json();
38
}
39