Notion SDK / Kotlin Multiplatform
November 18, 2021 ยท View on GitHub

Use your Notion table as a data source.
Export Notion pages as Markdown recursively.
Not affiliated with Notion but uses the official API.
The documentation is available here: https://developers.notion.com
Installation
implementation("com.petersamokhin.notionsdk:notionsdk:$latestVersion")
Library is published to the Maven Central repository.
Supported endpoints
Example usage - retrieve a database
How to get a token and the database ID: https://developers.notion.com/docs/getting-started
val notion = Notion.fromToken(
token = "token",
httpClient = HttpClient(CIO)
)
val schema = notion.retrieveDatabase("databaseId")
val database = notion.queryDatabase("databaseId")
val uncheckedRowSelectedOptionsIds = database.results
.first { row ->
val checkboxColumnSelected = (row.columns
.getValue("CheckboxColumn")
.value as NotionDatabaseProperty.Checkbox)
.selected
!checkboxColumnSelected
}
.columns.getValue("MultiSelectColumn")
.let { column -> column.value as NotionDatabaseProperty.MultiSelect }
.selected.map { option -> option.id }
val availableOptions = databaseInfo.schema.getValue("MultiSelectColumn")
.let { property -> property as NotionDatabasePropertySchema.MultiSelect }
.options
val uncheckedRowIgnoredOptionsNames = availableOptions
.filter { option -> option.id !in uncheckedRowSelectedOptionsIds }
.map { option -> option.name }
println(uncheckedRowIgnoredOptionsNames)
// Output:
// [OptionThree]
println(database)
// Output:
// NotionDatabase(
// rows=[
// NotionDatabaseRow(
// columns={
// MultiSelectColumn=NotionDatabaseColumn(key=MultiSelectColumn, value=MultiSelect(id=MWKa, selected=[])),
// CheckboxColumn=NotionDatabaseColumn(key=CheckboxColumn, value=Checkbox(id=%5CUbj, selected=true)),
// LastEditedByColumn=NotionDatabaseColumn(key=LastEditedByColumn, value=LastEditedBy(id=d%3EIW, lastEditedBy=User(id=UUID, name=Peter Samokhin, avatarUrl=https://site.com/whatever.png, email=contact+notionsdk@petersamokhin.com))),
// TitleColumn=NotionDatabaseColumn(key=TitleColumn, value=Title(id=title, text=second row title))
// }
// ),
//
// NotionDatabaseRow(
// columns={
// MultiSelectColumn=NotionDatabaseColumn(key=MultiSelectColumn, value=MultiSelect(id=MWKa, selected=[Option(id=UUID, name=OptionOne), Option(id=UUID, name=OptionTwo)])),
// CheckboxColumn=NotionDatabaseColumn(key=CheckboxColumn, value=Checkbox(id=%5CUbj, selected=false)),
// LastEditedByColumn=NotionDatabaseColumn(key=LastEditedByColumn, value=LastEditedBy(id=d%3EIW, lastEditedBy=User(id=UUID, name=Peter Samokhin, avatarUrl=https://site.com/whatever.png, email=contact+notionsdk@petersamokhin.com))),
// TitleColumn=NotionDatabaseColumn(key=TitleColumn, value=Title(id=title, text=first row title))
// }
// )
// ],
// nextCursor=null,
// hasMore=false
// )
Example usage - export a Notion page as markdown
How to get a token and the page (block) ID: https://developers.notion.com/docs/getting-started
val notion = Notion.fromToken(
token = "token",
httpClient = HttpClient(CIO)
)
val blocks: List<NotionBlock> = notion.retrieveBlockChildren("page-id").results
val exporter = NotionMarkdownExporter.create()
// the simplest way
val markdown: String = exporter.export(blocks = blocks)
// or a bit more complicated way
val markdown: String = exporter.exportRecursively(
blocks = blocks,
settings = Settings(), // please read the KDocs
notion = notion,
depthLevel = 3,
)
What is supported:
- All block types which have the content returned from the API (i.e. except the table of contents, etc.).
- Formatting
- Indentation for the children pages embedded content
What is NOT supported:
- HTML blocks like aside (callout), summary+details (spoiler aka toggle), font colors
- Many of other advanced blocks, they are simplified
Purpose of the SDK
This library is intended to help with retrieving the info from the Notion databases and pages.
Mostly, this is a handy tool which covers only the specific needs if you use Notion as a convenient data source.
Also, it can help to migrate from Notion to any other tool by exporting the pages as Markdown.