Skip to content

Errors & retries

Describes github.com/stxapp/stx-go v0.3.1, the current release.

What each endpoint can return is in the API reference, and request limits are in Rate limits. Every call returns an error; nothing panics. An error answered by the API is a *stx.APIError, and it matches a sentinel for its status with errors.Is, so you can check for the case you care about:

order, err := client.Orders.Get(ctx, orderID)
var apiErr *stx.APIError
switch {
case err == nil:
fmt.Println(order.Status)
case errors.Is(err, stx.ErrNotFound):
fmt.Println("no such order")
case errors.Is(err, stx.ErrRateLimited) && errors.As(err, &apiErr):
fmt.Println("slow down, retry after", apiErr.RetryAfter)
default:
log.Fatal(err)
}
Sentinel Returned for
stx.ErrBadRequest 400
stx.ErrUnauthorized 401
stx.ErrInvalidToken 401 on an OAuth request whose token is missing, expired or revoked (also matches ErrUnauthorized)
stx.ErrForbidden 403
stx.ErrInsufficientScope 403 on an OAuth token without the scope the call needs (APIError.RequiredScopes)
stx.ErrIPNotAllowed 403 ip_not_allowed: a write from an address the app’s IP allow-list lacks
stx.ErrBlocked 403 from the firewall in front of the API (APIError.Code, APIError.Reference); see Blocked requests
stx.ErrRestrictedJurisdiction 403 restricted_jurisdiction: a location the exchange does not serve (also matches ErrBlocked)
stx.ErrUnusualNetwork 403 unusual_network: a network the exchange does not accept, such as an anonymizing proxy (also matches ErrBlocked)
stx.ErrTermsRequired 403 terms_required: the member has new STX terms to accept (APIError.TermsURL)
stx.ErrNotFound 404
stx.ErrRejected 422
stx.ErrFillModeNotAllowed 422: the market does not take the order’s fill mode (also matches ErrRejected); see Post-only and fill-or-kill
stx.ErrPostOnlyWouldMatch a post-only order that would have matched (a *stx.PostOnlyWouldMatchError with Reason and Order)
stx.ErrRateLimited 429 (APIError.RetryAfter, and APIError.Reference when the firewall answered)
stx.ErrServer 5xx
stx.ErrTransport no response: connection, DNS, TLS or timeout (a *stx.TransportError that wraps the network error)
stx.ErrConfig a client misconfiguration, such as a missing key or an unknown environment
stx.ErrNotAvailable a feature this exchange does not have yet (a *stx.NotAvailableError with Feature and Host); see What you can do

The 403 refinements also match stx.ErrForbidden. *stx.APIError carries StatusCode, Code, Message (the API’s error text), RequestID, Method, Path, Body, and Reference on a blocked request. WebSocket and combo errors are on WebSockets and Combos.

A canceled or expired ctx stops the call and any retries still to come.

Failure GET, DELETE POST, PATCH
429 retried after Retry-After retried after Retry-After
5xx retried not retried
No response retried not retried
403, including a blocked request not retried not retried
Any other 4xx not retried not retried

A 429 is retried after the server’s Retry-After, in full; when that is longer than MaxBackoff (30 s by default), the call is not retried and the error, with APIError.RetryAfter, reaches you. A failed POST may still have placed the order, so it is never resent; see Avoid duplicate orders. Each attempt is signed again. The default is 3 attempts with exponential backoff from 500 ms, capped at 30 s, with jitter. stx.WithTimeout sets the limit for each attempt (default 30 s).

patient, err := stx.New(
stx.WithRetry(stx.RetryPolicy{MaxAttempts: 5, InitialBackoff: time.Second, MaxBackoff: 30 * time.Second}),
stx.WithTimeout(10*time.Second),
)
noRetries, err := stx.New(stx.WithRetry(stx.NoRetry))

A firewall in front of the API can refuse a request before it reaches the exchange. It answers with an error and a message, which the SDK puts in APIError.Code and APIError.Message, and APIError.Reference is the request’s id from the x-amz-cf-id response header:

error Status Sentinel
restricted_jurisdiction 403 stx.ErrRestrictedJurisdiction
unusual_network 403 stx.ErrUnusualNetwork
rate_limited 429 stx.ErrRateLimited, with RetryAfter (60 seconds)
_, err := client.Account.Balance(ctx)
var apiErr *stx.APIError
if errors.Is(err, stx.ErrBlocked) && errors.As(err, &apiErr) {
fmt.Println(apiErr.Code, apiErr.Message, apiErr.Reference)
// restricted_jurisdiction STX isn't available in your country. Quote the reference when you contact STX support. Yq3vD8KXhZ2mN0pLr6sT4uWb9eFgHjKl1cVxQaZs-_d0EnRoPiUyTw==
}

Quote the Reference when you contact Support. A blocked request is never retried. A 403 with no JSON body at all also matches stx.ErrBlocked, with Code blocked: the API refuses requests without a User-Agent header. The client always sends one (client.UserAgent(), on REST and on the WebSocket handshake), so check that no proxy or custom http.RoundTripper removes it.

stx.WithResponseHook is called after every attempt with the operation id, method, path, status, attempt number, duration and bodies. stx.WithLogger takes a *slog.Logger for the client’s own diagnostics, such as retries and socket reconnects.

client, err := stx.New(
stx.WithResponseHook(func(e stx.ResponseEvent) {
fmt.Println(e.Method, e.Path, e.StatusCode, e.Duration.Milliseconds(), "ms")
}),
stx.WithLogger(slog.Default()),
)
if err != nil {
log.Fatal(err)
}
if _, err := client.Account.Me(ctx); err != nil {
log.Fatal(err)
}
// GET /api/v1/me 200 84 ms

stx.WithHTTPClient takes your own *http.Client, for a proxy or custom transport, and stx.WithUserAgent("my-app/1.2") adds your product to the User-Agent header.

STX is operated by XV Exchange, LLC, a Designated Contract Market regulated by the U.S. Commodity Futures Trading Commission (CFTC). Contracts traded on STX are cleared by XV Clearing, LLC, a CFTC-registered Derivatives Clearing Organization. Trading involves substantial risk of loss and may not be appropriate for everyone. Members risk losing the cost to enter any transaction, including fees. Consider whether trading is appropriate for you in light of your experience and financial resources. Nothing on this site is investment advice or a recommendation to trade. Past performance is not indicative of future results.

© 2026 STX. All rights reserved.llms.txtllms-full.txtView as Markdown
Docs version v1.10.5