0

Create catalog

by
Published Dec 20, 2024

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