import { Client } from "@notionhq/client";
async function main() {
const token = process.env.NOTION_API_KEY;
const pageId = process.env.NOTION_PAGE_ID;
const propertyId = process.env.NOTION_PROPERTY_ID;
if (!token || !pageId || !propertyId) {
throw new Error("Set NOTION_API_KEY, NOTION_PAGE_ID, and NOTION_PROPERTY_ID.");
}
const notion = new Client({ auth: token });
let response = await notion.pages.properties.retrieve({
page_id: pageId,
property_id: propertyId,
page_size: 100,
});
if (response.object === "property_item") {
if (response.type === "formula" && response.formula.type === "unsupported") {
throw new Error("This formula cannot be calculated by the API.");
}
console.log(JSON.stringify(response, null, 2));
return;
}
while (true) {
for (const item of response.results) {
console.log(JSON.stringify(item, null, 2));
}
if (!response.has_more) {
if (response.property_item.type === "rollup") {
const rollup = response.property_item.rollup;
if (rollup.type === "unsupported" || rollup.type === "incomplete") {
throw new Error("This rollup has no completed calculation. See the rollup limits.");
}
console.log("Final rollup:", JSON.stringify(rollup, null, 2));
}
return;
}
if (!response.next_cursor) {
throw new Error("The response has more items but no next cursor.");
}
response = await notion.pages.properties.retrieve({
page_id: pageId,
property_id: propertyId,
page_size: 100,
start_cursor: response.next_cursor,
});
if (response.object !== "list") {
throw new Error("The property response changed while reading it. Start again.");
}
}
}
main().catch((error) => {
console.error(error instanceof Error ? error.message : "Could not read the property.");
process.exitCode = 1;
});