> ## Documentation Index
> Fetch the complete documentation index at: https://docs.limitless.exchange/llms.txt
> Use this file to discover all available pages before exploring further.

# Markets

> Market discovery and orderbook data with the TypeScript SDK

## MarketFetcher setup

`MarketFetcher` provides read-only access to market data. No API key is required for public endpoints.

```typescript theme={null}
import { HttpClient, MarketFetcher } from '@limitless-exchange/sdk';

const httpClient = new HttpClient({
  baseURL: 'https://api.limitless.exchange',
});

const marketFetcher = new MarketFetcher(httpClient);
```

## Active markets

Retrieve a paginated list of currently active markets with optional sorting.

```typescript theme={null}
const { data: markets, totalMarketsCount } = await marketFetcher.getActiveMarkets({
  limit: 20,
  page: 1,
  sortBy: 'newest',
});

console.log(`${totalMarketsCount} active markets`);
for (const market of markets) {
  console.log(`${market.slug} — ${market.title}`);
}
```

### Parameters

| Parameter | Type | Default | Description |
| - | - | - | - |
| `limit` | `number` | `25` | Markets per page. Maximum 25; larger values return 400. |
| `page` | `number` | `1` | Page number (1-indexed) |
| `sortBy` | `string` | (none) | Sort order. When omitted the server orders by priority, then by deadline. |

### Sort options

| Value | Description |
| - | - |
| `'lp_rewards'` | Markets with the highest LP rewards |
| `'ending_soon'` | Markets closest to resolution |
| `'newest'` | Most recently created markets |
| `'high_value'` | Markets with the highest volume or liquidity |

## Single market

Fetch a single market by slug. The response includes venue contract addresses and token IDs needed for trading.

```typescript theme={null}
const market = await marketFetcher.getMarket('btc-100k-weekly');

console.log('Slug:', market.slug);
console.log('Venue exchange:', market.venue.exchange);
console.log('YES token ID:', market.tokens.yes);
console.log('NO token ID:', market.tokens.no);
```

> [!NOTE]
>
> `getMarket()` caches the venue (signing contract addresses) of the returned market for `OrderClient`; the market itself is fetched from the API on every call. Always fetch the market before placing orders so the venue is cached.

### Market response structure

```typescript theme={null}
interface Market {
  slug: string;
  title: string;
  venue?: {
    exchange: string;        // EIP-712 verifyingContract address
    adapter: string | null;  // Adapter contract address (null for some CLOB markets)
  };
  tokens?: { yes: string; no: string }; // YES / NO token IDs (MarketTokens)
  openInterest?: string;
  liquidity?: string;
  imageUrl?: string | null;
  automationType?: 'manual' | 'lumy' | 'sports';
  // ... additional fields
}
```

## Orderbook

Fetch the current orderbook for a CLOB market. Returns bids, asks, and the current spread.

```typescript theme={null}
const orderbook = await marketFetcher.getOrderBook('btc-100k-weekly');

console.log('Best bid:', orderbook.bids[0]?.price);
console.log('Best ask:', orderbook.asks[0]?.price);

// Calculate spread
if (orderbook.bids.length > 0 && orderbook.asks.length > 0) {
  const spread = orderbook.asks[0].price - orderbook.bids[0].price;
  console.log('Spread:', spread.toFixed(4));
}
```

### Orderbook response structure

```typescript theme={null}
interface Orderbook {
  bids: OrderbookLevel[];      // YES bids, price descending
  asks: OrderbookLevel[];      // YES asks, price ascending
  tokenId: string;             // YES position token id
  adjustedMidpoint: number;    // midpoint after excluding orders below minSize
  midpoint: number;            // midpoint of the best displayed bid and ask
  maxSpread: string;           // e.g. "0.035"
  minSize: string;             // raw 6-decimal integer string, "50000000" = 50 shares
  lastTradePrice: number | null;
}

interface OrderbookLevel {
  price: number;         // 0 to 1
  size: number;          // raw 6-decimal integer, 56000000 = 56 shares
  side: 'BUY' | 'SELL';
}
```

> [!NOTE]
>
> `bids` / `asks` include levels below `minSize`; `adjustedMidpoint` excludes them, so the top level and `adjustedMidpoint` can disagree. Use `midpoint` for the displayed touch.

### Handling illiquid markets

Markets with empty or one-sided orderbooks can produce misleading midpoint prices. When there are no bids, the midpoint defaults to `(0 + bestAsk) / 2`, which may show 50% even though no one is actively trading. To detect these markets:

```typescript theme={null}
const orderbook = await marketFetcher.getOrderBook('some-market');

const hasBids = orderbook.bids.length > 0;
const hasAsks = orderbook.asks.length > 0;

if (!hasBids || !hasAsks) {
  console.log('No two-sided market — skip or flag as illiquid');
} else {
  const spread = orderbook.asks[0].price - orderbook.bids[0].price;
  if (spread > 0.20) {
    console.log('Wide spread — market may have low liquidity');
  }
}
```

## User orders

Retrieve your open orders for a specific market using the fluent API. Requires an API key.

```typescript theme={null}
const httpClient = new HttpClient({
  baseURL: 'https://api.limitless.exchange',
  hmacCredentials: {
    tokenId: process.env.LMTS_TOKEN_ID,
    secret: process.env.LMTS_TOKEN_SECRET,
  },
});

const marketFetcher = new MarketFetcher(httpClient);
const market = await marketFetcher.getMarket('btc-100k-weekly');

const orders = await market.getUserOrders();

for (const order of orders) {
  console.log(order.id, order.side, order.price, order.status);
}
```

> [!NOTE]
>
> The `getUserOrders()` method is available on the market object returned by `getMarket()`. It requires an authenticated `HttpClient` (with an API key).

## NegRisk group markets

NegRisk markets are organized into groups. Each group contains multiple submarkets (outcomes). To trade a specific outcome, fetch the group first, then access the submarket you need.

**1. Fetch the NegRisk group**

```typescript theme={null}
const group = await marketFetcher.getMarket('us-election-2024');

console.log('Group:', group.slug);
console.log('Submarkets:', group.markets.length);
```

**2. Access a submarket**

```typescript theme={null}
const submarket = group.markets[0];
console.log('Submarket:', submarket.slug, submarket.title);
```

**3. Fetch the submarket for token IDs**

You must call `getMarket()` on the submarket slug to get the venue and token IDs required for placing orders.

```typescript theme={null}
const submarketDetail = await marketFetcher.getMarket(submarket.slug);

console.log('YES token:', submarketDetail.tokens.yes);
console.log('NO token:', submarketDetail.tokens.no);
```

> [!WARNING]
>
> Always use the **submarket slug** (not the group slug) when placing orders on NegRisk markets. The group slug does not have token IDs associated with it.

## Best practices

> [accordion] Cache venue data
>
> Always call `getMarket()` before placing orders. The SDK caches venue data (exchange and adapter addresses) internally. This avoids redundant API calls and ensures the `OrderClient` has the information it needs to sign orders.

> [accordion] Use WebSocket instead of polling
>
> For real-time orderbook data, subscribe to WebSocket events rather than polling `getOrderBook()`. The WebSocket pushes `orderbookUpdate` events whenever the book changes, reducing latency and API usage. See the [WebSocket Streaming](https://docs.limitless.exchange/developers/sdk/typescript/websocket.md) guide.

> [accordion] Paginate large result sets
>
> When fetching active markets, always use `limit` and `page` parameters. Requesting unbounded result sets can cause timeouts and high memory usage.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.