0

List ad groups

by
Published Dec 20, 2024

List ad groups based on provided campaign IDs or ad group IDs.(campaign_ids or ad_group_ids). Note: Provide only campaign_id or ad_group_id. Do not provide both.

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 groups
7
 * List ad groups based on provided campaign IDs or ad group IDs.(campaign_ids or ad_group_ids). 
8
Note:
9
Provide only campaign_id or ad_group_id. Do not provide both.
10
 */
11
export async function main(
12
  auth: Pinterest,
13
  ad_account_id: string,
14
  campaign_ids: string | undefined,
15
  ad_group_ids: string | undefined,
16
  entity_statuses: string | undefined,
17
  page_size: string | undefined,
18
  order: "ASCENDING" | "DESCENDING" | undefined,
19
  bookmark: string | undefined,
20
  translate_interests_to_names: string | undefined,
21
) {
22
  const url = new URL(
23
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/ad_groups`,
24
  );
25
  for (const [k, v] of [
26
    ["campaign_ids", campaign_ids],
27
    ["ad_group_ids", ad_group_ids],
28
    ["entity_statuses", entity_statuses],
29
    ["page_size", page_size],
30
    ["order", order],
31
    ["bookmark", bookmark],
32
    ["translate_interests_to_names", translate_interests_to_names],
33
  ]) {
34
    if (v !== undefined && v !== "" && k !== undefined) {
35
      url.searchParams.append(k, v);
36
    }
37
  }
38
  const response = await fetch(url, {
39
    method: "GET",
40
    headers: {
41
      Authorization: "Bearer " + auth.token,
42
    },
43
    body: undefined,
44
  });
45
  if (!response.ok) {
46
    const text = await response.text();
47
    throw new Error(`${response.status} ${text}`);
48
  }
49
  return await response.json();
50
}
51