Apply production-ready @notionhq/client SDK patterns for TypeScript and Python. Use when implementing Notion integrations, building database queries with filters and sorts, handling pagination, constructing rich text blocks, or establishing team coding standards for Notion API usage. Trigger with "notion SDK patterns", "notion best practices", "notion code patterns", "idiomatic notion", "notion typescript", "notion python SDK".
Production-ready patterns for the official Notion SDK (@notionhq/client for TypeScript, notion-client for Python) covering client initialization, database queries with filters and sorts, cursor-based pagination, rich text construction, block manipulation, and type-safe error handling using SDK error codes.
@notionhq/client v2.x installed, or Python 3.9+ with notion-clientNOTION_TOKEN) from notion.so/my-integrationsSet up the SDK client and execute filtered, sorted database queries.
TypeScript — Client initialization:
import { Client } from '@notionhq/client';
const notion = new Client({ auth: process.env.NOTION_TOKEN });
Database query with filter and sort:
const response = await notion.databases.query({
database_id,
filter: {
property: 'Status',
select: {
equals: 'Active',
},
},
sorts: [
{
property: 'Created',
direction: 'descending',
},
],
});
Compound filters combine conditions with and/or:
const response = await notion.databases.query({
database_id,
filter: {
and: [
{ property: 'Status', select: { equals: 'Active' } },
{ property: 'Priority', select: { does_not_equal: 'Low' } },
{ property: 'Assignee', people: { is_not_empty: true } },
],
},
sorts: [
{ property: 'Priority', direction: 'ascending' },
{ property: 'Created', direction: 'descending' },
],
});
Python — Client initialization and query:
from notion_client import Client
notion = Client(auth=os.environ["NOTION_TOKEN"])
results = notion.databases.query(
database_id=db_id,
filter={
"property": "Status",
"select": {"equals": "Active"},
},
sorts=[{"property": "Created", "direction": "descending"}],
)
The Notion API returns at most 100 results per request. Use cursor-based pagination to retrieve all records.
Cursor-based pagination:
let cursor: string | undefined;
do {
const { results, next_cursor, has_more } = await notion.databases.query({
database_id,
start_cursor: cursor,
});
// Process each page of results
for (const page of results) {
console.log(page.id);
}
cursor = has_more && next_cursor ? next_cursor : undefined;
} while (cursor);
Reusable pagination helper (generic):
type PaginatedFn<T> = (args: { start_cursor?: string }) => Promise<{
results: T[];
has_more: boolean;
next_cursor: string | null;
}>;
async function collectPaginated<T>(fn: PaginatedFn<T>): Promise<T[]> {
const all: T[] = [];
let cursor: string | undefined;
do {
const response = await fn({ start_cursor: cursor });
all.push(...response.results);
cursor = response.has_more && response.next_cursor
? response.next_cursor
: undefined;
} while (cursor);
return all;
}
// Usage — collect all pages from a database
const allPages = await collectPaginated((args) =>
notion.databases.query({ database_id: 'db-id', ...args })
);
Read block children (page content):
const blocks = await notion.blocks.children.list({
block_id: pageId,
});
for (const block of blocks.results) {
if ('type' in block) {
console.log(block.type, block.id);
}
}
Append blocks to a page:
await notion.blocks.children.append({
block_id: pageId,
children: [
{
type: 'paragraph',
paragraph: {
rich_text: [{ text: { content: 'Hello from the SDK' } }],
},
},
{
type: 'heading_2',
heading_2: {
rich_text: [{ text: { content: 'Section Title' } }],
},
},
{
type: 'bulleted_list_item',
bulleted_list_item: {
rich_text: [{ text: { content: 'First item' } }],
},
},
],
});
Rich text with annotations and links:
const richTextBlock = {
type: 'text' as const,
text: {
content: 'Hello',
link: { url: 'https://developers.notion.com' },
},
annotations: {
bold: true,
italic: false,
strikethrough: false,
underline: false,
code: false,
color: 'default' as const,
},
};
Python — block manipulation:
# List block children
blocks = notion.blocks.children.list(block_id=page_id)
# Append blocks
notion.blocks.children.append(
block_id=page_id,
children=[
{
"type": "paragraph",
"paragraph": {
"rich_text": [{"text": {"content": "Added via Python SDK"}}]
},
}
],
)
Use the SDK's built-in error type guards instead of catching generic exceptions.
TypeScript — type-safe error handling:
import {
isNotionClientError,
APIErrorCode,
ClientErrorCode,
} from '@notionhq/client';
try {
const page = await notion.pages.retrieve({ page_id: pageId });
} catch (error) {
if (isNotionClientError(error)) {
switch (error.code) {
case APIErrorCode.ObjectNotFound:
console.error('Page not found — ensure it is shared with the integration');
break;
case APIErrorCode.Unauthorized:
console.error('Invalid token — regenerate at notion.so/my-integrations');
break;
case APIErrorCode.RateLimited:
console.error(`Rate limited — retry after ${error.headers?.['retry-after']}s`);
break;
case APIErrorCode.ValidationError:
console.error(`Invalid request: ${error.message}`);
break;
case APIErrorCode.ConflictError:
console.error('Conflict — resource was modified by another request');
break;
case ClientErrorCode.RequestTimeout:
console.error('Request timed out — increase timeoutMs or check network');
break;
default:
console.error(`Notion error [${error.code}]: ${error.message}`);
}
} else {
throw error; // Re-throw non-Notion errors
}
}
Python — error handling:
from notion_client import Client, APIResponseError