Client library for Notion API
Project description
Notions - A Python Client Library and CLI for Notion
A command line client and API library for Notion.
Uses the Notion API to communicate with your Notion pages and databases.
- Homepage: https://github.com/micktwomey/notions
- Repo: https://github.com/micktwomey/notions
- PyPI project page: https://pypi.org/project/notions/
- Notions - A Python Client Library and CLI for Notion
- Installation
- Authentication
- Command Line Usage
- API
- Developing Notions
- What's the deal with the name?
Installation
Homebrew for CLI
You can use the notions tap to install:
brew tap micktwomey/notions
brew install notions
# Or in one line: `brew install micktwomey/notions/notions`
pipx for CLI usage anywhere
To install as a CLI and make it available on your PATH you can use pipx to install:
pipx install notions
pip
To install using pip:
pip install notions
poetry
To add as a depdency using poetry:
poetry add notions
pipenv
To add as a dependency using pipenv:
pipenv install notions
Authentication
Currently only authentication using an Notion API token is supported (see Notion's Getting Started Guide for full details).
Rough steps to get a token you can use:
- Go to https://www.notion.so/my-integrations
- Create a new internal integration
- Note down the API key, you'll need that later
- Go to a Database or page in your notion (a top level item in your sidebar) and click on the sharing button
- Add your integration to the shares. You need to do this for every database you want to share, Notion doesn't expose everything by default.
Command Line Usage
- Authenticate first, see Authentication, and get your API token (we'll refer to it as
YOUR_KEY_GOES_HERE
below) - You can either pass in the key each time you call the client using
notions --notion-api-key YOUR_KEY_GOES_HERE
or set theNOTION_API_KEY
environment variable.- To set in fish:
set -Ux NOTION_API_KEY YOUR_KEY_GOES_HERE
- To set in bash/zsh:
export NOTION_API_KEY=YOUR_KEY_GOES_HERE
- To set in fish:
- Call
notions --help
to explore your options
Some options are global and go before the command, e.g. notions --output-format notion_yaml database query --query foo
.
Output Formats
Formats you can specify with notions --output-format
notion_json
- Parses API output and dumps back as JSON, with multiple items contained in a list.notion_jsonl
- Parses API output and dumps back as JSON, one JSON object per line.notion_yaml
- Parses API output and dumps back as YAML, with multiple items in a list.text
(default) - Parse API output and return a simple text representation. Handy for looking up page and database ids.json
- Parses API output and dumps to a flatter JSON, more suitable for templating and processsing. Multiple items are placed in a list.jsonl
- Parses API output and dumps to a flatter JSON, one per line, more suitable for templating and processsing.yaml
- Parses API output and dumps to a flatter YAML, more suitable for templating and processsing. Multiple items are placed in a list.tsv
- Parses API output and writes out tab separated values.csv
- Parses API output and writes out comma separated values.
Commands
notions api
notions api
allows you to issue requests using the authentication features of the client.
- To make a request and get back the response JSON:
notions api METHOD PATH
. - To make a request and leverage the pagination support (this will parse the response and give you the option to render as YAML or other formats):
notions api --paginate METHOD PATH
. Note that due to how the flags are handled you need the--paginate
to trigger parsing and allow for different output formats.
# Get all databases, without pagination and return raw JSON from Notion's API
notions api GET /v1/databases/
# Get all databases with pagination and format output as YAML
# This is the same as `notions --output-format yaml database list`
notions --output-format yaml api GET /v1/databases/ --paginate
notions search
notions search
allows you to search across all databases and pages you have shared with the Notion integration you created. You can restrict the search with --query SOME_QUERY_STRING
and items which have a matching title will be returned.
# show all pages and dbs with "example" in their title
notions search --query example
notions database list
notions database list
lists all the databases the integration has access to.
notions database query
Run a query against all pages in a given database. You'll need the database UUID from something like notions database list
.
# Show all pages in the given db and sort by the HP property in descending order
notions database query cc5ef123-05f5-409e-9b34-38043df965b0 --sort-property HP:descending
notions page get
Get a single page.
# Get a page in YAML
notions --output-format yaml page get ccad10e7-c776-423e-9662-6ad5fb1256d6
API
The main way to use the library is via the HTTP client (see examples/basic.py for same code):
"""Example showing basic use of the client for API calls
"""
import asyncio
import os
import typing
import httpx
from notions.client import NotionAsyncClient
from notions.models.database import Database
async def http_list_databases(notion_client: NotionAsyncClient) -> httpx.Response:
"""Do a HTTP call with authentication to list databases
This uses notions as a convenience for HTTP calls using httpx
"""
async with notion_client.async_client() as client:
url = notion_client.get_url_for_path("/v1/databases")
return await client.request("GET", url.url)
async def list_databases(
notion_client: NotionAsyncClient,
) -> typing.AsyncIterable[Database]:
"""Do a list db call (automatically reads all pages)
This is the same underlying call as `http_list_databases` above
but will also parse the response and handle pagination.
Will yield notions.models.database.Database
"""
async for db in notion_client.list_databases():
yield db
async def main():
api_token = os.environ.get("NOTION_API_KEY", "no-token-set")
notion_client = NotionAsyncClient(api_token)
response = await http_list_databases(notion_client)
print(response)
print(response.json())
response.raise_for_status()
async for db in list_databases(notion_client):
print(db.id)
print(db.title)
print(db)
if __name__ == "__main__":
asyncio.run(main())
Developing Notions
To develop locally you'll need python 3.6+, poetry and make:
make dep
to set up the development environment.make test
to test all supported pythons ormake pytest
to run tests more directly for your python.
You'll need a plan which allows for creating Notion integrations (API access) to develop against Notion, but I'm going to assume you have that if you're interested in this library :D
To release ensure you're happy with the current state of main and run make release
.
What's the deal with the name?
"Notions" is sometimes used in Ireland to refer to someone who has ideas abover their station ("so and so has notions"). It's typically Irish and a little bit mean, but often used in an ironic way. I picked it 'cos I thought it'd be funny (and maybe I have notions about writing a client library for a product produced by a company called Notion).
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file notions-0.3.0.tar.gz
.
File metadata
- Download URL: notions-0.3.0.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9801f29ce78d1f91eb2e3d9a79bc03591c807ae467b3562798ca465f61cadb72 |
|
MD5 | 6e02a3c8cc22fab2535429303a8b4ce3 |
|
BLAKE2b-256 | 4d72f3a4185ab6ba7c4a3754ad9b5cc0312ef0d56d3c6474bb3c92564df02dd8 |
File details
Details for the file notions-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: notions-0.3.0-py3-none-any.whl
- Upload date:
- Size: 21.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.7 CPython/3.9.1 Darwin/20.6.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8ab67eeb0d27206dd44671cd3169b91df4096cdf34e6887d05f06b61a5def2e4 |
|
MD5 | c8349fa1e8c0f40ff5ab1a65478fea55 |
|
BLAKE2b-256 | 9016d3ed5d3abc296807eff0eafcbe898dcc85a7595c0f84fdbb63c5854ab197 |