0

Save Pin

by
Published Dec 20, 2024

Save a Pin on a board or board section owned by the "operation 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
 * Save Pin
7
 * Save a Pin on a board or board section owned by the "operation user_account".
8
 */
9
export async function main(
10
  auth: Pinterest,
11
  pin_id: string,
12
  ad_account_id: string | undefined,
13
  body: { board_id?: string; board_section_id?: string },
14
) {
15
  const url = new URL(`https://api.pinterest.com/v5/pins/${pin_id}/save`);
16
  for (const [k, v] of [["ad_account_id", ad_account_id]]) {
17
    if (v !== undefined && v !== "" && k !== undefined) {
18
      url.searchParams.append(k, v);
19
    }
20
  }
21
  const response = await fetch(url, {
22
    method: "POST",
23
    headers: {
24
      "Content-Type": "application/json",
25
      Authorization: "Bearer " + auth.token,
26
    },
27
    body: JSON.stringify(body),
28
  });
29
  if (!response.ok) {
30
    const text = await response.text();
31
    throw new Error(`${response.status} ${text}`);
32
  }
33
  return await response.json();
34
}
35