//native
type Pinterest = {
token: string;
};
/**
* Create async request for an analytics report using a template
* This takes a template ID and an optional custom timeframe and constructs an asynchronous report based on the
template. It returns a token that you can use to download the report when it is ready.
*/
export async function main(
auth: Pinterest,
ad_account_id: string,
template_id: string,
start_date: string | undefined,
end_date: string | undefined,
granularity: "TOTAL" | "DAY" | "HOUR" | "WEEK" | "MONTH" | undefined,
) {
const url = new URL(
`https://api.pinterest.com/v5/ad_accounts/${ad_account_id}/templates/${template_id}/reports`,
);
for (const [k, v] of [
["start_date", start_date],
["end_date", end_date],
["granularity", granularity],
]) {
if (v !== undefined && v !== "" && k !== undefined) {
url.searchParams.append(k, v);
}
}
const response = await fetch(url, {
method: "POST",
headers: {
Authorization: "Bearer " + auth.token,
},
body: undefined,
});
if (!response.ok) {
const text = await response.text();
throw new Error(`${response.status} ${text}`);
}
return await response.json();
}
Submitted by hugo697 536 days ago