A moonbit client for the Notion API. This reference covers the SDK's methods, options, and helpers.
///|
fn main {
let fetch : @notionsdk.Fetch = (url, init) => {
// Connect a real HTTP stack here (for example one built on
// moonbitlang/async) and return an HttpResponse.
@notionsdk.HttpResponse::from_text(
status=200,
headers=[],
body="{\"object\":\"user\",\"id\":\"u1\"}",
)
}
let client = @notionsdk.Client::new(
options=@notionsdk.ClientOptions::new(
auth=Some("secret_your_integration_token"),
fetch=Some(fetch),
),
)
let me = (client.users().me)({})
println(me.stringify())
}///|
let page = (client.pages().create)({
"parent": { "page_id": parent },
"properties": {},
})
///|
let block = (client.blocks().retrieve)({ "block_id": block_id })
///|
let results = (client.search().search)({ "query": "roadmap" })
///|
let events = (client.sessions().stream)({ "message": "hi" })| TypeScript source | MoonBit file |
|---|---|
| src/Client.ts | Client.mbt |
| src/api-endpoint-methods.ts | api_endpoint_methods.mbt |
| src/api-endpoints.ts + src/api-endpoints/*.ts | api-endpoints/*.mbt |
| src/errors.ts | errors.mbt |
| src/helpers.ts | helpers.mbt |
| src/utils.ts | utils.mbt |
| src/logging.ts | logging.mbt |
| src/constants.ts | constants.mbt |
| src/fetch-types.ts | fetch_types.mbt |
| src/webhooks.ts | webhooks.mbt (+ sha256.mbt) |
///|
let response = (client.pages().retrieve)({ "page_id": id }) catch {
@notionsdk.NotionClientError(error) => {
if error.code == "object_not_found" {
// handle a missing page
}
@notionsdk.Json::null()
}
_ => @notionsdk.Json::null()
}///|
let blocks = @notionsdk.collect_paginated_api(
args => (client.blocks().children_list)(args),
{ "block_id": parent },
)///|
let ok = @notionsdk.verify_webhook_signature({
body: raw_body,
signature: Some(header_value),
verification_token: token,
})moon testpub(all) enum APIErrorCode {
Unauthorized
RestrictedResource
ObjectNotFound
RateLimited
InvalidJson
InvalidRequestUrl
InvalidRequest
InvalidBeta
ValidationError
ConflictError
InternalServerError
ServiceOverload
ServiceUnavailable
GatewayTimeout
}pub struct Client {
// private fields
}pub(all) enum ClientErrorCode {
RequestTimeout
ResponseError
InvalidPathParameter
}pub(all) struct ClientOptions {
auth : String?
timeout_ms : Int
base_url : String
log_level : LogLevel
logger : (LogLevel, String, Json) -> Unit?
notion_version : String
fetch : (String, HttpRequestInit) -> HttpResponse raise?
retry : Bool?
max_retries : Int
initial_retry_delay_ms : Int
max_retry_delay_ms : Int
dangerously_allow_browser : Bool
browser_environment : Bool
sleep : (Int) -> Unit?
random : () -> Double?
}fn ClientOptions::new(auth? : String?, timeout_ms? : Int, base_url? : String, log_level? : LogLevel, logger? : (LogLevel, String, Json) -> Unit?, notion_version? : String, fetch? : (String, HttpRequestInit) -> HttpResponse raise?, retry? : Bool?, max_retries? : Int, initial_retry_delay_ms? : Int, max_retry_delay_ms? : Int, dangerously_allow_browser? : Bool, browser_environment? : Bool, sleep? : (Int) -> Unit?, random? : () -> Double?) -> ClientOptionspub(all) struct HttpRequestInit {
http_method : String
headers : Array[(String, String)]
body : String?
timeout_ms : Int
agent : String?
}pub(all) struct HttpResponse {
ok : Bool
status : Int
headers : Array[(String, String)]
text : () -> String
}fn HttpResponse::from_text(status~ : Int, headers~ : Array[(String, String)], body~ : String) -> HttpResponsepub(all) enum LogLevel {
Debug
Info
Warn
Error
}pub(all) struct NotionError {
kind : NotionErrorKind
code : String
message : String
status : Int
headers : Array[(String, String)]
body : String
additional_data : Json?
request_id : String?
ray_id : String?
}fn NotionError::new(kind~ : NotionErrorKind, code~ : String, message~ : String, status? : Int, headers? : Array[(String, String)], body? : String, additional_data? : Json?, request_id? : String?, ray_id? : String?) -> NotionErrorpub(all) enum NotionErrorKind {
RequestTimeoutError
UnknownHTTPResponseError
ApiResponseError
InvalidPathParameterError
BrowserTokenNotAllowedError
}pub(all) struct ParsedAPIError {
code : APIErrorCode
message : String
additional_data : Json?
request_id : String?
}pub(all) enum RequestAuth {
Bearer(String)
Basic(client_id~ : String, client_secret~ : String)
}pub(all) struct RequestParameters {
path : String
http_method : String
query : Json?
body : Json?
form_data_params : Json?
headers : Array[(String, String)]
auth : RequestAuth?
}fn RequestParameters::new(path~ : String, http_method~ : String, query? : Json?, body? : Json?, form_data_params? : Json?, headers? : Array[(String, String)], auth? : RequestAuth?) -> RequestParameterspub(all) struct SseFrame {
event_name : String
data : String
}pub(all) struct VerifyWebhookSignatureArgs {
body : String
signature : String?
verification_token : String
}fn api_response_error(code~ : APIErrorCode, message~ : String, status~ : Int, headers~ : Array[(String, String)], body~ : String, additional_data? : Json?, request_id? : String?) -> NotionErrorfn build_unknown_response_message(status~ : Int, content_type~ : String?, ray_id~ : String?, request_id~ : String?) -> Stringfn bytes_to_hex(bytes : BytesView) -> Stringlet default_base_url : Stringlet default_initial_retry_delay_ms : Intlet default_max_retries : Intlet default_max_retry_delay_ms : Intlet default_notion_version : Stringlet default_timeout_ms : Intfn encode_uri_component(value : String) -> Stringfn extract_block_id(url_with_block : String) -> String?fn extract_database_id(database_url : String) -> String?fn extract_notion_id(url_or_id : String) -> String?fn extract_page_id(page_url : String) -> String?fn hmac_sha256(key : BytesView, message : BytesView) -> Bytesfn is_api_error_code(value : String) -> Boolfn is_api_response_error(error : Error) -> Boolfn is_http_response_error(error : Error) -> Boolfn is_notion_client_error(error : Error) -> Boollet min_view_column_width : Intlet sdk_version : Stringfn sha256_bytes(data : BytesView) -> Bytesfn sign_webhook_payload(body : String, verification_token : String) -> Stringfn unknown_http_response_error(status~ : Int, headers~ : Array[(String, String)], body~ : String, message? : String?) -> NotionErrorfn validate_request_path(path : String) -> Unit raiseInstall
Download zipA moonbit client for the Notion API. This reference covers the SDK's methods, options, and helpers.