0

List campaigns

by
Published Dec 20, 2024

Get a list of the campaigns in the specified ad_account_id, filtered by the specified options. - The token's user_account must either be the Owner of the specified ad account, or have one of the necessary roles granted to them via Business Access: Admin, Analyst, Campaign Manager.

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 campaigns
7
 * Get a list of the campaigns in the specified ad_account_id, filtered by the specified options.
8
- The token's user_account must either be the Owner of the specified ad account, or have one of the necessary roles granted to them via Business Access: Admin, Analyst, Campaign Manager.
9
 */
10
export async function main(
11
  auth: Pinterest,
12
  ad_account_id: string,
13
  campaign_ids: string | undefined,
14
  entity_statuses: string | undefined,
15
  page_size: string | undefined,
16
  order: "ASCENDING" | "DESCENDING" | undefined,
17
  bookmark: string | undefined,
18
) {
19
  const url = new URL(
20
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/campaigns`,
21
  );
22
  for (const [k, v] of [
23
    ["campaign_ids", campaign_ids],
24
    ["entity_statuses", entity_statuses],
25
    ["page_size", page_size],
26
    ["order", order],
27
    ["bookmark", bookmark],
28
  ]) {
29
    if (v !== undefined && v !== "" && k !== undefined) {
30
      url.searchParams.append(k, v);
31
    }
32
  }
33
  const response = await fetch(url, {
34
    method: "GET",
35
    headers: {
36
      Authorization: "Bearer " + auth.token,
37
    },
38
    body: undefined,
39
  });
40
  if (!response.ok) {
41
    const text = await response.text();
42
    throw new Error(`${response.status} ${text}`);
43
  }
44
  return await response.json();
45
}
46