Skip to main content

APIError class

All HTTP errors from the SDK are thrown as APIError 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 a withRetry 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:
For example, with exponentialBase: 2 and maxDelay: 60:

Rate limiting with OrderQueue

For high-frequency trading scenarios, implement an OrderQueue to throttle outgoing requests and avoid 429 errors:

Best practices

Catch APIError separately from other errors. Network failures, JSON parse errors, and timeouts are not APIError instances and should be handled differently.
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).
When rate limited, the server may include a Retry-After header. If available, respect it. Otherwise, use exponential backoff starting at 1 second.
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 simple ILogger interface. Logging is completely opt-in with zero overhead by default.

Quick start

Log levels

Custom logger for production

Implement the ILogger 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.