Golang
Create a new project
Create a new project with the following command:
mkdir formance-go-demo
cd formance-go-demo
go mod init example/formance-go-demo
Install the Formance SDK
Install the Formance SDK with the following command:
go get github.com/formancehq/formance-sdk-go/v3
The Formance API relies on OAuth 2.0 for authentication.
For more information, see OAuth 2.0.
In this demo, we will setup the Formance SDK with the client credentials grant type, which requires a client ID and client secret. These are sensitive credentials that should not be exposed to the public.
If you're integrating the Formance SDK in a client-side application, you should should use an OAuth 2.0 flow that does not expose the client secret, such as the authorization code grant.
Hello world from the Formance Ledger
Create a new file called main.go
with the following content:
package main
import(
"github.com/formancehq/formance-sdk-go/v3/pkg/models/shared"
formancesdkgo "github.com/formancehq/formance-sdk-go/v3"
"context"
"github.com/formancehq/formance-sdk-go/v3/pkg/models/operations"
"log"
)
func main() {
s := formancesdkgo.New(
formancesdkgo.WithServerURL("https://<YOUR_SERVER_URL_HERE>"),
formancesdkgo.WithSecurity(shared.Security{
ClientID: formancesdkgo.String("<YOUR_CLIENT_ID_HERE>"),
ClientSecret: formancesdkgo.String("<YOUR_CLIENT_SECRET_HERE>"),
}),
)
ctx := context.Background()
res, err := s.Ledger.V2.ListLedgers(ctx, operations.V2ListLedgersRequest{
PageSize: formancesdkgo.Int64(100),
})
if err != nil {
log.Fatal(err)
}
fmt.Println("response:", res.StatusCode)
}
You'll need the API endpoint and the credentials created during the SDK's Getting Started guide. If you don't have them, you can create them by following the guide.
Run the project
Run the project with the following command:
go run main.go
You should see the version of the Formance Ledger printed to the console, for example:
2.1.0