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.APIErrorswitch {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)}Sentinels
Section titled “Sentinels”| 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.
What is retried
Section titled “What is retried”| 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))Blocked requests
Section titled “Blocked requests”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.APIErrorif 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.
Logging requests
Section titled “Logging requests”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 msstx.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.

