APIError class
All HTTP errors from the SDK are thrown asAPIError instances. Inspect the status, message, and data properties to determine what went wrong.
APIError properties
Common status codes
Raw HTTP responses
Every API-backed service method accepts an opt-in{ withRawResponse: true } option that returns the decoded value alongside the underlying HTTP status, headers, and response body. Use it when you need to inspect status codes, response headers (rate limit metadata, request IDs, content-type), or the exact bytes the API returned. The default return shape is unchanged when the option is omitted or set to false.
The raw variant still throws the same typed errors (APIError and its subclasses) on any response with status >= 400. It only exposes the raw response on success.
SdkResponse<T>
The option is available on every API-backed method (markets, portfolio, orders, AMM, API tokens, delegated orders, partner accounts, and market pages). WebSocket subscriptions are not affected.
withRetry wrapper
The SDK provides awithRetry utility function that wraps any async operation with configurable retry logic.
withRetry options
Only
APIError instances with a matching status code trigger retries. Other errors (network failures, timeouts) are thrown immediately.@retryOnErrors decorator
For class-based architectures, use the@retryOnErrors decorator to add retry logic to individual methods.
Decorator options
@retryOnErrors takes the same RetryConfigOptions as withRetry.
When
delays is omitted, the delay for each retry is calculated (in seconds) as:
exponentialBase: 2 and maxDelay: 60:
Rate limiting with OrderQueue
For high-frequency trading scenarios, implement anOrderQueue to throttle outgoing requests and avoid 429 errors:
Best practices
Always handle APIError specifically
Always handle APIError specifically
Catch
APIError separately from other errors. Network failures, JSON parse errors, and timeouts are not APIError instances and should be handled differently.Do not retry 400 or 401 errors
Do not retry 400 or 401 errors
Client errors (400, 401, 403) indicate a problem with your request or credentials. Retrying them wastes time and API quota. Only retry transient server errors (429, 5xx).
Use exponential backoff for 429 responses
Use exponential backoff for 429 responses
When rate limited, the server may include a
Retry-After header. If available, respect it. Otherwise, use exponential backoff starting at 1 second.Combine retry with OrderQueue
Combine retry with OrderQueue
For the most robust setup, use
OrderQueue to throttle request rate and withRetry or @retryOnErrors to handle transient failures:Logging
The SDK provides optional logging through a simpleILogger interface. Logging is completely opt-in with zero overhead by default.
Quick start
Log levels
Custom logger for production
Implement theILogger interface to integrate with your own logging infrastructure:
Passing logger to SDK components
All SDK components accept an optional logger:The SDK automatically redacts API keys in logs (shown as
***). Private keys are never logged.