Low-level REST API client for the O2 Exchange.

Provides typed methods for every REST endpoint. Automatically retries on rate limit errors with exponential backoff and jitter.

import { O2Api, TESTNET } from "@o2exchange/sdk";

const api = new O2Api({ config: TESTNET });
const markets = await api.getMarkets();
console.log(markets.markets.length);

Constructors

Properties

VALID_RESOLUTIONS: Set<string> = ...

Valid bar resolutions accepted by the API.

Methods

  • Fetch account information by owner address, contract, or trade account ID.

    Parameters

    • params: { owner?: string; ownerContract?: string; tradeAccountId?: TradeAccountId }

      Lookup parameters (provide one of owner, ownerContract, or tradeAccountId).

    Returns Promise<AccountInfo>

  • Fetch aggregated order book (CoinGecko-compatible).

    Parameters

    • marketPair: string

      The market pair (e.g., "fFUEL_fUSDC").

    • depth: number = 500

      Number of levels (default: 500).

    • level: number = 2

      Aggregation level (default: 2).

    Returns Promise<AggregatedOrderbook>

  • Fetch aggregated trades for a pair (CoinGecko-compatible).

    Parameters

    • marketPair: string

      The market pair (e.g., "fFUEL_fUSDC").

    Returns Promise<Trade[]>

  • Fetch OHLCV candlestick bars.

    Parameters

    • marketId: MarketId

      The market identifier.

    • from: number

      Start time in milliseconds (not seconds).

    • to: number

      End time in milliseconds (not seconds).

    • resolution: string

      Bar resolution. Valid values: 1s, 1m, 2m, 3m, 5m, 15m, 30m, 1h, 2h, 4h, 6h, 8h, 12h, 1d, 3d, 1w, 1M, 3M.

    Returns Promise<Bar[]>

    If resolution is not valid.

  • Fetch the order book depth snapshot.

    Parameters

    • marketId: MarketId

      The market identifier.

    • precision: number = 10

      Price aggregation precision (default: 10).

    • Optionallimit: number

      Maximum number of price levels per side. undefined returns the full book.

    Returns Promise<DepthSnapshot>

  • Fetch orders for an account on a specific market.

    Parameters

    • marketId: MarketId

      The market identifier.

    • contract: TradeAccountId

      The trade account contract ID.

    • direction: "desc" | "asc" = "desc"

      Sort direction (default: "desc").

    • count: number = 20

      Number of orders to return (default: 20).

    • OptionalisOpen: boolean

      Filter by open/closed status.

    • OptionalstartTimestamp: number
    • OptionalstartOrderId: OrderId

    Returns Promise<OrdersResponse>

  • Fetch recent trades for a market.

    Parameters

    • marketId: MarketId

      The market identifier.

    • direction: "desc" | "asc" = "desc"

      Sort direction (default: "desc").

    • count: number = 50

      Number of trades to return (default: 50).

    • OptionalstartTimestamp: number

      Optional starting timestamp for pagination.

    • OptionalstartTradeId: string

      Optional starting trade ID for pagination.

    • Optionalcontract: string

    Returns Promise<Trade[]>

  • Fetch trades for a specific account.

    Parameters

    • marketId: MarketId

      The market identifier.

    • contract: TradeAccountId

      The trade account contract ID.

    • direction: "desc" | "asc" = "desc"

      Sort direction (default: "desc").

    • count: number = 50

      Number of trades to return (default: 50).

    • OptionalstartTimestamp: number
    • OptionalstartTradeId: string

    Returns Promise<Trade[]>

  • Mint test tokens to an address (testnet/devnet only).

    Parameters

    • address: string

      The destination address.

    Returns Promise<FaucetResponse>

    O2Error if faucet is not available on this network.

  • Submit session actions (create/cancel orders, settle balances).

    Parameters

    • ownerId: string

      The owner's b256 address.

    • request: SessionActionsRequest

      The signed session actions request.

      Preflight validation errors (e.g., invalid nonce, insufficient balance) are returned as a SessionActionsResponse with isPreflightError === true rather than thrown.

    Returns Promise<SessionActionsResponse>