GO
Koios API Client Library for Go
Overview
A GO package for Cardano Blockchain using Koios API
Below there is an example to study, while to see the full koios-go-client documentation you can find it here
Install
Tips
To install the module, type the following command:
go get github.com/cardano-community/koios-go-client/v3
Import the module
Tips
To use the module, see the following example:
import (
"github.com/cardano-community/koios-go-client/v3" // imports as package "koios"
)
Examples
Basic usage
package main
import (
"context"
"fmt"
"log"
koios "github.com/cardano-community/koios-go-client/v3"
)
func main() {
// Call to koios.New without options is same as calling it with default opts.
// See godoc for available configuration options.
// api, err := koios.New(
// koios.Host(koios.MainnetHost),
// koios.APIVersion(koios.DefaultAPIVersion),
// koios.Port(koios.DefaultPort),
// koios.Schema(koios.DefaultSchema),
// koios.HttpClient(koios.DefaultHttpClient),
// ).
api, err := koios.New()
if err != nil {
log.Fatal(err)
}
res, err := api.GetTip(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
fmt.Println("status: ", res.Status)
fmt.Println("statu_code: ", res.StatusCode)
fmt.Println("abs_slot: ", res.Data.AbsSlot)
fmt.Println("block_no: ", res.Data.BlockNo)
fmt.Println("block_time: ", res.Data.BlockTime)
fmt.Println("epoch: ", res.Data.Epoch)
fmt.Println("epoch_slot: ", res.Data.EpochSlot)
fmt.Println("hash: ", res.Data.Hash)
}
Concurrency
func main() {
api, _ := koios.New(
// limit client request 1 per second even though
// this example will send requests in goroutines.
koios.RateLimit(1),
)
ctx := context.Background()
var wg sync.WaitGroup
servers := []string{
koios.GuildHost,
koios.PreviewHost,
koios.TestnetHost,
koios.MainnetHost,
koios.PreProdHost,
koios.MainnetHost,
koios.MainnetHostEU,
}
// Thanks to rate limit option requests will be made
// once in a second.
for _, host := range servers {
wg.Add(1)
go func(ctx context.Context, host string) {
defer wg.Done()
// switching host by creating light clone of client
// with new options
client, err := api.WithOptions(koios.Host(host))
res, _ := client.GET(ctx, "/tip", nil)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println("Host: ", host)
fmt.Println("Response: ", string(body))
}(ctx, host)
}
wg.Wait()
}