Skip to content

dtmirizzi/go-notion

 
 

Repository files navigation

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.

About

Notion Official API Go Client.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%