NotionAPI Public Data

node v18.11.0
version: 12.0.0
endpointsharetweet
const tonicExpress = require("@runkit/runkit/express-endpoint/1.0.0"); const app = tonicExpress(module.exports); const cors = require('cors') const axios = require("axios"); const NotionPageToHtml = require('notion-page-to-html'); let apiUrl = 'https://api.notion.com'; let apiKey = process.env.NOTION_API; let workDb = process.env.NOTION_WORK_DB; let blogDb = process.env.NOTION_BLOG_DB; let corsOptions = { origin: "https://ltrademark.com", methods: ['GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS'], credentials:true, allowedHeaders: ['Content-Type', 'Origin', 'X-Requested-With', 'Accept', 'x-client-key', 'x-client-token', 'x-client-secret', 'Authorization'], optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204 } async function fetchData(db) { try { const config = { method: 'post', maxBodyLength: Infinity, url:`${apiUrl}/v1/databases/${db}/query`, headers: { 'Notion-Version': '2022-06-28', 'Authorization': `Bearer ${apiKey}` } }; const response = await axios.request(config); console.log(JSON.stringify(response.data.results)) return JSON.stringify(response.data.results); } catch (err) { console.error('things went horribly', err) } } async function fetchPageData(id) { try { let options = { bodyContentOnly: true }; const { title, icon, cover, html } = await NotionPageToHtml.convert(`https://www.notion.so/ltrademark/${id}`, options); const pageData = { title: title, icon: icon, hero: cover, contents: html }; console.log(cover, html) return pageData; } catch (err) { console.error('ur page doesnt exist lil bro', err) } } app.use(cors(corsOptions)); app.get("/", (req, res) => res.send()) app.get("/work", async (req, res) => { let data = await fetchData(workDb) res.send(data) }) app.get("/work/:pageId", async (req, res) => { let data = await fetchPageData(req.params.pageId) res.send(data) }) app.get("/blog", async (req, res) => { let data = await fetchData(blogDb) res.send(data) }) app.get("/blog/:pageId", async (req, res) => { let data = await fetchPageData(req.params.pageId) res.send(data) })
Loading…

1 comment

  • posted 4 months ago by ltrademark
    Runkit seems to add an arbitrary rate-limit to some of these requests, but I can't figure out how and when I'm hitting them until I do. Just something to note.

sign in to comment