Skip to content

REST Client and Configuration

Igor Sazonov edited this page Aug 9, 2026 · 1 revision

NewRestClient is the standard constructor for REST use. It wires a shared low-level HTTP transport into the Market, Account, and Trade service clients, so configuration applies consistently across all three services. 1

client := bitget.NewRestClient(apiKey, secretKey, passphrase, opts...)

Every network method accepts context.Context as its first parameter. This makes cancellation and request deadlines part of the call site rather than hidden client state. 2

Defaults

Setting Default Notes
REST base URL https://api.bitget.com Production UTA REST endpoint.
Internal HTTP timeout 15s Used only when no custom *http.Client is supplied.
Locale header en-US Sent as locale on every REST request.
Logger No-op No log entries are emitted unless a logger is configured.
Demo trading Disabled Must be explicitly enabled.

These defaults are established by the root client and options implementation. 2 3

Available options

Option Use it when Important behavior
WithHTTPClient(*http.Client) You need a proxy, custom transport, test double, or custom timeout policy. The supplied client is used as-is; WithTimeout does not modify it.
WithBaseURL(string) You need an approved Bitget alternative domain or an httptest server. The value replaces the REST base URL.
WithDemoTrading() You use Demo API credentials for private REST requests. Adds paptrading: 1 to signed requests.
WithTimeout(time.Duration) The internally created HTTP client needs a different timeout. Ignored when WithHTTPClient is also supplied.
WithLogger(Logger) You want structured request diagnostics. Credentials, signatures, and passphrases are not logged by the SDK.
WithLocale(string) Bitget responses should use a different supported locale. Replaces the default en-US header value.

A production-style constructor

A caller-managed HTTP client is appropriate when transport settings are part of the application's shared operational configuration:

package main

import (
    "log/slog"
    "net/http"
    "os"
    "time"

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

func newBitgetClient() *bitget.RestClient {
    logger := slog.Default()

    return bitget.NewRestClient(
        os.Getenv("BITGET_API_KEY"),
        os.Getenv("BITGET_SECRET_KEY"),
        os.Getenv("BITGET_PASSPHRASE"),
        bitget.WithHTTPClient(&http.Client{Timeout: 20 * time.Second}),
        bitget.WithLogger(bitget.NewSlogLogger(logger)),
        bitget.WithLocale("en-US"),
    )
}

NewSlogLogger adapts a standard-library *slog.Logger to the SDK's small Logger interface. A custom logger only needs Debug, Info, Warn, and Error methods with the same variadic argument shape. 3

Context deadlines

Prefer a deadline for operational requests. This protects the caller from waiting indefinitely when a request no longer matters:

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

positions, err := client.Trade.GetPositions(
    ctx,
    models.CategoryUSDTFutures,
    "BTCUSDT",
    "",
)

The SDK constructs requests with http.NewRequestWithContext, so a canceled context is propagated to the HTTP operation. 2

Authentication and signing

For signed calls, the client serializes the body as JSON, sorts and URL-encodes non-empty query parameters, and signs the following pre-hash string:

timestamp + METHOD + requestPath [+ "?" + queryString] + body

It then places the HMAC-SHA256/Base64 value into ACCESS-SIGN with the rest of Bitget's required authentication headers. This mirrors the official UTA signature procedure. 2 4

Public market methods use the same transport but are explicitly unsigned. Therefore, supplying empty credentials is acceptable for the SDK's implemented Market service methods. 1 2

Demo versus production

Demo REST requests require both a Demo API key and Bitget's paptrading: 1 header. WithDemoTrading() adds that header only when the SDK makes signed REST requests. 3 4

For WebSockets, demo trading uses different public/private endpoints and is configured with WithWSURL; see WebSocket Streaming. Do not assume that enabling the REST demo option switches WebSocket endpoints. 4 5

References

Clone this wiki locally