官方 Notion JavaScript 客户端
一个用于 Notion API 的 JavaScript 和 TypeScript 客户端。本参考涵盖了 SDK 的方法、选项和辅助工具。
npm install @notionhq/client[!NOTE] 有关设置步骤,请参阅 Notion 的入门指南。
Client 接受集成令牌(integration token)或 OAuth 访问令牌。
const { Client } = require("@notionhq/client")
const notion = new Client({
auth: process.env.NOTION_TOKEN,
})向任意 Notion API 端点发起请求。
;(async () => {
const listUsersResponse = await notion.users.list({})
console.log(listUsersResponse)
})()[!NOTE] 完整的端点列表请参阅 API 参考。
请求方法会返回一个包含响应的 Promise。例如:
{
results: [
{
object: "user",
id: "d40e767c-d7af-4b18-a86d-55c61f1e39a4",
type: "person",
person: {
email: "[email protected]",
},
name: "Avocado Lovelace",
avatar_url:
"https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg",
},
// ...
]
}端点参数被归组到单个对象中。你无需记住哪些参数属于路径、查询还是请求体。
const myPage = await notion.dataSources.query({
data_source_id: "897e5a76-ae52-4b48-9fdf-e71f5945d1af",
filter: {
property: "Landmark",
rich_text: {
contains: "Bridge",
},
},
})Notion API 错误会以 APIResponseError 拒绝请求。code 属性标识了错误类型。APIErrorCode 包含已知的服务器错误代码。
…默认的日志记录器会将警告和错误输出到控制台。LogLevel.DEBUG 还会记录响应体。
const { Client, LogLevel } = require("@notionhq/client")
const notion = new Client({
auth: process.env.NOTION_TOKEN,
logLevel: LogLevel.DEBUG,
})自定义的 logger 会接收 logLevel、message 和 extraInfo。它不应返回任何值。
Client 构
暂无开放 Issues,或尚未同步最近议题。