@interactive-inc/notion-client
v3.0.0
Published
Readme
notion-client
A TypeScript ORM for Notion databases. Query, create, update, and delete records with type safety -- no more wrestling with deeply nested API responses.
https://interactive-inc.github.io/open-notion-client/
Why notion-client?
Working directly with the Notion API is painful. A simple text value comes buried in layers of nested objects:
{
"properties": {
"Name": {
"type": "title",
"title": [
{
"text": { "content": "Hello World" },
"annotations": { "bold": false, "italic": false }
}
]
}
}
}notion-client flattens this to { name: "Hello World" } and gives you a clean, familiar ORM interface.
Features
- Type-safe schema definitions with full TypeScript inference
- Intuitive CRUD operations that feel like Prisma or Drizzle
- Powerful query builder with filters, sorting, and pagination
- Automatic conversion between Notion's complex JSON and simple values
- Markdown support for page content with bidirectional conversion
- Built-in memory cache to reduce API calls
- Safe mode (
.safe) that returnsT | Errorinstead of throwing - Automatic retry with exponential backoff for rate-limited requests
- Batch operations with partial failure handling
Installation
vp add @interactive-inc/notion-client @notionhq/clientDevelopment
vp install
vp lint
vp fmt
vp test
vp run check
vp run buildSee Testing for HTTP mocks, regression scenarios, and verification limits. Pull requests run format, lint, type, test, library build, and documentation build checks.
Quick Start
Define Your Schema
import { Client } from "@notionhq/client"
import { NotionTable } from "@interactive-inc/notion-client"
const client = new Client({ auth: process.env.NOTION_TOKEN })
const tasks = new NotionTable({
client,
dataSourceId: "your-database-id",
properties: {
title: { type: "title" },
status: { type: "select", options: ["todo", "doing", "done"] },
priority: { type: "number" },
tags: { type: "multi_select", options: ["bug", "feature"] },
} as const,
})Create
const task = await tasks.create({
properties: {
title: "Implement new feature",
status: "todo",
priority: 1,
tags: ["feature"],
},
})Read
// Find multiple records
const allTasks = await tasks.findMany({
where: { status: "doing" },
sorts: [{ field: "priority", direction: "desc" }],
limit: 10,
})
// Find one record
const task = await tasks.findOne({
where: { title: "Implement new feature" },
})
// Find by ID
const task = await tasks.findById("page-id")Update
// Update one
await tasks.update(task.id, {
properties: { status: "done" },
})
// Update with markdown body
await tasks.update(task.id, {
properties: { status: "done" },
body: "# Done\n\nCompleted this task.",
})
// Clear page body
await tasks.update(task.id, {
properties: {},
body: null,
})
// Update many (processes all matching records, returns BatchResult)
const result = await tasks.updateMany({
where: { status: "todo" },
update: { properties: { status: "doing" } },
})
console.log(result.succeeded.length, result.failed.length)Delete
// Archive one
await tasks.delete(task.id)
// Archive many (processes all matching records, returns BatchResult)
const result = await tasks.deleteMany({ status: "done" })
// Restore archived
await tasks.restore(task.id)Upsert
await tasks.upsert({
where: { title: "Daily standup" },
create: {
properties: { title: "Daily standup", status: "todo" },
},
update: {
properties: { status: "in_progress" },
},
})Queries
Filter Operators
const results = await tasks.findMany({
where: {
priority: { greater_than_or_equal_to: 5 },
status: { does_not_equal: "done" },
tags: { contains: "bug" },
},
})Supported operators: equals, does_not_equal, contains, does_not_contain, starts_with, ends_with, greater_than, less_than, greater_than_or_equal_to, less_than_or_equal_to, before, after, on_or_before, on_or_after, is_empty, is_not_empty
If a where value cannot be converted to a Notion filter, an Error is thrown instead of silently matching all records.
Logical Operators
const results = await tasks.findMany({
where: {
or: [
{ status: "todo" },
{
and: [{ priority: { greater_than_or_equal_to: 5 } }, { tags: { contains: "urgent" } }],
},
],
},
})Property Types
Supported Notion property types and their TypeScript mappings:
title--string-- Page title (required)rich_text--string-- Text contentnumber--number | null-- Numeric valueselect--string | null-- Single selectmulti_select--string[]-- Multiple selectstatus--string | null-- Status fieldcheckbox--boolean-- Booleanurl--string | null-- URL stringemail--string | null-- Email addressphone_number--string | null-- Phone numberdate--{ start, end, timeZone } | null-- Date valuefiles--Array<{ name, url, type }>-- File attachments (typeis"file"or"external")people--Array<{ id }>-- User referencesrelation--string[]-- Relationsformula-- Read-only computed valuecreated_time/last_edited_time-- Read-only timestampscreated_by/last_edited_by-- Read-only user references
Notes on writing values:
title/rich_text-- Passnullto clear the value. Strings over 2,000 characters are split automatically to fit Notion's limit.checkbox--nullis written asfalse.number-- Values outside the configuredmin/maxthrow an error.select/multi_select/status-- Values outside the configuredoptionsthrow an error.files-- Notion-hosted files (type: "file") are preserved when written back.
Markdown Support
Create pages with Markdown content that automatically converts to Notion blocks:
const post = await posts.create({
properties: { title: "Blog Post" },
body: `
# Introduction
This paragraph has **bold** and *italic* text.
## Features
- Feature A
- Feature B
\`\`\`typescript
const hello = "world"
\`\`\`
`,
})Supported when writing: headings, paragraphs, bulleted/numbered lists, to-do items (- [x]), tables, images, equations ($$), code blocks, blockquotes, dividers, and inline formatting. Bodies longer than 100 blocks are appended in chunks automatically.
Heading Level Transformation
import { NotionMarkdown } from "@interactive-inc/notion-client"
const markdown = new NotionMarkdown({
heading_1: "heading_2",
heading_2: "heading_3",
})
// Create a new instance with different mapping
const updated = markdown.withMapping({
heading_1: "heading_3",
})
const posts = new NotionTable({
client,
dataSourceId: "database-id",
properties: { title: { type: "title" } } as const,
markdown,
})Safe Mode
Use .safe to get T | Error instead of exceptions:
const result = await tasks.safe.findById("page-id")
if (result instanceof Error) {
console.error("Failed:", result.message)
} else {
console.log(result.properties())
}All methods are available on .safe: findMany, findOne, findById, create, createMany, update, updateMany, upsert, delete, deleteMany, restore.
Retry
API calls automatically retry on rate limits (429) and server errors (5xx). The Retry-After header is respected when present; otherwise delays use exponential backoff with full jitter:
const tasks = new NotionTable({
client,
dataSourceId: "database-id",
properties: { title: { type: "title" } } as const,
retry: {
maxRetries: 5,
baseDelayMs: 500,
},
})Default: 3 retries, 400ms base delay.
Caching
Reduce API calls with the built-in memory cache:
import { NotionMemoryCache } from "@interactive-inc/notion-client"
const cache = new NotionMemoryCache({
ttlMs: 60_000, // Expire entries after 60s (default: no expiration)
maxEntries: 1000, // Evict oldest entries beyond this count (default: unlimited)
})
const tasks = new NotionTable({
client,
dataSourceId: "database-id",
properties: { title: { type: "title" } } as const,
cache,
})
tasks.clearCache()Zod Schemas
Validate schema definitions at runtime using the separate models entry point:
import { zNotionPropertyConfig } from "@interactive-inc/notion-client/models"
const result = zNotionPropertyConfig.safeParse(untrusted)License
MIT
