Go SDK for Notion Official API.
go get github.com/sorcererxw/go-notion
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.
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")
}
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
}
}
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")
}
}
}
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,
})
}
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)
})
}
go-notion is distributed under MIT.