0

Create custom audience

by
Published Dec 20, 2024

Create a custom audience and find the audiences you want your ads to reach.

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Create custom audience
7
 * Create a custom audience and find the audiences you want your ads to reach.
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  body: {
13
    ad_account_id?: string;
14
    name?: string;
15
    rule?: {
16
      country?: string;
17
      customer_list_id?: string;
18
      engagement_domain?: string[];
19
      engagement_type?: string;
20
      event?: string;
21
      event_data?: {
22
        currency?:
23
          | "UNK"
24
          | "USD"
25
          | "GBP"
26
          | "CAD"
27
          | "EUR"
28
          | "AUD"
29
          | "NZD"
30
          | "SEK"
31
          | "ILS"
32
          | "CHF"
33
          | "HKD"
34
          | "JPY"
35
          | "SGD"
36
          | "KRW"
37
          | "NOK"
38
          | "DKK"
39
          | "PLN"
40
          | "RON"
41
          | "HUF"
42
          | "CZK"
43
          | "BRL"
44
          | "MXN"
45
          | "ARS"
46
          | "CLP"
47
          | "COP"
48
          | "INR"
49
          | "TRY";
50
        lead_type?: string;
51
        line_items?: {
52
          product_brand?: string;
53
          product_category?: string;
54
          product_id?: number;
55
          product_name?: string;
56
          product_price?: string;
57
          product_quantity?: number;
58
          product_variant?: string;
59
          product_variant_id?: string;
60
        };
61
        order_id?: string;
62
        order_quantity?: number;
63
        page_name?: string;
64
        promo_code?: string;
65
        property?: string;
66
        search_query?: string;
67
        value?: string;
68
        video_title?: string;
69
      };
70
      percentage?: number;
71
      pin_id?: string[];
72
      prefill?: false | true;
73
      retention_days?: number;
74
      seed_id?: string[];
75
      url?: string[];
76
      visitor_source_id?: string;
77
      event_source?: {};
78
      ingestion_source?: {};
79
      engager_type?: number;
80
      campaign_id?: string[];
81
      ad_id?: string[];
82
      objective_type?:
83
        | "AWARENESS"
84
        | "CONSIDERATION"
85
        | "VIDEO_VIEW"
86
        | "WEB_CONVERSION"
87
        | "CATALOG_SALES"
88
        | "WEB_SESSIONS"
89
        | "VIDEO_COMPLETION"[];
90
      ad_account_id?: string;
91
    };
92
  } & {
93
    sharing_type: "CUSTOM" | "SYNDICATED";
94
    data_party: "1p" | "3p";
95
    category?: string;
96
  },
97
) {
98
  const url = new URL(
99
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/audiences/custom`,
100
  );
101

102
  const response = await fetch(url, {
103
    method: "POST",
104
    headers: {
105
      "Content-Type": "application/json",
106
      Authorization: "Bearer " + auth.token,
107
    },
108
    body: JSON.stringify(body),
109
  });
110
  if (!response.ok) {
111
    const text = await response.text();
112
    throw new Error(`${response.status} ${text}`);
113
  }
114
  return await response.json();
115
}
116