0

Update Pin

by
Published Dec 20, 2024

Update a pin owned by the "operating user_account".

Script pinterest Verified

The script

Submitted by hugo697 Bun
Verified 536 days ago
1
//native
2
type Pinterest = {
3
  token: string;
4
};
5
/**
6
 * Update Pin
7
 * Update a pin owned by the "operating user_account".
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  pin_id: string,
12
  ad_account_id: string | undefined,
13
  body: {
14
    alt_text?: string;
15
    board_id?: string;
16
    board_section_id?: string;
17
    description?: string;
18
    link?: string;
19
    title?: string;
20
    carousel_slots?: { title?: string; description?: string; link?: string }[];
21
    note?: string;
22
  },
23
) {
24
  const url = new URL(`https://api.pinterest.com/v5/pins/${pin_id}`);
25
  for (const [k, v] of [["ad_account_id", ad_account_id]]) {
26
    if (v !== undefined && v !== "" && k !== undefined) {
27
      url.searchParams.append(k, v);
28
    }
29
  }
30
  const response = await fetch(url, {
31
    method: "PATCH",
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