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

# Fetch Orderbook Snapshot

> Read the full Kalshi orderbook for a market by ticker or mint

<Info>
  During development, you can use the [developer endpoints](/get-started/endpoints) without an API key. For production use, [request an API key](/get-started/api-key) for higher rate limits.
</Info>

<Note>
  Response is relayed verbatim from Kalshi. Each side is a `{price: size}` object: `yes_bids` are bids on the YES outcome, `no_bids` are bids on the NO outcome. To buy YES, walk `no_bids` from highest price down (each `no_bids` entry at price `p` implies a YES seller at `1 - p`).
</Note>

<Note>
  Every block below is an independent example. Each one redeclares the env consts and the `headers` object so you can copy and run any one of them on its own. Many markets return `404` when they have no active orderbook (finalized, illiquid, or off-hours).
</Note>

## By market ticker

<AccordionGroup>
  <Accordion title="Fetch orderbook by ticker">
    ```typescript theme={null}
    import "dotenv/config";

    const DFLOW_METADATA_API_URL = process.env.DFLOW_METADATA_API_URL ?? "https://dev-prediction-markets-api.dflow.net";
    const DFLOW_API_KEY = process.env.DFLOW_API_KEY; // optional; not needed for dev endpoints

    const headers: HeadersInit = {};
    if (DFLOW_API_KEY) headers["x-api-key"] = DFLOW_API_KEY;

    const ticker = "MARKET_TICKER_HERE";

    const book = await fetch(
      `${DFLOW_METADATA_API_URL}/api/v1/orderbook/${encodeURIComponent(ticker)}`,
      { headers }
    ).then((r) => r.json());

    if (!book.yes_bids) {
      throw new Error(`No orderbook for "${ticker}". Pick an active market. Response: ${JSON.stringify(book)}`);
    }

    console.log(`yes_bids: ${Object.keys(book.yes_bids).length} levels`);
    console.log(`no_bids:  ${Object.keys(book.no_bids).length} levels`);
    console.log(JSON.stringify(book, null, 2));
    ```
  </Accordion>
</AccordionGroup>

## By mint address

<AccordionGroup>
  <Accordion title="Fetch orderbook by mint">
    ```typescript theme={null}
    import "dotenv/config";

    const DFLOW_METADATA_API_URL = process.env.DFLOW_METADATA_API_URL ?? "https://dev-prediction-markets-api.dflow.net";
    const DFLOW_API_KEY = process.env.DFLOW_API_KEY; // optional; not needed for dev endpoints

    const headers: HeadersInit = {};
    if (DFLOW_API_KEY) headers["x-api-key"] = DFLOW_API_KEY;

    const mintAddress = "OUTCOME_OR_LEDGER_MINT_ADDRESS";

    const book = await fetch(
      `${DFLOW_METADATA_API_URL}/api/v1/orderbook/by-mint/${mintAddress}`,
      { headers }
    ).then((r) => r.json());

    if (!book.yes_bids) {
      throw new Error(`No orderbook for mint "${mintAddress}". Pick a mint from an active market. Response: ${JSON.stringify(book)}`);
    }

    console.log(JSON.stringify(book, null, 2));
    ```
  </Accordion>
</AccordionGroup>

## Estimate fill cost

Walk the best bids on the NO side to estimate how much it costs to buy a target number of YES contracts. Each entry `no_bids[p]` implies a YES seller at price `1 - p`, so the highest NO bid is the lowest YES ask.

<AccordionGroup>
  <Accordion title="Estimate average fill price">
    ```typescript theme={null}
    import "dotenv/config";

    const DFLOW_METADATA_API_URL = process.env.DFLOW_METADATA_API_URL ?? "https://dev-prediction-markets-api.dflow.net";
    const DFLOW_API_KEY = process.env.DFLOW_API_KEY; // optional; not needed for dev endpoints

    const headers: HeadersInit = {};
    if (DFLOW_API_KEY) headers["x-api-key"] = DFLOW_API_KEY;

    const ticker = "MARKET_TICKER_HERE";
    const TARGET_CONTRACTS = 50;

    const book = await fetch(
      `${DFLOW_METADATA_API_URL}/api/v1/orderbook/${encodeURIComponent(ticker)}`,
      { headers }
    ).then((r) => r.json());

    if (!book.no_bids) {
      throw new Error(`No orderbook for "${ticker}". Pick an active market. Response: ${JSON.stringify(book)}`);
    }

    // no_bids is { priceString: sizeString }. Sort by price descending so the
    // highest NO bid (= lowest implied YES ask) is walked first.
    const levels = Object.entries(book.no_bids)
      .map(([price, size]) => [parseFloat(price), parseFloat(size as string)] as [number, number])
      .sort((a, b) => b[0] - a[0]);

    let remaining = TARGET_CONTRACTS;
    let cost = 0; // in dollars

    for (const [noPrice, size] of levels) {
      const yesAskPrice = 1 - noPrice;
      const take = Math.min(remaining, size);
      cost += take * yesAskPrice;
      remaining -= take;
      if (remaining <= 0) break;
    }

    if (remaining > 0) {
      console.log(`Insufficient depth: short by ${remaining} contracts`);
    } else {
      const avg = cost / TARGET_CONTRACTS;
      console.log(`Avg fill for ${TARGET_CONTRACTS} YES contracts: $${avg.toFixed(4)} (${(avg * 100).toFixed(2)}¢) per contract, $${cost.toFixed(2)} total`);
    }
    ```
  </Accordion>
</AccordionGroup>

## Related Resources

<CardGroup cols={2}>
  <Card title="Buy Outcome Tokens" href="/prediction-markets/recipes/buy-outcome-tokens" icon="wallet">
    Open a prediction market position from any input token.
  </Card>

  <Card title="Find Markets" href="/prediction-markets/recipes/find-markets" icon="search">
    Discover markets by category, tag, status, or series.
  </Card>

  <Card title="Prediction Market Data Model" href="/prediction-markets/prediction-market-data-model" icon="diagram-project">
    Events, markets, outcomes, mints, and how they relate.
  </Card>
</CardGroup>

## API Routes

* [`GET /api/v1/orderbook/{market_ticker}`](/resources/metadata-api/orderbook/orderbook-by-ticker)
* [`GET /api/v1/orderbook/by-mint/{mint_address}`](/resources/metadata-api/orderbook/orderbook-by-mint)
