CoinGlass Golang SDK

September 4, 2026 · View on GitHub

CoinGlass Golang SDK

CI Tests CodeQL Codecov Go Reference Go version License

A Go client for the Coinglass API v4, with no external dependencies.

coinglass-go is a small, typed Go client for the Coinglass API v4. I built it to scratch my own itch while working on liquidation dashboards and funding-rate tooling, and it covers the Futures, Spot, Options, ETF, and Indicator endpoints. It leans entirely on the standard library, so adding it to your project won't drag in a tree of transitive dependencies.

Documentation

Highlights

  • Covers all five endpoint groups: Futures, Spot, Options, ETF, and Indicators.
  • Includes a WebSocket client for real-time liquidation, trade, and futures ticker streams.
  • No third-party dependencies — just the standard library, for both the REST and WebSocket clients.
  • Configured with functional options, the way most Go clients do it.
  • Every method takes a context.Context, and the client is safe to share across goroutines.
  • Retries rate-limited (429) requests with exponential backoff, and honors Retry-After when the API sends it.
  • Returns typed sentinel errors for 401, 404, and 429, and a detailed APIError (with the API's own code/msg) for anything else.
  • Each service ships with its own httptest-based tests.

Requirements

RequirementVersion
Go1.21+
Dependenciesnone (standard library only)
Coinglass API keyGet one here

Installation

go get github.com/tigusigalpa/coinglass-go

Getting started

package main

import (
    "context"
    "fmt"
    "log"
    "time"

    coinglass "github.com/tigusigalpa/coinglass-go"
)

func main() {
    ctx := context.Background()

    client := coinglass.NewClient("YOUR_API_KEY",
        coinglass.WithTimeout(15*time.Second),
        coinglass.WithRetry(3, time.Second), // 3 attempts, 1s initial backoff
    )

    // BTC open interest history (last 30 days, daily)
    oi, err := client.Futures.OpenInterestHistory(ctx, &coinglass.OIHistoryParams{
        Symbol:   "BTC",
        Interval: "1d",
        Limit:    coinglass.IntPtr(30),
    })
    if err != nil {
        log.Fatal(err)
    }
    for _, point := range oi {
        fmt.Printf("OI: %.2f USD at %d\n", point.OpenInterestUsd, point.Timestamp)
    }
}

Compile and run it:

go run main.go

Configuring the client

NewClient takes functional options:

client := coinglass.NewClient("YOUR_API_KEY",
    coinglass.WithBaseURL("https://open-api-v4.coinglass.com"), // default, shown for clarity
    coinglass.WithTimeout(15*time.Second),
    coinglass.WithRetry(3, time.Second),
    coinglass.WithHTTPClient(&http.Client{}), // custom transport, proxies, TLS, etc.
)
OptionWhat it doesWhen to use it
WithBaseURL(url string)Overrides the API base URL.Mocking the API in tests or using a custom gateway.
WithHTTPClient(client *http.Client)Supplies your own HTTP client.Custom TLS, proxies, tracing, or middleware.
WithTimeout(d time.Duration)Sets the per-request timeout.Default is 30s; lower it for fast UI endpoints.
WithRetry(maxAttempts int, baseDelay time.Duration)Retries on HTTP 429 with exponential backoff.Strongly recommended for production workloads.

You can also read the API key from the COINGLASS_API_KEY environment variable:

client, err := coinglass.NewClientFromEnv(coinglass.WithTimeout(15 * time.Second))
if err != nil {
    log.Fatal(err)
}

Rate limits by plan

PlanRequests/min
Hobbyist30
Startup80
Standard300
Professional1200

If you enable retries, the client backs off automatically and doubles the wait on each attempt (or uses the Retry-After header when Coinglass sends one):

client := coinglass.NewClient("YOUR_API_KEY",
    coinglass.WithRetry(3, time.Second), // 1s, then 2s, then 4s
)

Full API reference

Futures — client.Futures

MethodEndpointDescription
SupportedCoins(ctx)GET /futures/supported-coinsSupported futures coins
SupportedExchangePairs(ctx, params)GET /api/futures/supported-exchange-pairsSupported exchange pairs
CoinsMarkets(ctx, params)GET /api/futures/coins-marketsFutures coin markets
PairsMarkets(ctx, params)GET /api/futures/pairs-marketsFutures pair markets
PriceChangeList(ctx)GET /futures/price-change-listPrice change list
OpenInterestHistory(ctx, params)GET /api/futures/openInterest/ohlc-historyOI OHLC history
OpenInterestAggregatedHistory(ctx, params)GET /api/futures/openInterest/ohlc-aggregated-historyAggregated OI OHLC
OpenInterestExchangeList(ctx, params)GET /api/futures/openInterest/exchange-listOI by exchange
FundingRateHistory(ctx, params)GET /api/futures/fundingRate/ohlc-historyFunding rate OHLC
FundingRateOiWeighted(ctx, params)GET /api/futures/fundingRate/oi-weight-ohlc-historyOI-weighted funding rate
FundingRateExchangeList(ctx, params)GET /api/futures/fundingRate/exchange-listFunding rate by exchange
FundingRateArbitrage(ctx, params)GET /api/futures/fundingRate/arbitrageFunding arbitrage
LongShortRatioHistory(ctx, params)GET /api/futures/global-long-short-account-ratio/historyGlobal L/S ratio
TopLongShortRatioHistory(ctx, params)GET /api/futures/top-long-short-account-ratio/historyTop trader L/S ratio
LiquidationHistory(ctx, params)GET /api/futures/liquidation/historyPair liquidation history
LiquidationAggregatedHistory(ctx, params)GET /api/futures/liquidation/aggregated-historyCoin liquidation history
LiquidationCoinList(ctx, params)GET /api/futures/liquidation/coin-listLiquidation coin list
LiquidationHeatmap(ctx, model, params)GET /api/futures/liquidation/heatmap/model{1,2,3}Liquidation heatmaps
LiquidationMap(ctx, params)GET /api/futures/liquidation/mapLiquidation map
OrderbookHistory(ctx, params)GET /api/futures/orderbook/historyOrderbook heatmap
LargeOrders(ctx, params)GET /api/futures/orderbook/large-limit-orderLarge orderbook orders
TakerBuySellHistory(ctx, params)GET /api/futures/taker-buy-sell-volume/historyTaker buy/sell history
WhaleAlert(ctx, params)GET /api/hyperliquid/whale-alertHyperliquid whale alert

Spot — client.Spot

MethodEndpointDescription
SupportedCoins(ctx)GET /api/spot/supported-coinsSupported coins
CoinsMarkets(ctx, params)GET /api/spot/coins-marketsCoins markets
PairsMarkets(ctx, params)GET /api/spot/pairs-marketsPairs markets
PriceHistory(ctx, params)GET /api/spot/price/historyPrice OHLC history
OrderbookHistory(ctx, params)GET /api/spot/orderbook/historyOrderbook heatmap
TakerBuySellHistory(ctx, params)GET /api/spot/taker-buy-sell-volume/historyTaker buy/sell history

Options — client.Options

MethodEndpointDescription
MaxPain(ctx, params)GET /api/option/max-painOption max pain
Info(ctx, params)GET /api/option/infoOptions info
ExchangeOIHistory(ctx, params)GET /api/option/exchange-oi-historyExchange OI history
ExchangeVolHistory(ctx, params)GET /api/option/exchange-vol-historyExchange volume history

ETF — client.ETF

MethodEndpointDescription
BitcoinList(ctx)GET /api/etf/bitcoin/listBitcoin ETF list
BitcoinFlowHistory(ctx, params)GET /api/etf/bitcoin/flow-historyBTC ETF flows
BitcoinNetAssetsHistory(ctx, params)GET /api/etf/bitcoin/net-assets/historyETF net assets
EthereumList(ctx)GET /api/etf/ethereum/listEthereum ETF list
EthereumFlowHistory(ctx, params)GET /api/etf/ethereum/flow-historyETH ETF flows
GrayscaleHoldings(ctx)GET /api/grayscale/holdings-listGrayscale holdings

Indicators — client.Indicators

MethodEndpointDescription
FearGreedHistory(ctx, params)GET /api/index/fear-greed-historyFear & Greed index
RSIList(ctx, params)GET /api/futures/rsi/listRSI list
BasisHistory(ctx, params)GET /api/futures/basis/historyFutures basis
CoinbasePremium(ctx, params)GET /api/coinbase-premium-indexCoinbase premium
BitcoinRainbowChart(ctx)GET /api/index/bitcoin/rainbow-chartBTC rainbow chart
StockToFlow(ctx)GET /api/index/stock-flowStock-to-Flow model
StablecoinMarketCap(ctx, params)GET /api/index/stableCoin-marketCap-historyStablecoin market cap

Error handling

Any non-2xx response, or a response whose Coinglass envelope code isn't zero, comes back as an *coinglass.APIError:

type APIError struct {
    StatusCode int
    Code       string
    Message    string
    RawBody    []byte
}

For the common cases, match on the sentinel errors with errors.Is; when you need the details, pull out the *APIError with errors.As:

import "errors"

oi, err := client.Futures.OpenInterestHistory(ctx, params)
if err != nil {
    switch {
    case errors.Is(err, coinglass.ErrUnauthorized):
        log.Fatal("Invalid API key")
    case errors.Is(err, coinglass.ErrRateLimited):
        log.Println("Rate limited — retries exhausted")
    default:
        var apiErr *coinglass.APIError
        if errors.As(err, &apiErr) {
            log.Printf("API error %d (%s): %s", apiErr.StatusCode, apiErr.Code, apiErr.Message)
        }
    }
}

WebSocket API

Alongside the REST client, coinglass-go ships a small WebSocket client under the websocket subpackage for Coinglass's real-time streams — liquidation orders, spot and futures trades, and futures ticker snapshots. It's built entirely on the standard library too: the WebSocket handshake, framing, and masking are implemented directly on top of net/crypto/tls, so no third-party dependency is pulled in.

package main

import (
    "context"
    "log"
    "os"
    "os/signal"
    "syscall"

    coinglass "github.com/tigusigalpa/coinglass-go"
    "github.com/tigusigalpa/coinglass-go/websocket"
)

func main() {
    client := coinglass.NewClient(os.Getenv("COINGLASS_API_KEY"))
    ws := client.WSClient()

    ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
    defer stop()

    stream, err := ws.Connect(ctx)
    if err != nil {
        log.Fatal(err)
    }
    defer stream.Close()

    stream.Subscribe(
        websocket.ChannelLiquidationOrders(),
        websocket.ChannelFuturesTicker("Binance", "BTCUSDT"),
    )

    for {
        select {
        case <-ctx.Done():
            return
        case msg, ok := <-stream.Messages():
            if !ok {
                return
            }
            if msg.Channel == websocket.ChannelLiquidationOrders() {
                orders, _ := websocket.DecodeLiquidationOrders(msg.Data)
                for _, o := range orders {
                    log.Printf("%s %s liquidated %.2f USD", o.Exchange, o.Symbol, o.VolumeUSD)
                }
            }
        case err := <-stream.Errors():
            log.Println("stream error:", err)
        }
    }
}

A single connection carries every subscription; Subscribe/Unsubscribe accept any number of channel names, and the client sends the "ping" heartbeat every 20 seconds that Coinglass expects to keep the socket open.

Channel helpers

HelperChannelDocs
websocket.ChannelLiquidationOrders()liquidation_ordersLiquidation Order
websocket.ChannelSpotTrades(exchange, symbol, minVolumeUSD)spot_trades@{exchange}_{symbol}@{minVolumeUSD}Spot Trade Order
websocket.ChannelFuturesTrades(exchange, symbol, minVolumeUSD)futures_trades@{exchange}_{symbol}@{minVolumeUSD}Futures Trade Order
websocket.ChannelFuturesTicker(exchange, symbol)futures_ticker@{exchange}_{symbol}Futures Ticker Snapshot

Each channel has a matching decode helper — DecodeLiquidationOrders, DecodeTrades, and DecodeFuturesTicker — that unmarshals Message.Data into typed structs.

Context and concurrency

Every method takes a context, and a single Client is safe to use from multiple goroutines:

var wg sync.WaitGroup
for _, symbol := range []string{"BTC", "ETH", "SOL"} {
    wg.Add(1)
    go func(sym string) {
        defer wg.Done()
        oi, err := client.Futures.OpenInterestHistory(ctx, &coinglass.OIHistoryParams{
            Symbol:   sym,
            Interval: "1d",
        })
        if err != nil {
            log.Printf("%s failed: %v", sym, err)
            return
        }
        log.Printf("%s: %d points", sym, len(oi))
    }(symbol)
}
wg.Wait()

Pointer helpers

Optional parameters are pointer fields, so the client can tell "not set" apart from a real zero value. These helpers save you a few lines when passing literals:

coinglass.IntPtr(30)
coinglass.StringPtr("BTC")
coinglass.BoolPtr(true)
coinglass.Int64Ptr(1690000000)
coinglass.Float64Ptr(1.5)

Examples

There are a few runnable examples in the examples/ directory:

ExampleWhat it shows
examples/basicClient setup, Futures/Spot/Options queries, error handling
examples/etfBitcoin/Ethereum ETF flows, Grayscale holdings, Fear & Greed Index
examples/concurrencySharing a single Client safely across goroutines
examples/websocketSubscribing to liquidation orders, trades, and futures ticker streams

Run any of them with:

export COINGLASS_API_KEY=your-api-key
go run ./examples/basic

Running the tests

go test ./...

Race detection requires CGO_ENABLED=1 and a C toolchain:

go test -race ./...

The same checks run automatically on every push and pull request through GitHub Actions.

License

MIT © Igor Sazonov