Currently, this library supports only the “internal integration” authorization mode. For more information about authorization and
instruction how to obtain NOTION_TOKEN please check Notion Offical Documentation.
Important: Integrations are granted access to resources (pages and databases) which users have shared with the integration. Resources that are not shared with the integration are not visible by API endpoints.
Creating a Notion client
let notion = NotionClient(accessKeyProvider: StringAccessKeyProvider(accessKey: "{NOTION_TOKEN}"))
Tweak network configuration
To tweak things like network timeouts you can provide a custom URLSessionConfiguration to NotionClient like below.
let sessionConfig = URLSessionConfiguration.default
sessionConfig.timeoutIntervalForRequest = 15
let notion = NotionClient(accessKeyProvider: StringAccessKeyProvider(accessKey: "{NOTION_TOKEN}"), sessionConfiguration: sessionConfig)
If that’s not enough for your needs, you can implement the NetworkClient protocol and provide your implementation to NotionClient.
List all databases
The https://api.notion.com/v1/databases is deprecated. To recommended way to list all databases is to use https://api.notion.com/v1/search endpoint.
In theory, search allows filtering results by object type. However, currently, the only filter allowed is object which will filter by type of object (either page or database)
To narrow search results, use code snippet belove.
// fetch avaiable databases
notion.search(request: .init(filter: .database)) { result in
let databases = result.map { objects in
objects.results.compactMap({ object -> Database? in
if case .database(let db) = object {
return db
}
return nil
})
}
print(databases)
}
Query a database
In this example we will get all pages in the database. To narrow results use params argument.
let databaseId = Database.Identifier("{DATABASE UUIDv4}")
notion.databaseQuery(databaseId: databaseId) {
print($0)
}
Retrieve a database
let databaseId = Database.Identifier("{DATABASE UUIDv4}")
notion.database(databaseId: databaseId) {
print($0)
}
let pageId = Page.Identifier("{PAGE UUIDv4}")
notion.page(pageId: pageId) {
print($0)
}
Page content (text for example) is represented as an array of blocks. The example below loads properties and page content.
let pageId = Page.Identifier("{PAGE UUIDv4}")
notion.page(pageId: pageId) { [notion] in
print("---- Properties ----- ")
print($0)
switch $0 {
case .success(let page):
notion.blockChildren(blockId: page.id.toBlockIdentifier) {
print("---- Children ----- ")
print($0)
}
default:
break
}
}
Note: The API returns only the direct children of the page. If there is content nested in the block (nested lists for example) it requires other calls.
Create a page
let parentPageId = Page.Identifier("{PAGE UUIDv4}")
let request = PageCreateRequest(
parent: .page(parentPageId),
properties: [
"title": .init(
type: .title([
.init(string: "Lorem ipsum \(Date())")
])
)
]
)
notion.pageCreate(request: request) {
print($0)
}
Note: This endpoint returns only the first level of children, so for example, nested list items won’t be returned. In that case, you need to make another request with the block id of the parent block.
let pageId = Block.Identifier("{PAGE UUIDv4}")
notion.blockChildren(blockId: pageId) {
print($0)
}
NotionSwift provide an internal rudimental logging system to track HTTP traffic.
To enable it you need to set a build-in or custom logger handler and decide about log level (.info by default).
With .track log level you can see all content of a request. This is useful to track mapping issues between library data models and API.
Example logging configuration:
// This code should be in the ApplicationDelegate
NotionSwiftEnvironment.logHandler = NotionSwift.PrintLogHandler() // uses print command
NotionSwiftEnvironment.logLevel = .trace // show me everything
License
NotionSwift is available under the MIT license. See the LICENSE file for more info.
NotionSwift
Unofficial Notion SDK for iOS & macOS.
This is still work in progress version, the module interface might change.
API Documentation
This library is a client for the official Notion API. For more details and documentation please check Notion Developer Portal
Installation
CocoaPods
Swift Package Manager
Usage
Currently, this library supports only the “internal integration” authorization mode. For more information about authorization and instruction how to obtain
NOTION_TOKEN
please check Notion Offical Documentation.Important: Integrations are granted access to resources (pages and databases) which users have shared with the integration. Resources that are not shared with the integration are not visible by API endpoints.
Creating a Notion client
Tweak network configuration
To tweak things like network timeouts you can provide a custom
URLSessionConfiguration
toNotionClient
like below.If that’s not enough for your needs, you can implement the
NetworkClient
protocol and provide your implementation toNotionClient
.List all databases
The
https://api.notion.com/v1/databases
is deprecated. To recommended way to list all databases is to usehttps://api.notion.com/v1/search
endpoint. In theory, search allows filtering results by object type. However, currently, the only filter allowed isobject
which will filter by type of object (eitherpage
ordatabase
) To narrow search results, use code snippet belove.Query a database
In this example we will get all pages in the database. To narrow results use
params
argument.Retrieve a database
Create a database
Update a database
Create a database entry
Notion database entries are pages, whose properties conform to the parent database’s schema.
Retrieve a page
Retrieve page properties.
Page content (text for example) is represented as an array of blocks. The example below loads properties and page content.
Note: The API returns only the direct children of the page. If there is content nested in the block (nested lists for example) it requires other calls.
Create a page
Update page properties
Retrieve block children
Note: This endpoint returns only the first level of children, so for example, nested list items won’t be returned. In that case, you need to make another request with the block id of the parent block.
Append block children
Update a block
Block delete
Retrieve a user
List all users
Search
Search for pages & databases with a title containing text “Lorem”
Search for all databases and ignore pages.
Get all pages & databases
Logging and debugging
NotionSwift
provide an internal rudimental logging system to track HTTP traffic. To enable it you need to set a build-in or custom logger handler and decide about log level (.info
by default). With.track
log level you can see all content of a request. This is useful to track mapping issues between library data models and API.Example logging configuration:
License
NotionSwift is available under the MIT license. See the LICENSE file for more info.