0

List ads

by
Published Dec 20, 2024

List ads that meet the filters provided: - Listed campaign ids or ad group ids or ad ids - Listed entity statuses If no filter is provided, all ads in the ad account are returned. Note: Provide only campaign_id or ad_group_id or ad_id. Do not provide more than one type. Review status is provided for each ad; if review_status is REJECTED, the rejected_reasons field will contain additional information. For more, see Pinterest advertising standards.

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 ads
7
 * List ads that meet the filters provided:
8
  - Listed campaign ids or ad group ids or ad ids
9
  - Listed entity statuses 
10
If no filter is provided, all ads in the ad account are returned. 
11
Note:
12
Provide only campaign_id or ad_group_id or ad_id. Do not provide more than one type. 
13
Review status is provided for each ad; if review_status is REJECTED, the rejected_reasons field will contain additional information.
14
For more, see Pinterest advertising standards.
15
 */
16
export async function main(
17
  auth: Pinterest,
18
  ad_account_id: string,
19
  campaign_ids: string | undefined,
20
  ad_group_ids: string | undefined,
21
  ad_ids: string | undefined,
22
  entity_statuses: string | undefined,
23
  page_size: string | undefined,
24
  order: "ASCENDING" | "DESCENDING" | undefined,
25
  bookmark: string | undefined,
26
) {
27
  const url = new URL(
28
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/ads`,
29
  );
30
  for (const [k, v] of [
31
    ["campaign_ids", campaign_ids],
32
    ["ad_group_ids", ad_group_ids],
33
    ["ad_ids", ad_ids],
34
    ["entity_statuses", entity_statuses],
35
    ["page_size", page_size],
36
    ["order", order],
37
    ["bookmark", bookmark],
38
  ]) {
39
    if (v !== undefined && v !== "" && k !== undefined) {
40
      url.searchParams.append(k, v);
41
    }
42
  }
43
  const response = await fetch(url, {
44
    method: "GET",
45
    headers: {
46
      Authorization: "Bearer " + auth.token,
47
    },
48
    body: undefined,
49
  });
50
  if (!response.ok) {
51
    const text = await response.text();
52
    throw new Error(`${response.status} ${text}`);
53
  }
54
  return await response.json();
55
}
56