notion-database 2.0.dev0__tar.gz → 2.0.dev1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- notion_database-2.0.dev1/PKG-INFO +355 -0
- notion_database-2.0.dev1/README.md +332 -0
- notion_database-2.0.dev1/notion_database/__init__.py +73 -0
- notion_database-2.0.dev1/notion_database/api/blocks.py +141 -0
- notion_database-2.0.dev1/notion_database/api/comments.py +73 -0
- notion_database-2.0.dev1/notion_database/api/databases.py +209 -0
- notion_database-2.0.dev1/notion_database/api/pages.py +183 -0
- notion_database-2.0.dev1/notion_database/api/search.py +142 -0
- notion_database-2.0.dev1/notion_database/api/users.py +78 -0
- notion_database-2.0.dev1/notion_database/client.py +84 -0
- notion_database-2.0.dev1/notion_database/const/__init__.py +46 -0
- notion_database-2.0.dev1/notion_database/exceptions.py +51 -0
- notion_database-2.0.dev1/notion_database/http/client.py +109 -0
- notion_database-2.0.dev1/notion_database/models/__init__.py +20 -0
- notion_database-2.0.dev1/notion_database/models/blocks.py +528 -0
- notion_database-2.0.dev1/notion_database/models/filters.py +420 -0
- notion_database-2.0.dev1/notion_database/models/icons.py +70 -0
- notion_database-2.0.dev1/notion_database/models/properties.py +548 -0
- notion_database-2.0.dev1/notion_database/models/rich_text.py +160 -0
- notion_database-2.0.dev1/notion_database/models/sorts.py +75 -0
- notion_database-2.0.dev1/notion_database.egg-info/PKG-INFO +355 -0
- notion_database-2.0.dev1/notion_database.egg-info/SOURCES.txt +35 -0
- notion_database-2.0.dev1/notion_database.egg-info/requires.txt +1 -0
- {notion_database-2.0.dev0 → notion_database-2.0.dev1}/notion_database.egg-info/top_level.txt +0 -1
- notion_database-2.0.dev1/pyproject.toml +41 -0
- notion_database-2.0.dev1/tests/test_models_blocks.py +179 -0
- notion_database-2.0.dev1/tests/test_models_filters.py +181 -0
- notion_database-2.0.dev1/tests/test_models_icons.py +28 -0
- notion_database-2.0.dev1/tests/test_models_properties.py +192 -0
- notion_database-2.0.dev1/tests/test_models_rich_text.py +102 -0
- notion_database-2.0.dev1/tests/test_models_sorts.py +36 -0
- notion_database-2.0.dev0/PKG-INFO +0 -132
- notion_database-2.0.dev0/README.md +0 -110
- notion_database-2.0.dev0/notion_database/NotionDatabase.py +0 -107
- notion_database-2.0.dev0/notion_database/__init__.py +0 -7
- notion_database-2.0.dev0/notion_database/components/request.py +0 -58
- notion_database-2.0.dev0/notion_database/const/__init__.py +0 -1
- notion_database-2.0.dev0/notion_database/const/query.py +0 -38
- notion_database-2.0.dev0/notion_database/service/block.py +0 -43
- notion_database-2.0.dev0/notion_database/service/children.py +0 -520
- notion_database-2.0.dev0/notion_database/service/cover.py +0 -43
- notion_database-2.0.dev0/notion_database/service/database.py +0 -179
- notion_database-2.0.dev0/notion_database/service/icon.py +0 -61
- notion_database-2.0.dev0/notion_database/service/page.py +0 -118
- notion_database-2.0.dev0/notion_database/service/properties.py +0 -220
- notion_database-2.0.dev0/notion_database/service/search.py +0 -78
- notion_database-2.0.dev0/notion_database/utils/__init__.py +0 -0
- notion_database-2.0.dev0/notion_database/utils/deprecate.py +0 -21
- notion_database-2.0.dev0/notion_database.egg-info/PKG-INFO +0 -132
- notion_database-2.0.dev0/notion_database.egg-info/SOURCES.txt +0 -31
- notion_database-2.0.dev0/notion_database.egg-info/requires.txt +0 -2
- notion_database-2.0.dev0/setup.py +0 -34
- notion_database-2.0.dev0/tests/__init__.py +0 -1
- notion_database-2.0.dev0/tests/test_children.py +0 -286
- notion_database-2.0.dev0/tests/test_cover.py +0 -22
- notion_database-2.0.dev0/tests/test_icon.py +0 -33
- notion_database-2.0.dev0/tests/test_properties.py +0 -245
- {notion_database-2.0.dev0 → notion_database-2.0.dev1}/LICENSE +0 -0
- {notion_database-2.0.dev0/notion_database/components → notion_database-2.0.dev1/notion_database/api}/__init__.py +0 -0
- {notion_database-2.0.dev0 → notion_database-2.0.dev1}/notion_database/const/color.py +0 -0
- {notion_database-2.0.dev0/notion_database/service → notion_database-2.0.dev1/notion_database/http}/__init__.py +0 -0
- {notion_database-2.0.dev0 → notion_database-2.0.dev1}/notion_database.egg-info/dependency_links.txt +0 -0
- {notion_database-2.0.dev0 → notion_database-2.0.dev1}/setup.cfg +0 -0
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: notion-database
|
|
3
|
+
Version: 2.0.dev1
|
|
4
|
+
Summary: Python client for the Notion API
|
|
5
|
+
Author-email: minwook-shin <minwook0106@gmail.com>
|
|
6
|
+
License: LGPLv3
|
|
7
|
+
Project-URL: Homepage, https://github.com/minwook-shin/notion-database
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/minwook-shin/notion-database/issues
|
|
9
|
+
Classifier: Programming Language :: Python :: 3
|
|
10
|
+
Classifier: License :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
17
|
+
Classifier: Development Status :: 4 - Beta
|
|
18
|
+
Requires-Python: >=3.10
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: requests<3.0,>=2.33.1
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
[](https://github.com/minwook-shin/notion-database/actions/workflows/python-publish.yml)
|
|
25
|
+
|
|
26
|
+
> [!IMPORTANT]
|
|
27
|
+
> **Upgrading from v1.4?**
|
|
28
|
+
> Version 2.0 is a full redesign with a new API — `NotionClient` replaces the old `Page`, `Database`, and `Search` classes.
|
|
29
|
+
> v1.4.x will continue to receive security fixes only. See the [migration table](#install) and [CHANGELOG](CHANGELOG.md) for details.
|
|
30
|
+
|
|
31
|
+
# notion-database
|
|
32
|
+
|
|
33
|
+
> Python client for the Notion API — easy to use, 1-to-1 API mapping, AI/MCP friendly
|
|
34
|
+
|
|
35
|
+
## Install
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install notion-database==2.0.dev1
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Quick start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
from notion_database import (
|
|
45
|
+
NotionClient,
|
|
46
|
+
PropertyValue,
|
|
47
|
+
PropertySchema,
|
|
48
|
+
BlockContent,
|
|
49
|
+
RichText,
|
|
50
|
+
Filter,
|
|
51
|
+
Sort,
|
|
52
|
+
Icon,
|
|
53
|
+
Cover,
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
client = NotionClient("secret_xxx")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Databases
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
# Retrieve
|
|
63
|
+
db = client.databases.retrieve("database-id")
|
|
64
|
+
|
|
65
|
+
# Create
|
|
66
|
+
db = client.databases.create(
|
|
67
|
+
parent={"type": "page_id", "page_id": "page-id"},
|
|
68
|
+
title=[RichText.text("My Database")],
|
|
69
|
+
properties={
|
|
70
|
+
"Name": PropertySchema.title(),
|
|
71
|
+
"Status": PropertySchema.select([
|
|
72
|
+
{"name": "Active", "color": "green"},
|
|
73
|
+
{"name": "Done", "color": "gray"},
|
|
74
|
+
]),
|
|
75
|
+
"Score": PropertySchema.number("number"),
|
|
76
|
+
"Due": PropertySchema.date(),
|
|
77
|
+
},
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
# Query with filter and sort
|
|
81
|
+
results = client.databases.query(
|
|
82
|
+
"database-id",
|
|
83
|
+
filter=Filter.select("Status").equals("Active"),
|
|
84
|
+
sorts=[Sort.descending("Score")],
|
|
85
|
+
)
|
|
86
|
+
pages = results["results"]
|
|
87
|
+
|
|
88
|
+
# Compound filter
|
|
89
|
+
results = client.databases.query(
|
|
90
|
+
"database-id",
|
|
91
|
+
filter=Filter.and_([
|
|
92
|
+
Filter.select("Status").equals("Active"),
|
|
93
|
+
Filter.number("Score").greater_than(80),
|
|
94
|
+
]),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
# Auto-paginate (returns all pages at once)
|
|
98
|
+
all_pages = client.databases.query_all("database-id")
|
|
99
|
+
|
|
100
|
+
# Update schema
|
|
101
|
+
client.databases.update(
|
|
102
|
+
"database-id",
|
|
103
|
+
title=[RichText.text("Renamed DB")],
|
|
104
|
+
)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Pages
|
|
108
|
+
|
|
109
|
+
```python
|
|
110
|
+
# Create
|
|
111
|
+
page = client.pages.create(
|
|
112
|
+
parent={"database_id": "database-id"},
|
|
113
|
+
properties={
|
|
114
|
+
"Name": PropertyValue.title("Hello, Notion 2.0!"),
|
|
115
|
+
"Status": PropertyValue.select("Active"),
|
|
116
|
+
"Score": PropertyValue.number(95),
|
|
117
|
+
"Due": PropertyValue.date("2024-12-31"),
|
|
118
|
+
"Done": PropertyValue.checkbox(False),
|
|
119
|
+
},
|
|
120
|
+
icon=Icon.emoji("🚀"),
|
|
121
|
+
cover=Cover.external("https://example.com/cover.jpg"),
|
|
122
|
+
children=[
|
|
123
|
+
BlockContent.heading_1("Introduction"),
|
|
124
|
+
BlockContent.paragraph("This page was created via notion-database 2.0."),
|
|
125
|
+
],
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# Retrieve
|
|
129
|
+
page = client.pages.retrieve("page-id")
|
|
130
|
+
|
|
131
|
+
# Update properties
|
|
132
|
+
client.pages.update(
|
|
133
|
+
"page-id",
|
|
134
|
+
properties={
|
|
135
|
+
"Status": PropertyValue.select("Done"),
|
|
136
|
+
"Done": PropertyValue.checkbox(True),
|
|
137
|
+
},
|
|
138
|
+
)
|
|
139
|
+
|
|
140
|
+
# Archive / restore
|
|
141
|
+
client.pages.archive("page-id")
|
|
142
|
+
client.pages.archive("page-id", archived=False)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Blocks
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
# Append content to a page
|
|
149
|
+
client.blocks.append_children(
|
|
150
|
+
"page-id",
|
|
151
|
+
children=[
|
|
152
|
+
BlockContent.heading_2("Section"),
|
|
153
|
+
BlockContent.paragraph([
|
|
154
|
+
RichText.text("Normal text, "),
|
|
155
|
+
RichText.text("bold", bold=True),
|
|
156
|
+
RichText.text(", and "),
|
|
157
|
+
RichText.text("italic", italic=True),
|
|
158
|
+
]),
|
|
159
|
+
BlockContent.bulleted_list_item("First item"),
|
|
160
|
+
BlockContent.bulleted_list_item("Second item"),
|
|
161
|
+
BlockContent.to_do("Finish docs", checked=False),
|
|
162
|
+
BlockContent.code("print('hello')", language="python"),
|
|
163
|
+
BlockContent.divider(),
|
|
164
|
+
BlockContent.image("https://example.com/image.png", caption="Fig 1"),
|
|
165
|
+
BlockContent.column_list([
|
|
166
|
+
[BlockContent.paragraph("Left column")],
|
|
167
|
+
[BlockContent.paragraph("Right column")],
|
|
168
|
+
]),
|
|
169
|
+
],
|
|
170
|
+
)
|
|
171
|
+
|
|
172
|
+
# Retrieve children (single page)
|
|
173
|
+
response = client.blocks.retrieve_children("page-id")
|
|
174
|
+
blocks = response["results"]
|
|
175
|
+
|
|
176
|
+
# Auto-paginate all children
|
|
177
|
+
all_blocks = client.blocks.retrieve_all_children("page-id")
|
|
178
|
+
|
|
179
|
+
# Delete a block
|
|
180
|
+
client.blocks.delete("block-id")
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Search
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
# Search all
|
|
187
|
+
results = client.search.search("Project")
|
|
188
|
+
|
|
189
|
+
# Filter by type
|
|
190
|
+
db_results = client.search.search_databases("Project")
|
|
191
|
+
page_results = client.search.search_pages("Meeting notes")
|
|
192
|
+
|
|
193
|
+
# Auto-paginate all results
|
|
194
|
+
all_results = client.search.search_all("Q1")
|
|
195
|
+
```
|
|
196
|
+
|
|
197
|
+
## Users
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
# Current bot
|
|
201
|
+
me = client.users.me()
|
|
202
|
+
|
|
203
|
+
# List all workspace users
|
|
204
|
+
all_users = client.users.list_all()
|
|
205
|
+
|
|
206
|
+
# Retrieve by ID
|
|
207
|
+
user = client.users.retrieve("user-id")
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
## Comments
|
|
211
|
+
|
|
212
|
+
```python
|
|
213
|
+
# Retrieve comments on a page
|
|
214
|
+
comments = client.comments.retrieve("page-id")
|
|
215
|
+
|
|
216
|
+
# Post a comment
|
|
217
|
+
client.comments.create(
|
|
218
|
+
parent={"page_id": "page-id"},
|
|
219
|
+
rich_text=[RichText.text("Great work!")],
|
|
220
|
+
)
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
## Filters reference
|
|
224
|
+
|
|
225
|
+
```python
|
|
226
|
+
# Text / title
|
|
227
|
+
Filter.text("Name").equals("Alice")
|
|
228
|
+
Filter.text("Name").contains("Al")
|
|
229
|
+
Filter.text("Name").starts_with("A")
|
|
230
|
+
Filter.text("Name").is_empty()
|
|
231
|
+
|
|
232
|
+
# Number
|
|
233
|
+
Filter.number("Score").greater_than(80)
|
|
234
|
+
Filter.number("Score").less_than_or_equal_to(100)
|
|
235
|
+
|
|
236
|
+
# Checkbox
|
|
237
|
+
Filter.checkbox("Done").equals(True)
|
|
238
|
+
|
|
239
|
+
# Select / status
|
|
240
|
+
Filter.select("Status").equals("Active")
|
|
241
|
+
Filter.status("Status").does_not_equal("Archived")
|
|
242
|
+
|
|
243
|
+
# Multi-select
|
|
244
|
+
Filter.multi_select("Tags").contains("python")
|
|
245
|
+
|
|
246
|
+
# Date
|
|
247
|
+
Filter.date("Due").before("2025-01-01")
|
|
248
|
+
Filter.date("Due").past_week()
|
|
249
|
+
Filter.date("Due").next_month()
|
|
250
|
+
|
|
251
|
+
# Timestamp
|
|
252
|
+
Filter.created_time().after("2024-01-01")
|
|
253
|
+
Filter.last_edited_time().past_week()
|
|
254
|
+
|
|
255
|
+
# Compound
|
|
256
|
+
Filter.and_([
|
|
257
|
+
Filter.select("Status").equals("Active"),
|
|
258
|
+
Filter.number("Score").greater_than(80),
|
|
259
|
+
])
|
|
260
|
+
Filter.or_([
|
|
261
|
+
Filter.text("Name").contains("Alice"),
|
|
262
|
+
Filter.text("Name").contains("Bob"),
|
|
263
|
+
])
|
|
264
|
+
|
|
265
|
+
# Nested compound
|
|
266
|
+
Filter.and_([
|
|
267
|
+
Filter.checkbox("Done").equals(False),
|
|
268
|
+
Filter.or_([
|
|
269
|
+
Filter.select("Priority").equals("High"),
|
|
270
|
+
Filter.date("Due").before("2025-01-01"),
|
|
271
|
+
]),
|
|
272
|
+
])
|
|
273
|
+
|
|
274
|
+
# Raw (escape hatch for unsupported types)
|
|
275
|
+
Filter.raw({"property": "Formula", "formula": {"string": {"equals": "ok"}}})
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
## Sorts reference
|
|
279
|
+
|
|
280
|
+
```python
|
|
281
|
+
Sort.by_property("Name") # ascending by default
|
|
282
|
+
Sort.by_property("Score", "descending")
|
|
283
|
+
Sort.ascending("Name") # alias
|
|
284
|
+
Sort.descending("CreatedAt") # alias
|
|
285
|
+
Sort.by_timestamp("created_time", "descending")
|
|
286
|
+
Sort.by_timestamp("last_edited_time")
|
|
287
|
+
```
|
|
288
|
+
|
|
289
|
+
## RichText reference
|
|
290
|
+
|
|
291
|
+
```python
|
|
292
|
+
RichText.text("plain")
|
|
293
|
+
RichText.text("bold", bold=True)
|
|
294
|
+
RichText.text("italic", italic=True)
|
|
295
|
+
RichText.text("underline", underline=True)
|
|
296
|
+
RichText.text("strike", strikethrough=True)
|
|
297
|
+
RichText.text("code", code=True)
|
|
298
|
+
RichText.text("colored", color="red")
|
|
299
|
+
RichText.text("link", link="https://example.com")
|
|
300
|
+
|
|
301
|
+
RichText.mention_page("page-id")
|
|
302
|
+
RichText.mention_database("db-id")
|
|
303
|
+
RichText.mention_user("user-id")
|
|
304
|
+
RichText.mention_date("2024-01-01", end="2024-01-31")
|
|
305
|
+
RichText.equation("E=mc^2")
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
## Error handling
|
|
309
|
+
|
|
310
|
+
```python
|
|
311
|
+
from notion_database import (
|
|
312
|
+
NotionAPIError,
|
|
313
|
+
NotionNotFoundError,
|
|
314
|
+
NotionRateLimitError,
|
|
315
|
+
NotionUnauthorizedError,
|
|
316
|
+
)
|
|
317
|
+
import time
|
|
318
|
+
|
|
319
|
+
try:
|
|
320
|
+
page = client.pages.retrieve("invalid-id")
|
|
321
|
+
except NotionNotFoundError:
|
|
322
|
+
print("Page not found")
|
|
323
|
+
except NotionRateLimitError:
|
|
324
|
+
time.sleep(1)
|
|
325
|
+
page = client.pages.retrieve("invalid-id")
|
|
326
|
+
except NotionUnauthorizedError:
|
|
327
|
+
print("Check your integration token")
|
|
328
|
+
except NotionAPIError as e:
|
|
329
|
+
print(f"[{e.status_code}] {e.code}: {e.message}")
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
## Color constants
|
|
333
|
+
|
|
334
|
+
```python
|
|
335
|
+
from notion_database.const import (
|
|
336
|
+
DEFAULT, GRAY, BROWN, ORANGE, YELLOW, GREEN, BLUE, PURPLE, PINK, RED,
|
|
337
|
+
GRAY_BACKGROUND, BROWN_BACKGROUND, ORANGE_BACKGROUND, YELLOW_BACKGROUND,
|
|
338
|
+
GREEN_BACKGROUND, BLUE_BACKGROUND, PURPLE_BACKGROUND, PINK_BACKGROUND,
|
|
339
|
+
RED_BACKGROUND,
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
BlockContent.paragraph("Highlighted", color=RED_BACKGROUND)
|
|
343
|
+
```
|
|
344
|
+
|
|
345
|
+
## MCP integration
|
|
346
|
+
|
|
347
|
+
`NotionClient` is designed to map directly to Notion API endpoints, making it
|
|
348
|
+
straightforward to expose as MCP tools. Each sub-client (`databases`,
|
|
349
|
+
`pages`, `blocks`, `search`, `users`, `comments`) corresponds to one section
|
|
350
|
+
of the Notion API docs. All parameters are typed and documented, so an AI can
|
|
351
|
+
introspect the signatures without additional context.
|
|
352
|
+
|
|
353
|
+
## License
|
|
354
|
+
|
|
355
|
+
LGPLv3
|
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
[](https://github.com/minwook-shin/notion-database/actions/workflows/python-publish.yml)
|
|
2
|
+
|
|
3
|
+
> [!IMPORTANT]
|
|
4
|
+
> **Upgrading from v1.4?**
|
|
5
|
+
> Version 2.0 is a full redesign with a new API — `NotionClient` replaces the old `Page`, `Database`, and `Search` classes.
|
|
6
|
+
> v1.4.x will continue to receive security fixes only. See the [migration table](#install) and [CHANGELOG](CHANGELOG.md) for details.
|
|
7
|
+
|
|
8
|
+
# notion-database
|
|
9
|
+
|
|
10
|
+
> Python client for the Notion API — easy to use, 1-to-1 API mapping, AI/MCP friendly
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```bash
|
|
15
|
+
pip install notion-database==2.0.dev1
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Quick start
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from notion_database import (
|
|
22
|
+
NotionClient,
|
|
23
|
+
PropertyValue,
|
|
24
|
+
PropertySchema,
|
|
25
|
+
BlockContent,
|
|
26
|
+
RichText,
|
|
27
|
+
Filter,
|
|
28
|
+
Sort,
|
|
29
|
+
Icon,
|
|
30
|
+
Cover,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
client = NotionClient("secret_xxx")
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Databases
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
# Retrieve
|
|
40
|
+
db = client.databases.retrieve("database-id")
|
|
41
|
+
|
|
42
|
+
# Create
|
|
43
|
+
db = client.databases.create(
|
|
44
|
+
parent={"type": "page_id", "page_id": "page-id"},
|
|
45
|
+
title=[RichText.text("My Database")],
|
|
46
|
+
properties={
|
|
47
|
+
"Name": PropertySchema.title(),
|
|
48
|
+
"Status": PropertySchema.select([
|
|
49
|
+
{"name": "Active", "color": "green"},
|
|
50
|
+
{"name": "Done", "color": "gray"},
|
|
51
|
+
]),
|
|
52
|
+
"Score": PropertySchema.number("number"),
|
|
53
|
+
"Due": PropertySchema.date(),
|
|
54
|
+
},
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
# Query with filter and sort
|
|
58
|
+
results = client.databases.query(
|
|
59
|
+
"database-id",
|
|
60
|
+
filter=Filter.select("Status").equals("Active"),
|
|
61
|
+
sorts=[Sort.descending("Score")],
|
|
62
|
+
)
|
|
63
|
+
pages = results["results"]
|
|
64
|
+
|
|
65
|
+
# Compound filter
|
|
66
|
+
results = client.databases.query(
|
|
67
|
+
"database-id",
|
|
68
|
+
filter=Filter.and_([
|
|
69
|
+
Filter.select("Status").equals("Active"),
|
|
70
|
+
Filter.number("Score").greater_than(80),
|
|
71
|
+
]),
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
# Auto-paginate (returns all pages at once)
|
|
75
|
+
all_pages = client.databases.query_all("database-id")
|
|
76
|
+
|
|
77
|
+
# Update schema
|
|
78
|
+
client.databases.update(
|
|
79
|
+
"database-id",
|
|
80
|
+
title=[RichText.text("Renamed DB")],
|
|
81
|
+
)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Pages
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
# Create
|
|
88
|
+
page = client.pages.create(
|
|
89
|
+
parent={"database_id": "database-id"},
|
|
90
|
+
properties={
|
|
91
|
+
"Name": PropertyValue.title("Hello, Notion 2.0!"),
|
|
92
|
+
"Status": PropertyValue.select("Active"),
|
|
93
|
+
"Score": PropertyValue.number(95),
|
|
94
|
+
"Due": PropertyValue.date("2024-12-31"),
|
|
95
|
+
"Done": PropertyValue.checkbox(False),
|
|
96
|
+
},
|
|
97
|
+
icon=Icon.emoji("🚀"),
|
|
98
|
+
cover=Cover.external("https://example.com/cover.jpg"),
|
|
99
|
+
children=[
|
|
100
|
+
BlockContent.heading_1("Introduction"),
|
|
101
|
+
BlockContent.paragraph("This page was created via notion-database 2.0."),
|
|
102
|
+
],
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
# Retrieve
|
|
106
|
+
page = client.pages.retrieve("page-id")
|
|
107
|
+
|
|
108
|
+
# Update properties
|
|
109
|
+
client.pages.update(
|
|
110
|
+
"page-id",
|
|
111
|
+
properties={
|
|
112
|
+
"Status": PropertyValue.select("Done"),
|
|
113
|
+
"Done": PropertyValue.checkbox(True),
|
|
114
|
+
},
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
# Archive / restore
|
|
118
|
+
client.pages.archive("page-id")
|
|
119
|
+
client.pages.archive("page-id", archived=False)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Blocks
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
# Append content to a page
|
|
126
|
+
client.blocks.append_children(
|
|
127
|
+
"page-id",
|
|
128
|
+
children=[
|
|
129
|
+
BlockContent.heading_2("Section"),
|
|
130
|
+
BlockContent.paragraph([
|
|
131
|
+
RichText.text("Normal text, "),
|
|
132
|
+
RichText.text("bold", bold=True),
|
|
133
|
+
RichText.text(", and "),
|
|
134
|
+
RichText.text("italic", italic=True),
|
|
135
|
+
]),
|
|
136
|
+
BlockContent.bulleted_list_item("First item"),
|
|
137
|
+
BlockContent.bulleted_list_item("Second item"),
|
|
138
|
+
BlockContent.to_do("Finish docs", checked=False),
|
|
139
|
+
BlockContent.code("print('hello')", language="python"),
|
|
140
|
+
BlockContent.divider(),
|
|
141
|
+
BlockContent.image("https://example.com/image.png", caption="Fig 1"),
|
|
142
|
+
BlockContent.column_list([
|
|
143
|
+
[BlockContent.paragraph("Left column")],
|
|
144
|
+
[BlockContent.paragraph("Right column")],
|
|
145
|
+
]),
|
|
146
|
+
],
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
# Retrieve children (single page)
|
|
150
|
+
response = client.blocks.retrieve_children("page-id")
|
|
151
|
+
blocks = response["results"]
|
|
152
|
+
|
|
153
|
+
# Auto-paginate all children
|
|
154
|
+
all_blocks = client.blocks.retrieve_all_children("page-id")
|
|
155
|
+
|
|
156
|
+
# Delete a block
|
|
157
|
+
client.blocks.delete("block-id")
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
## Search
|
|
161
|
+
|
|
162
|
+
```python
|
|
163
|
+
# Search all
|
|
164
|
+
results = client.search.search("Project")
|
|
165
|
+
|
|
166
|
+
# Filter by type
|
|
167
|
+
db_results = client.search.search_databases("Project")
|
|
168
|
+
page_results = client.search.search_pages("Meeting notes")
|
|
169
|
+
|
|
170
|
+
# Auto-paginate all results
|
|
171
|
+
all_results = client.search.search_all("Q1")
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
## Users
|
|
175
|
+
|
|
176
|
+
```python
|
|
177
|
+
# Current bot
|
|
178
|
+
me = client.users.me()
|
|
179
|
+
|
|
180
|
+
# List all workspace users
|
|
181
|
+
all_users = client.users.list_all()
|
|
182
|
+
|
|
183
|
+
# Retrieve by ID
|
|
184
|
+
user = client.users.retrieve("user-id")
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Comments
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
# Retrieve comments on a page
|
|
191
|
+
comments = client.comments.retrieve("page-id")
|
|
192
|
+
|
|
193
|
+
# Post a comment
|
|
194
|
+
client.comments.create(
|
|
195
|
+
parent={"page_id": "page-id"},
|
|
196
|
+
rich_text=[RichText.text("Great work!")],
|
|
197
|
+
)
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
## Filters reference
|
|
201
|
+
|
|
202
|
+
```python
|
|
203
|
+
# Text / title
|
|
204
|
+
Filter.text("Name").equals("Alice")
|
|
205
|
+
Filter.text("Name").contains("Al")
|
|
206
|
+
Filter.text("Name").starts_with("A")
|
|
207
|
+
Filter.text("Name").is_empty()
|
|
208
|
+
|
|
209
|
+
# Number
|
|
210
|
+
Filter.number("Score").greater_than(80)
|
|
211
|
+
Filter.number("Score").less_than_or_equal_to(100)
|
|
212
|
+
|
|
213
|
+
# Checkbox
|
|
214
|
+
Filter.checkbox("Done").equals(True)
|
|
215
|
+
|
|
216
|
+
# Select / status
|
|
217
|
+
Filter.select("Status").equals("Active")
|
|
218
|
+
Filter.status("Status").does_not_equal("Archived")
|
|
219
|
+
|
|
220
|
+
# Multi-select
|
|
221
|
+
Filter.multi_select("Tags").contains("python")
|
|
222
|
+
|
|
223
|
+
# Date
|
|
224
|
+
Filter.date("Due").before("2025-01-01")
|
|
225
|
+
Filter.date("Due").past_week()
|
|
226
|
+
Filter.date("Due").next_month()
|
|
227
|
+
|
|
228
|
+
# Timestamp
|
|
229
|
+
Filter.created_time().after("2024-01-01")
|
|
230
|
+
Filter.last_edited_time().past_week()
|
|
231
|
+
|
|
232
|
+
# Compound
|
|
233
|
+
Filter.and_([
|
|
234
|
+
Filter.select("Status").equals("Active"),
|
|
235
|
+
Filter.number("Score").greater_than(80),
|
|
236
|
+
])
|
|
237
|
+
Filter.or_([
|
|
238
|
+
Filter.text("Name").contains("Alice"),
|
|
239
|
+
Filter.text("Name").contains("Bob"),
|
|
240
|
+
])
|
|
241
|
+
|
|
242
|
+
# Nested compound
|
|
243
|
+
Filter.and_([
|
|
244
|
+
Filter.checkbox("Done").equals(False),
|
|
245
|
+
Filter.or_([
|
|
246
|
+
Filter.select("Priority").equals("High"),
|
|
247
|
+
Filter.date("Due").before("2025-01-01"),
|
|
248
|
+
]),
|
|
249
|
+
])
|
|
250
|
+
|
|
251
|
+
# Raw (escape hatch for unsupported types)
|
|
252
|
+
Filter.raw({"property": "Formula", "formula": {"string": {"equals": "ok"}}})
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
## Sorts reference
|
|
256
|
+
|
|
257
|
+
```python
|
|
258
|
+
Sort.by_property("Name") # ascending by default
|
|
259
|
+
Sort.by_property("Score", "descending")
|
|
260
|
+
Sort.ascending("Name") # alias
|
|
261
|
+
Sort.descending("CreatedAt") # alias
|
|
262
|
+
Sort.by_timestamp("created_time", "descending")
|
|
263
|
+
Sort.by_timestamp("last_edited_time")
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## RichText reference
|
|
267
|
+
|
|
268
|
+
```python
|
|
269
|
+
RichText.text("plain")
|
|
270
|
+
RichText.text("bold", bold=True)
|
|
271
|
+
RichText.text("italic", italic=True)
|
|
272
|
+
RichText.text("underline", underline=True)
|
|
273
|
+
RichText.text("strike", strikethrough=True)
|
|
274
|
+
RichText.text("code", code=True)
|
|
275
|
+
RichText.text("colored", color="red")
|
|
276
|
+
RichText.text("link", link="https://example.com")
|
|
277
|
+
|
|
278
|
+
RichText.mention_page("page-id")
|
|
279
|
+
RichText.mention_database("db-id")
|
|
280
|
+
RichText.mention_user("user-id")
|
|
281
|
+
RichText.mention_date("2024-01-01", end="2024-01-31")
|
|
282
|
+
RichText.equation("E=mc^2")
|
|
283
|
+
```
|
|
284
|
+
|
|
285
|
+
## Error handling
|
|
286
|
+
|
|
287
|
+
```python
|
|
288
|
+
from notion_database import (
|
|
289
|
+
NotionAPIError,
|
|
290
|
+
NotionNotFoundError,
|
|
291
|
+
NotionRateLimitError,
|
|
292
|
+
NotionUnauthorizedError,
|
|
293
|
+
)
|
|
294
|
+
import time
|
|
295
|
+
|
|
296
|
+
try:
|
|
297
|
+
page = client.pages.retrieve("invalid-id")
|
|
298
|
+
except NotionNotFoundError:
|
|
299
|
+
print("Page not found")
|
|
300
|
+
except NotionRateLimitError:
|
|
301
|
+
time.sleep(1)
|
|
302
|
+
page = client.pages.retrieve("invalid-id")
|
|
303
|
+
except NotionUnauthorizedError:
|
|
304
|
+
print("Check your integration token")
|
|
305
|
+
except NotionAPIError as e:
|
|
306
|
+
print(f"[{e.status_code}] {e.code}: {e.message}")
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
## Color constants
|
|
310
|
+
|
|
311
|
+
```python
|
|
312
|
+
from notion_database.const import (
|
|
313
|
+
DEFAULT, GRAY, BROWN, ORANGE, YELLOW, GREEN, BLUE, PURPLE, PINK, RED,
|
|
314
|
+
GRAY_BACKGROUND, BROWN_BACKGROUND, ORANGE_BACKGROUND, YELLOW_BACKGROUND,
|
|
315
|
+
GREEN_BACKGROUND, BLUE_BACKGROUND, PURPLE_BACKGROUND, PINK_BACKGROUND,
|
|
316
|
+
RED_BACKGROUND,
|
|
317
|
+
)
|
|
318
|
+
|
|
319
|
+
BlockContent.paragraph("Highlighted", color=RED_BACKGROUND)
|
|
320
|
+
```
|
|
321
|
+
|
|
322
|
+
## MCP integration
|
|
323
|
+
|
|
324
|
+
`NotionClient` is designed to map directly to Notion API endpoints, making it
|
|
325
|
+
straightforward to expose as MCP tools. Each sub-client (`databases`,
|
|
326
|
+
`pages`, `blocks`, `search`, `users`, `comments`) corresponds to one section
|
|
327
|
+
of the Notion API docs. All parameters are typed and documented, so an AI can
|
|
328
|
+
introspect the signatures without additional context.
|
|
329
|
+
|
|
330
|
+
## License
|
|
331
|
+
|
|
332
|
+
LGPLv3
|