Skip to content

Installation & Configuration

Igor Sazonov edited this page Jul 12, 2026 · 1 revision

Installation

The SDK requires Go 1.21 or later. Install it using go get:

go get github.com/tigusigalpa/coinglass-go

Configuration

The client is configured using the functional options pattern. You can initialize it with an API key directly or read it from the environment.

Basic Initialization

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

client := coinglass.NewClient("YOUR_API_KEY")

From Environment Variable

If you prefer keeping credentials out of your code, set the COINGLASS_API_KEY environment variable:

export COINGLASS_API_KEY="your_api_key_here"
client, err := coinglass.NewClientFromEnv()
if err != nil {
    log.Fatal(err) // Returns ErrUnauthorized if the variable is missing
}

Functional Options

You can pass multiple options to NewClient or NewClientFromEnv to customize the HTTP client behavior.

Option Description Example
WithTimeout(d time.Duration) Sets the HTTP client timeout (default: 30s). coinglass.WithTimeout(15 * time.Second)
WithRetry(maxAttempts int, baseDelay time.Duration) Enables automatic retries on HTTP 429 with exponential backoff. coinglass.WithRetry(3, 1 * time.Second)
WithBaseURL(url string) Overrides the API base URL (useful for testing/mocking). coinglass.WithBaseURL("http://localhost:8080")
WithHTTPClient(client *http.Client) Uses a custom *http.Client (useful for proxies or custom TLS). coinglass.WithHTTPClient(myClient)

Example: Full Configuration

client := coinglass.NewClient("YOUR_API_KEY",
    coinglass.WithTimeout(10 * time.Second),
    coinglass.WithRetry(5, 500 * time.Millisecond),
    coinglass.WithHTTPClient(&http.Client{
        Transport: myCustomTransport,
    }),
)

Helper Functions

For endpoints that take optional parameters, the SDK uses pointers to distinguish between zero values and omitted values. To make constructing requests easier, the package provides helper functions:

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

Usage:

params := &coinglass.OIHistoryParams{
    Symbol: "BTC",
    Limit:  coinglass.IntPtr(50), // Easily pass a pointer to an int
}

Clone this wiki locally