0

Create keywords

Published Dec 20, 2024

Create keywords for following entity types(advertiser, campaign, ad group or ad).

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 698 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Create keywords
7
 * Create keywords for following entity types(advertiser, campaign, ad group or ad).
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  ad_account_id: string,
12
  body: {
13
    keywords: {
14
      bid?: number;
15
      match_type:
16
        | "BROAD"
17
        | "PHRASE"
18
        | "EXACT"
19
        | "EXACT_NEGATIVE"
20
        | "PHRASE_NEGATIVE";
21
      value: string;
22
    }[];
23
    parent_id: string;
24
  },
25
) {
26
  const url = new URL(
27
    `https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/keywords`,
28
  );
29

30
  const response = await fetch(url, {
31
    method: "POST",
32
    headers: {
33
      "Content-Type": "application/json",
34
      Authorization: "Bearer " + auth.token,
35
    },
36
    body: JSON.stringify(body),
37
  });
38
  if (!response.ok) {
39
    const text = await response.text();
40
    throw new Error(`${response.status} ${text}`);
41
  }
42
  return await response.json();
43
}
44