SKYCUBE.net

Solutions for Go, MySQL, PHP, Linux and more

Go (lang) library to get cryptocurrency data (current and historical)

Posted — Mar 13, 2018

Crypto currencies are getting more popular and while working with them I have written a reusable library.

The library cryptocurrencydata is licensed under MIT license.

It is written in Go (lang) my choice of code and work environment at the time.

The state of library is for production. And I hope it might be useful for some folks.

The library is able to not only provide you with current details, it is also able to receive historical data for currencies.

The way it is written, you can easy plug it into you current Go program/ Microservice.

As an example to receive the current US Dollar value of certain currencies you can just call the library by one call:

PriceUSDByIDs

d, err := cryptocurrencydata.PriceUSDByIDs([]string{"bitcoin", "ethereum", "skycoin"})
if err != nil {
    fmt.Printf("%v", err)
}

for _, v := range d {
    fmt.Printf("%-10s \t $%.2f\n", v.ID, v.Value)
}

This will get you the output of the following:

# GetPriceUSDByCurrencyIDs
bitcoin    	 $10638.00
ethereum   	 $787.79
skycoin    	 $14.02

GetCurrencyDataByCurrencyIDs

If you wish to get all details you can also get all details:

d, err := cryptocurrencydata.GetCurrencyDataByIDs([]string{"bitcoin", "ethereum", "skycoin"})
if err != nil {
    fmt.Printf("%v", err)
}

for _, v := range d {
    fmt.Printf("%v\n", v)
}

This will get you the output of the following:

{bitcoin Bitcoin BTC 1 10638 1 6.91178e+09 1.79837645256e+11 1.6905212e+07 1.6905212e+07 2.1e+07 0.55 -3.61 -0.32 2018-03-07 11:39:26 +0000 UTC}
{ethereum Ethereum ETH 2 787.794 0.074382 1.86455e+09 7.7233590834e+10 9.80378e+07 9.80378e+07 0 0.31 -5.76 -9.69 2018-03-07 11:39:12 +0000 UTC}
{skycoin Skycoin SKY 120 14.0169 0.00132345 552055 1.07331524e+08 7.657294e+06 2.5e+07 1e+08 2.5 -5.5 -16.3 2018-03-07 11:39:10 +0000 UTC}

GetCurrencyHistoryByCurrencyID

To receive the currency history of a single currency:

d, err := cryptocurrencydata.GetCurrencyHistoryByCurrencyID("skycoin")
if err != nil {
    fmt.Printf("%v", err)
}

for _, v := range d {
    fmt.Printf("timestamp %v timeUTC: %v MarketSupply: %d PriceUSD: $%.2f PriceBTC: $%.2f VolUSD: $%d\n", v.Timestamp, v.TimeUTC, v.MarketSupply, v.PriceUSD, v.PriceBTC, v.VolUSD)
}

This will get you the output of the following:

timestamp 1519029250000 timeUTC: 2018-02-19 08:34:10 +0000 UTC MarketSupply: 147280561 PriceUSD: $19.76 PriceBTC: $0.00 VolUSD: $670990
timestamp 1496298589000 timeUTC: 2017-06-01 06:29:49 +0000 UTC MarketSupply: 8866853 PriceUSD: $1.63 PriceBTC: $0.00 VolUSD: $73770
timestamp 1507642758000 timeUTC: 2017-10-10 13:39:18 +0000 UTC MarketSupply: 17325056 PriceUSD: $2.92 PriceBTC: $0.00 VolUSD: $9757
timestamp 1509521956000 timeUTC: 2017-11-01 07:39:16 +0000 UTC MarketSupply: 24330265 PriceUSD: $4.10 PriceBTC: $0.00 VolUSD: $33590
timestamp 1509845958000 timeUTC: 2017-11-05 01:39:18 +0000 UTC MarketSupply: 24284342 PriceUSD: $4.09 PriceBTC: $0.00 VolUSD: $13874
timestamp 1513735163000 timeUTC: 2017-12-20 01:59:23 +0000 UTC MarketSupply: 107126489 PriceUSD: $16.92 PriceBTC: $0.00 VolUSD: $320845
...

Documentation is available via godocs https://godoc.org/github.com/skycube/cryptocurrencydata

This is also an ongoing project and improvements will be made. If you have questions or would like to contribute please do this via GitHub issue section of project.