0

Get advertiser entities in bulk

by
Published Dec 20, 2024

Create an asynchronous report that may include information on campaigns, ad groups, product groups, ads, and/or keywords; can filter by campaigns. Though the entities may be active, archived, or paused, only active entities will return data.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Get advertiser entities in bulk
7
 * Create an asynchronous report that may include information on campaigns, ad groups, product groups, ads,
8
and/or keywords; can filter by campaigns. Though the entities may be active, archived, or paused,
9
only active entities will return data.
10
 */
11
export async function main(
12
  auth: Pinterest,
13
  ad_account_id: string,
14
  body: {
15
    entity_types?:
16
      | "CAMPAIGN"
17
      | "AD_GROUP"
18
      | "PRODUCT_GROUP"
19
      | "AD"
20
      | "KEYWORD"[];
21
    entity_ids?: string[];
22
    updated_since?: string;
23
    campaign_filter?: {
24
      start_time?: string;
25
      end_time?: string;
26
      name?: string;
27
      campaign_status?:
28
        | "RUNNING"
29
        | "PAUSED"
30
        | "NOT_STARTED"
31
        | "COMPLETED"
32
        | "ADVERTISER_DISABLED"
33
        | "ARCHIVED"
34
        | "DRAFT"
35
        | "DELETED_DRAFT"[];
36
      objective_type?:
37
        | "AWARENESS"
38
        | "CONSIDERATION"
39
        | "VIDEO_VIEW"
40
        | "WEB_CONVERSION"
41
        | "CATALOG_SALES"
42
        | "WEB_SESSIONS"
43
        | "VIDEO_COMPLETION"[];
44
    };
45
    output_format?: "CSV" | "JSON";
46
  },
47
) {
48
  const url = new URL(
49
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/bulk/download`,
50
  );
51

52
  const response = await fetch(url, {
53
    method: "POST",
54
    headers: {
55
      "Content-Type": "application/json",
56
      Authorization: "Bearer " + auth.token,
57
    },
58
    body: JSON.stringify(body),
59
  });
60
  if (!response.ok) {
61
    const text = await response.text();
62
    throw new Error(`${response.status} ${text}`);
63
  }
64
  return await response.json();
65
}
66