Overview
The Notion CMS module provides the core API for interacting with Notion as a headless CMS. It handles database queries, block retrieval, asset downloading, and response normalization.Key Concepts
Data Sources
With Notion API version 2025-09-03, databases contain one or more data sources. This module automatically resolves the primary data source ID for each database and caches it for performance.Functions
ensureFullResponse()
Converts partial Notion API responses to full responses with type safety.
The Notion API response that may be partial or full
The full response object with all properties populated
getDataSourceId()
Retrieves and caches the primary data source ID for a Notion database.
The Notion database ID (32-character UUID)
The data source ID for the database’s primary data source
- Automatic caching to avoid repeated API calls
- Console logging for debugging
- Error handling for databases without data sources
queryNotionDatabase()
Queries a Notion database and returns all matching pages (automatically paginated).
The Notion database ID to query
Optional query parameters (filter, sorts, page_size, etc.)
Array of all pages matching the query criteria
- Automatic pagination (fetches all results)
- Filters out non-page objects
- Ensures full response objects (not partial)
- Automatically resolves data source ID
getBlock()
Recursively retrieves all blocks (and their children) for a given page or block ID.
The block or page ID to retrieve content from
Array of block objects with nested children and downloaded assets
- Recursive child block retrieval
- Automatic asset downloading for images, videos, PDFs, and audio
- Local file URL remapping
- Handles pagination (up to 100 blocks per request)
image, video, audio, pdf), the function:
- Downloads the file to the local filesystem
- Remaps the URL to the local path
- Adds width/height parameters for images
Internal Functions
getBlockChildren()
Retrieves immediate children of a block (non-recursive, internal use only).
Type Guards
The module uses TypeScript type guards to ensure runtime safety:Caching
Data source IDs are cached in aMap to avoid repeated API calls:
Error Handling
Missing Data Sources:Performance Considerations
- Pagination: All queries automatically fetch all pages (not limited to 100 results)
- Parallel Processing: Block children are processed in parallel using
Promise.all() - Caching: Data source IDs are cached to minimize API calls
- Asset Downloads: Only downloads files that don’t already exist locally
Related Modules
- Notion Client - The underlying API client
- Notion Parse - Convert blocks to Markdown
- Notion CMS Asset - Asset download handling
- Notion CMS Page - Page property extraction
Source Reference
File:src/lib/notion-cms.ts:1-182
Key Dependencies:
@notionhq/client- Notion API types and client./notion-client- Shared API client instance./notion-cms-asset- Asset downloading functionality