Koios API is Elastic Cardano Query Layer!
A consistent query layer for developers to build upon Cardano, with multiple, redundant endpoints that allow for easy scalability.
Koios API Client Library for Go
go get github.com/cardano-community/koios-go-client/v4
...
import (
"github.com/cardano-community/koios-go-client/v4" // imports as package "koios"
)
...
There is CLI Application to interact with Koios API from Command-line see:
koios cli repository for souce and installing instruction of koios cli
Build Status
Koios API Endpoint Tests
Development Status
Additionally you can find all usecases by looking source of koio-cli
Command-line application koios-cli which utilizes entire API of this library.
NOTE
Library normalizes some of the API responses and constructs Typed response for each end point.
If you wish to work with *http.Response
directly you can do so by using api client GET,POST, HEAD
methods.
package main
import (
"context"
"fmt"
"log"
koios "github.com/cardano-community/koios-go-client/v4"
)
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)
}
This library is thread-safe so you can freerly use same api client instance passing it to your goroutines.
Following example uses goroutines to query chain tip from different endpoints.
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()
}
Library uses decimal.Decimal
data type to represent lovelace and coin values.
Which provides arbitrary-precision fixed-point decimal numbers in go.
For decimal package API see
We would love for you to contribute to Koios API Client Library for Go and help make it even better than it is today! As a contributor, here are the guidelines we would like you to follow:
Help us keep Koios API Client Library for Go open and inclusive. Please read and follow our Code of Conduct
Do not open issues for general support questions as we want to keep GitHub issues for bug reports and feature requests. You've got much better chances of getting your question answered on Koios Telegram Group
If you find a bug in the source code, you can help us by submitting an issue to our GitHub Repository. Even better, you can submit a Pull Request with a fix.
You can request a new feature by submitting an issue to our GitHub Repository. If you would like to implement a new feature, please submit an issue with a proposal for your work first, to be sure that we can use it. Please consider what kind of change it is:
Before you submit an issue, please search the issue tracker, maybe an issue for your problem already exists and the discussion might inform you of workarounds readily available.
You can file new issues by filling out our new issue form.
Before you submit your Pull Request (PR) consider the following guidelines:
Setup you local repository
git@github.com:<your-github-username>/koios-go-client.git
cd koios-go-client
git remote add upstream git@github.com:cardano-community/koios-go-client.git
Make your changes in a new git branch and ensure that you always start from up to date main branch. Repeat this step every time you are about to start woking on new PR.
e.g. Start new change work to update readme:
# if you are not in main branch e.g. still on previous work branch
git checkout main
git pull --ff upstream main
git checkout -b update-readme main
Commit your changes using a descriptive commit message that follows our commit message conventions. Adherence to these conventions is necessary because release notes are automatically generated from these messages.
git add -A
git commit --signoff
# or in short
git commit -sm"docs(markdown): update readme examples"
Push your branch to GitHub:
git push -u origin update-readme
In GitHub, send a pull request to main
branch.
Rebase your branch and force push to your GitHub repository (this will update your Pull Request):
git fetch --all
git rebase upstream main
git push -uf origin update-readme
That's it! Thank you for your contribution!
After your pull request is merged, you can safely delete your branch and pull the changes from the main (upstream) repository:
Delete the remote branch on GitHub either through the GitHub web UI or your local shell as follows:
git push origin --delete update-readme
Check out the main branch:
git checkout main -f
Delete the local branch:
git branch -D update-readme
Update your master with the latest upstream version:
git pull --ff upstream main
To ensure consistency throughout the source code, keep these rules in mind as you are working:
We have very precise rules over how our git commit messages can be formatted. This leads to more readable messages that are easy to follow when looking through the project history. Commit messages should be well formatted, and to make that "standardized", we are using Conventional Commits. Our release workflow uses these rules to generate changelogs.
Each commit message consists of a header, a body and a footer. The header has a special format that includes a type, a scope and a subject:
<type>(<scope>): <subject>
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
When maintainers are merging PR merge commit should be edited:
<type>(<scope>): <subject> (#pr)
<BLANK LINE>
<body>
<BLANK LINE>
<footer>
The header is mandatory and the scope of the header is optional.
Any line of the commit message cannot be longer 100 characters! This allows the message to be easier to read on GitHub as well as in various git tools.
The footer should contain a closing reference to an issue if any.
Samples:
docs(markdown): update readme examples
fix(endpoint): update Tip endpoint to latest specs.
description of your change.
refactor(client): change Client GET function signature
change order of client GET method arguments.
BREAKING CHANGE: Clien.Get signature has changed
If the commit reverts a previous commit, it should begin with revert:
, followed by the header of the reverted commit. In the body it should say: This reverts commit <hash>.
, where the hash is the SHA of the commit being reverted.
Must be one of the following:
go.mod
The following is the list of supported scopes:
scope | description |
---|---|
client | API client related changes |
endpoint | Changes related to api endpoints |
godoc | Go documentation |
markdown | Markdown files |
The subject contains a succinct description of the change:
Just as in the subject, use the imperative, present tense: "change" not "changed" nor "changes". The body should include the motivation for the change and contrast this with previous behavior.
The footer should contain any information about Breaking Changes and is also the place to reference GitHub issues that this commit Closes.
Breaking Changes should start with the word BREAKING CHANGE:
with a space or two newlines. The rest of the commit message is then used for this.
A detailed explanation can be found in this [document][commit-message-format].
Prerequisites:
Setup you local repository
git@github.com:<your-github-username>/koios-go-client.git
cd koios-go-client
git remote add upstream git@github.com:cardano-community/koios-go-client.git
Setup local env
task setup
Lint your code
task lint
Test your change
task test
View code coverage report from in browser (results from task test
)
task cover
Original author.
koios-go-client was moved under Cardano Community from howijd/koios-rest-go-client
Koios Go banner design.
Koios Go banner was designed by Egon Elbre egonelbre.com