Readme

Get All Rows of a Database in Notion

Reference: Query a Database

How to get an access token: https://developers.notion.com/reference/create-a-token

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
export const getAllNotionDbRows = async (databaseId, notionAccessToken) => {
if (!databaseId) {
console.error("No Database ID Provided");
return null;
}
if (!notionAccessToken) {
console.warn("Notion Access Token is invalid");
return null;
}
const { default: axios } = await import("npm:axios");
const getNotionDbContent = async (start_cursor = undefined) => {
return await axios({
method: "POST",
url: `https://api.notion.com/v1/databases/${databaseId}/query`,
headers: {
Authorization: `Bearer ${notionAccessToken}`,
"Notion-Version": "2022-06-28",
"Content-Type": "application/json",
},
data: {
start_cursor,
},
});
};
let result = await getNotionDbContent();
let items = result.data.results;
while (result?.data?.has_more) {
result = await getNotionDbContent(result.data.next_cursor);
items = items.concat(result?.data?.results);
}
const { length } = items;
console.log(
length
? `Successfully fetched ${length} row${length === 1 ? "" : "s"}`
: "No rows found",
);
return items;
};
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
October 23, 2023