visit
Boost your Golang backend with the supabase API: A seamless guide to setting up and utilizing supabase for efficient database operations and authentication.
supabase has emerged as a compelling open-source alternative to Firebase, offering a suite of tools including a database, authentication, real-time subscriptions, and storage. For Golang developers, integrating it can seem daunting due to the lack of proper library support.
However, the library provides a simplified way to interact with supabase
in your Golang projects. This article will guide you through setting up the library and demonstrate its use in a Golang backend project, ensuring you can leverage supabase
’s powerful features with minimal hassle.
To begin, install in your Golang project.
go get github.com/lengzuo/supa
Before diving into the code, ensure you have setup a supabase project. From your supabase project dashboard, note down the Project Ref and the Project API keys. These will be required to connect your Golang application to supabase.
Import the library along with other necessary packages in your Golang project.
package main
import (
"fmt"
"github.com/lengzuo/supa"
)
func main() {
conf := supabase.Config{
// Your project api key, you can use either `anon` or `service_role`.
// but i will suggest you to use `service_role` as your api key and keep it secret.
ApiKey: "your-project-api-key",
// Retrieve your project ref from project url
// eg: //this-your-project-ref.supabase.co
ProjectRef: "your-project-ref",
// Set it `false` in production to avoid extra log print.
Debug: true,
}
supaClient, err := supabase.New(conf)
if err != nil {
fmt.Println("failed in init supa client: ", err)
return
}
}
Replace your-project-api-key and your-project-ref according from your supabase project.
func signUp(supaClient *supabase.Client) {
body := dto.SignUpRequest{
Email: "[email protected]",
Password: "user-password",
}
resp, err := supaClient.Auth.SignUp(ctx, body)
if err != nil {
var supaErr catch.Exception
// use this to catch the error of http status code != 2xx
if errors.As(err, &supaErr) {
log.Error("status: %d, err: %s", supaErr.StatusCode(), supaErr.Error())
return
}
fmt.Println("failed in sign up: ", err)
return
}
bytes, _ := json.Marshal(resp)
fmt.Printf("sign up success: %s", bytes)
}
func signInWithPassword(supaClient *supabase.Client) {
body := dto.SignInRequest{
Email: "[email protected]",
Password: "user-password",
}
resp, err := supaClient.Auth.SignInWithPassword(ctx, body)
if err != nil {
...
}
bytes, _ := json.Marshal(resp)
fmt.Printf("sign in with password success: %s", bytes)
}
func getAuthUser(supaClient *supabase.Client) {
token = "logged-in-access-token"
user, err := supaClient.Auth.User(ctx, token)
if err != nil {
...
}
bytes, _ := json.Marshal(resp)
fmt.Printf("sign in with password success: %s", bytes)
}
There are more example functions such as RPC and DB operation in the repo for your reference.
In my personal experience with other supabase Golang libraries, I find the library to be the most developer-friendly. It offers extensive authentication APIs such as SignInWithOTP, SignInWithOAuth, and more. Additionally, it allows developers to pass in context
for each operation.