go-notion
go-notion copied to clipboard
Notion Official API Go Client.
go-notion
Go SDK for Notion Official API.
go get github.com/sorcererxw/go-notion
- Overview
-
Getting Started
- Pagination
- Error Handling
- Reverse Proxy
- OAuth
- License
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.