Documentation
¶
Overview ¶
Package bitget is an idiomatic Go SDK for the Bitget Unified Trading Account (UTA) API v3.
Index ¶
Constants ¶
const ( DefaultPublicWSURL = "wss://ws.bitget.com/v3/ws/public" DefaultPrivateWSURL = "wss://ws.bitget.com/v3/ws/private" DemoPublicWSURL = "wss://wspap.bitget.com/v3/ws/public" DemoPrivateWSURL = "wss://wspap.bitget.com/v3/ws/private" )
Public/private WebSocket endpoints.
Docs: https://www.bitget.com/api-doc/uta/guide
const DefaultBaseURL = "https://api.bitget.com"
DefaultBaseURL is Bitget's production UTA v3 REST endpoint.
Docs: https://www.bitget.com/api-doc/uta/guide
const DefaultTimeout = 15 * time.Second
DefaultTimeout is applied to the internal *http.Client when none is supplied via WithHTTPClient.
const MaxRESTReceiptBytes = maxResponseBodySize
MaxRESTReceiptBytes is the maximum number of response-body bytes retained in a RESTReceipt. An oversized response fails with an incomplete bounded receipt.
Variables ¶
var ( ErrInvalidSignature = errors.New("bitget: invalid signature") ErrInvalidTimestamp = errors.New("bitget: invalid or expired timestamp") ErrPermissionDenied = errors.New("bitget: permission denied for this API key") ErrRateLimited = errors.New("bitget: rate limit exceeded") ErrInvalidParameter = errors.New("bitget: invalid request parameter") ErrInsufficientFunds = errors.New("bitget: insufficient balance") ErrOrderNotFound = errors.New("bitget: order not found") ErrInternalServer = errors.New("bitget: internal server error") )
Sentinel errors that callers can match with errors.Is, wrapped alongside the detailed *Error (retrievable via errors.As) on every failed call.
Functions ¶
func MapErrorCode ¶
MapErrorCode maps a Bitget API response "code" to a sentinel error so callers can use errors.Is without parsing raw codes themselves. Unknown codes return nil (caller should fall back to the raw *Error).
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is the low-level authenticated HTTP transport shared by every service under RestClient. Most callers should construct a *RestClient via NewRestClient instead of using Client directly.
func NewClient ¶
NewClient creates a low-level Client. apiKey/secretKey/passphrase are the credentials generated for an API key in the Bitget web console (https://www.bitget.com/api-doc/uta/guide). Most callers want NewRestClient instead, which additionally wires up all API services.
type Error ¶ added in v1.0.4
Error represents a structured error returned by the Bitget UTA API, preserving the exact code/message pair sent back in the response envelope.
type Logger ¶
type Logger interface {
Debug(msg string, args ...any)
Info(msg string, args ...any)
Warn(msg string, args ...any)
Error(msg string, args ...any)
}
Logger is a minimal structured-logging interface. Pass an adapter (see NewSlogLogger) or your own implementation via WithLogger; the default is a no-op logger.
func NewSlogLogger ¶
NewSlogLogger wraps l so it can be passed to WithLogger. A nil logger uses the same no-op behavior as a client without an explicitly configured logger.
type Option ¶
type Option func(*Client)
Option configures a Client at construction time.
func WithBaseURL ¶
WithBaseURL overrides the REST base URL, e.g. for Bitget's VIP endpoint or a test server. Pass an origin without a trailing API path: SDK methods add their own /api/v3/... path.
func WithDemoTrading ¶
func WithDemoTrading() Option
WithDemoTrading enables Bitget's simulated-trading mode by sending the "paptrading: 1" header on every request. Use together with a Demo API key; see https://www.bitget.com/api-doc/uta/guide.
func WithHTTPClient ¶
WithHTTPClient injects a custom *http.Client (for proxies, custom transports, or test doubles). The client is used as-is; DefaultTimeout is not applied when this option is used.
func WithLocale ¶
WithLocale sets the "locale" header sent on every request (e.g. "en-US", "zh-CN"). Defaults to "en-US".
func WithLogger ¶
WithLogger sets a structured logger for request/response diagnostics. Never logs ACCESS-KEY, ACCESS-SIGN, or ACCESS-PASSPHRASE values.
func WithTimeout ¶
WithTimeout sets the timeout of the internally constructed *http.Client. Ignored if WithHTTPClient is also used.
type RawFrame ¶ added in v1.1.0
RawFrame is an exact WebSocket message received from Bitget before the SDK decodes it. Payload is a private copy and must be treated as read-only. Generation increases after every successful connection; ReceivedAt is taken immediately after the network read completes.
type RawFrameHandler ¶ added in v1.1.0
RawFrameHandler observes every received WebSocket message synchronously. It is the lossless canonical path for consumers that need exact wire data. Keep it short or apply backpressure deliberately. Returning an error emits a terminal lifecycle event and disconnects the current connection.
type RestClient ¶
type RestClient struct {
*Client
// Market provides public, unauthenticated market-data endpoints.
Market *market.Client
// Account provides private account/balance/leverage endpoints.
Account *account.Client
// Trade provides private order-placement and position endpoints.
Trade *trade.Client
}
RestClient is the main entry point for the REST API, grouping every service under one struct. Construct it with NewRestClient.
func NewRestClient ¶
func NewRestClient(apiKey, secretKey, passphrase string, opts ...Option) *RestClient
NewRestClient creates a fully wired Bitget UTA v3 REST client. apiKey, secretKey, and passphrase come from the API key created in the Bitget web console; see https://www.bitget.com/api-doc/uta/guide.
Public endpoints (RestClient.Market) work even with empty credentials.
type WSClient ¶
type WSClient struct {
// contains filtered or unexported fields
}
WSClient is a reconnecting WebSocket client for Bitget's public or private UTA v3 streams. Create one with NewPublicWSClient or NewPrivateWSClient.
Docs: https://www.bitget.com/api-doc/uta/websocket/private/Fast-Fill-Channel
func NewPrivateWSClient ¶
NewPrivateWSClient creates a client for Bitget's private WebSocket streams (order fills, positions, etc.), authenticating automatically on Connect.
func NewPublicWSClient ¶
NewPublicWSClient creates a client for Bitget's public market-data WebSocket streams (no authentication).
func (*WSClient) Close ¶
Close terminates the connection and stops all background loops. Safe to call multiple times.
func (*WSClient) Connect ¶
Connect dials the WebSocket endpoint, logs in (for private clients), and starts the background read/ping/reconnect loops. Connect blocks until the initial connection (and, for private clients, login) succeeds or ctx is done.
type WSEventHandler ¶ added in v1.1.0
type WSEventHandler func(WSLifecycleEvent)
WSEventHandler handles lifecycle events synchronously. It must return promptly and must not panic; the SDK does not buffer or drop these events.
type WSLifecycleEvent ¶ added in v1.1.0
type WSLifecycleEvent struct {
Type WSLifecycleType
Arg *models.WSArg
Code string
Message string
Err error
ReceivedAt time.Time
Generation uint64
}
WSLifecycleEvent reports the observable control-plane lifecycle. Arg is copied from Bitget's control event, allowing callers to correlate an ACK or provider error to a subscription. Err is non-nil for local terminal errors.
type WSLifecycleType ¶ added in v1.1.0
type WSLifecycleType string
WSLifecycleType identifies a WebSocket connection or subscription event.
const ( WSConnected WSLifecycleType = "connected" WSDisconnected WSLifecycleType = "disconnected" WSClosed WSLifecycleType = "closed" WSAuthenticated WSLifecycleType = "authenticated" WSSubscriptionRequested WSLifecycleType = "subscription_requested" WSSubscribed WSLifecycleType = "subscribed" WSUnsubscriptionRequested WSLifecycleType = "unsubscription_requested" WSUnsubscribed WSLifecycleType = "unsubscribed" WSProviderError WSLifecycleType = "provider_error" WSTerminal WSLifecycleType = "terminal" )
WSLifecycle event types are delivered to a WSEventHandler.
type WSOption ¶
type WSOption func(*WSClient)
WSOption configures a WSClient at construction time.
func WithRawFrameHandler ¶ added in v1.1.0
func WithRawFrameHandler(handler RawFrameHandler) WSOption
WithRawFrameHandler installs the canonical, synchronous raw-frame observer. It receives a full immutable copy before JSON decoding and before the bounded WSPush subscriber buffers are considered.
func WithWSAutoReconnect ¶
WithWSAutoReconnect toggles automatic reconnection with exponential backoff on unexpected disconnects. Enabled by default.
func WithWSEventHandler ¶ added in v1.1.0
func WithWSEventHandler(handler WSEventHandler) WSOption
WithWSEventHandler installs a synchronous observer for connection and subscription lifecycle events, including subscribe ACKs and provider errors.
func WithWSLogger ¶
WithWSLogger sets a structured logger for connection lifecycle events.
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
demo-account
command
Command demo_account reads account data with a Demo API key.
|
Command demo_account reads account data with a Demo API key. |
|
history
command
Command history fetches one public deep-history candle page and reports its bounded receipt.
|
Command history fetches one public deep-history candle page and reports its bounded receipt. |
|
rest
command
Command rest_example demonstrates public market data.
|
Command rest_example demonstrates public market data. |
|
websocket
command
Command websocket_example demonstrates connecting to Bitget's public WebSocket, subscribing to a channel, and handling reconnects.
|
Command websocket_example demonstrates connecting to Bitget's public WebSocket, subscribing to a channel, and handling reconnects. |
|
websocket-private
command
Command websocket_private demonstrates a Demo private WebSocket subscription.
|
Command websocket_private demonstrates a Demo private WebSocket subscription. |
|
Package models contains typed request/response structures for the Bitget UTA v3 API.
|
Package models contains typed request/response structures for the Bitget UTA v3 API. |
|
rest
|
|
|
account
Package account implements Bitget UTA v3's private account endpoints.
|
Package account implements Bitget UTA v3's private account endpoints. |
|
market
Package market implements Bitget UTA v3's public market-data endpoints.
|
Package market implements Bitget UTA v3's public market-data endpoints. |
|
trade
Package trade implements Bitget UTA v3's private order and position endpoints.
|
Package trade implements Bitget UTA v3's private order and position endpoints. |
