Skip to contentnotionic
Docs menu

Guides

Fetching pages

Get blocks and page metadata from the Notion API.

notionic/fetch turns the Notion API's one-level-at-a-time blocks into the tree NotionRenderer needs.

fetchPage

Returns the page's metadata and every block of it:

TypeScript
const { page, blocks } = await fetchPage(notion, pageId)
page.title          // "Fetching pages"page.icon           // { type: "emoji", emoji: "๐Ÿ“ฅ" } or nullpage.cover          // the cover image, or nullpage.lastEditedTime // ISO timestamppage.properties     // database properties, as the API returns them

It throws if the page doesn't exist or isn't shared with your integration.

fetchBlocks

Returns only the blocks, for a page or any block:

TypeScript
const blocks = await fetchBlocks(notion, pageId, { maxDepth: 5, concurrency: 3 })
  • Nesting. Nested blocks are attached as children, up to maxDepth levels below the top (default 5).
  • Rate limits. At most concurrency requests run at once per level (default 3).
  • Child pages and databases are separate documents, so their content isn't fetched. Each child page is retrieved for its icon, attached as linkedPage.
  • Linked pages. For each link to page block, the linked page's title and icon are attached as linkedPage, so the link has a label. Pages the integration can't read are skipped.
  • Bookmarks, embeds and link previews. For each one, the page behind it is fetched and its title, description, image and favicon are attached as bookmarkPreview, so it renders as a card. A page that's slow (over 5 seconds), unreachable or untitled renders as a one-line card with the URL, and private network addresses are never fetched. Pass bookmarkPreviews: false to make no requests outside Notion.

Link favicons

Links to other sites can show the site's favicon before their text, as in Notion. It's off by default: fetch the icons, then show them.

TypeScript
const blocks = await fetchBlocks(notion, pageId, { linkFavicons: true })
<NotionRenderer blocks={blocks} linkFavicons />
  • Where they come from. Each linked host is looked up once, at Google's favicon service and then DuckDuckGo's, with a 3 second timeout. The icon is attached to the blocks as a data URL of a few KB.
  • No requests when the page renders. The icons are cached with the blocks, by whatever caches fetchBlocks.
  • Which links. Only absolute http(s) links to other sites get one. Paths on your site, anchors, mailto links and Notion pages never do.
  • Subdomains. A subdomain without a favicon gets its site's: you.substack.com shows Substack's. It never goes above the site's own domain, so co.uk is never asked.
  • No icon, or no answer. A site with no favicon gets a muted globe. If the favicon services can't answer (a timeout, an outage), the host is left out, so its link shows no icon and the next fetch asks again. A link whose host wasn't looked up shows no icon either, so &lt;NotionRenderer linkFavicons&gt; without the fetchBlocks option shows none.
  • Private hosts (localhost, 10.x, *.local) are never sent to the favicon services.
  • The site's own icon. The services keep their own copies of icons, which can be stale or re-encoded, like a rounded icon stored as a white-cornered JPEG. Pass linkFavicons: { siteIcons: true } to use the icon each site declares for itself instead. fetchBlocks then reads the site's home page and takes its SVG icon, or the smallest PNG of 32px or more (never apple-touch-icon), or its /favicon.ico, and asks the services only when none of those loads.
  • Why that's off by default. With siteIcons, notionic fetches URLs the linked sites choose. It refuses private addresses, both literal ones and names that resolve to one (where the runtime has node:dns), but a DNS server can answer differently between that check and the request. Turn it on only where your server can't reach anything private worth protecting, or where outbound traffic is filtered.
  • Your own icons. The icons are the sites' own favicons, as they are. To draw your own for some sites, see Customizing blocks (opens in a new tab).

Caching

notionic doesn't cache. Use your framework's cache: in Next.js, export const revalidate or "use cache".

Uploaded files

Notion's URLs for uploaded images and files expire after an hour, which is shorter than most pages are cached. Pass transformFileUrl to point them at a route of yours that fetches a fresh URL:

TypeScript
<NotionRenderer  blocks={blocks}  transformFileUrl={({ blockId }) => `/api/notion/file/${blockId}`}/>

The route retrieves the block with notion.blocks.retrieve and redirects to its file's current URL. Cache the redirect for less than the time left before the URL expires. External image URLs are never rewritten.