Categorygithub.com/sorcererxw/go-notion
modulepackage
0.2.4
Repository: https://github.com/sorcererxw/go-notion.git
Documentation: pkg.go.dev

# README

go-notion

tests Go Reference Go Report Card codecov

Go SDK for Notion Official API.

go get github.com/sorcererxw/go-notion

Overview

go-notion is the Golang binding for Notion official API. This package provides:

  • Easy-to-use and well-testing API wrappers.

  • Complete type definition.

You can easily and quickly build notion integrations with this package.

⚠️ Notion official API is still in public beta, it's hard to guarantee forward compatibility in the future. This package will be continuously updated according to the official documentation.

Getting Started

At the beginning, you should follow the official document to create your workspace and integrations.

package main

import (
	"context"

	"github.com/sorcererxw/go-notion"
)

func main() {
	client := notion.NewClient(notion.Settings{Token: "token"})

	database, err := client.RetrieveDatabase(context.Background(), "database_id")
}

Pagination

package main

func main() {
	var cursor string
	for {
		data, nextCursor, hasMore, err := client.ListAllUsers(context.Background(), 30, cursor)
		if err != nil {
			break
		}
		if !hasMore {
			break
		}
		cursor = nextCursor
	}
}

Error Handling

go-notion declares error codes . You can compare error code to confirm which error occurred.

package main

import (
	"context"
	"fmt"

	"github.com/sorcererxw/go-notion"
)

func main() {
	user, err := client.RetrieveUser(context.Background(), "user_id")
	if err, ok := notion.AsError(err); ok {
		switch err.Code {
		case notion.ErrCodeRateLimited:
			fmt.Println("rate limited")
		}
	}
}

Reverse Proxy

If you cannot access Notion server in your region(e.g. China) directly, you can use reverse proxy to solve the problem:

package main

import "github.com/sorcererxw/go-notion"

const proxyEndpoint = "https://1.1.1.1/notion"

func main() {
  client := notion.NewClient(notion.Settings{
      Token: "token",
      Endpoint: proxyEndpoint,
  })
}

OAuth

package main

import "net/http"

func main() {
  client := notion.NewOAuthClient("client_id", "client_secret", "redirect_uri")

  mux := http.NewServeMux()
  mux.HandleFunc("/oauth", func(w http.ResponseWriter, r *http.Request) {
    code := r.URL.Query().Get("code")
    token, _ := client.ExchangeAccessToken(r.Context(), code)
    
    // store token to db ...
    
    w.WriteHeader(http.StatusOK)
  })
}

License

go-notion is distributed under MIT.

# Functions

AsError tries casting the basic error to notion error.
NewCheckboxPropertyValue creates a CheckboxPropertyValue.
NewClient creates a new API client.
NewDatabaseParent creates a database parent.
NewDatePropertyValue creates a DatePropertyValue.
NewEmailPropertyValue creates a EmailPropertyValue.
NewFilesPropertyValue creates a FilesPropertyValue.
NewMultiSelectPropertyValue creates a MultiSelectPropertyValue.
NewNumberPropertyValue creates a NumberPropertyValue.
NewOAuthClient creates a OAuthClient.
NewPageParent creates a page parent.
NewPeoplePropertyValue creates a PeoplePropertyValue.
NewPhoneNumberPropertyValue creates a PhonePropertyValue.
NewRelationPropertyValue creates a RelationPropertyValue.
NewRichTextPropertyValue creates a RichTextPropertyValue.
NewSelectPropertyValue creates a SelectPropertyValue.
NewTitlePropertyValue creates a TitlePropertyValue.
NewURLPropertyValue creates a URLPropertyValue.
NewWorkspaceParent creates a workspace parent.
SortByCreatedTime creates Sort to sort database by "created_time".
SortByLastEditedTime creates Sort to sort database by "last_edited_time".
SortByProperty creates Sort to sort database by specified property.

# Constants

BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
BlockType enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
Color enums.
SortDirection enums.
SortDirection enums.
ErrCodeConflictError The transaction could not be completed, potentially due to a data collision.
ErrCodeInternalServerError An unexpected error occurred.Reach out to Notion support.
ErrCodeInvalidJSON The request body could not be decoded as JSON.
ErrCodeInvalidRequest This request is not supported.
ErrCodeInvalidRequestURL The request URL is not valid.
ErrCodeObjectNotFound Given the bearer token used, the resource does not exist.
ErrCodeRateLimited This request exceeds the number of requests allowed.
ErrCodeRestrictedResource Given the bearer token used, the client doesn't have permission to perform this operation.
ErrCodeServiceUnavailable Notion is unavailable.
ErrCodeUnauthorized The bearer token is not valid.
ErrCodeValidationError The request body does not match the schema for the expected parameters.
FormulaValueType enums.
FormulaValueType enums.
FormulaValueType enums.
FormulaValueType enums.
MentionType enums.
MentionType enums.
MentionType enums.
MentionType enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
NumberFormat enums.
ObjectType enums.
ObjectType enums.
ObjectType enums.
ObjectType enums.
ObjectType enums.
ParentType enums.
ParentType enums.
ParentType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
PropertyType enums.
RichTextType enums.
RichTextType enums.
RichTextType enums.
RollupValueType enums.
RollupValueType enums.
RollupValueType enums.
UserBot means User objects represent bot.
UserPerson means User objects represent people.

# Structs

Annotation is style information which applies to the whole rich text object.
Block object represents content within Notion.
QueryDatabase related types.
ChildPage contains information of child page.
Client is implement of API.
Database objects describe the property schema of a database in Notion.
Date represents a datetime or time range.
QueryDatabase related types.
Equation .
Error is Notion error response body.
File reference is an object with an name property, with a string value corresponding to a filename of the original file upload (i.e.
QueryDatabase related types.
QueryDatabase related types.
QueryDatabase related types.
FormulaValue represents the result of evaluating a formula described in the database's properties.
Heading is the common type of Heading1, Heading2, Heading3.
Link objects contain a type key whose value is always "url" and a url key whose value is a web address.
List is Pagination response type.
ListItem is the common type of BulletedListItem and NumberedListItem.
Mention objects represent an inline mention of a user, page, database, or date.
QueryDatabase related types.
QueryDatabase related types.
OAuthAccessToken is the response of OAuth token exchanging.
OAuthClient is client to exchange OAuth token.
Object is the mix type of Top-level resources.
ObjectReference is simple type of Object that just contains id and type.
Page object contains the property values of a single Notion page.
Paragraph is paragraph block.
Parent represents the Page parent.
QueryDatabase related types.
Property is mix type of database property.
PropertyValue is the property value of Page.
QueryDatabase related types.
QueryDatabase related types.
RichText objects contain data for displaying formatted text, mentions, and equations.
RollupValue represent the result of evaluating a rollup described in the database's properties.
No description provided by the author
No description provided by the author
QueryDatabase related types.
SelectOption is the option value of single selector and multi selector.
Settings is configuration of Client.
Sort sorts the results based on the provided criteria.
Text is the content of rich text.
QueryDatabase related types.
ToDo is todo item.
Toggle is toggle item.
The User object represents a user in a Notion workspace.

# Interfaces

API is declaration of Notion.so APIs.

# Type aliases

BlockType is type of Block.
Color is color definition of Notion.
ErrCode is the "code" field in Notion error.
FormulaValueType is type of formula value.
MentionType is type of Mention.
NumberFormat is format of number property value.
Objects is wrapper of Object list.
ObjectType is enum of Notion top level resource types.
ParentType is type of Parent.
PropertyType is type of database Property.
RichTextType is type of RichText.
RollupValueType is type of rollup value.
SortDirection query result order.
UserType is type of User.