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

# getOrderBook

> Python SDK reference for getOrderBook.

Retrieve order book by market ID and return it as a JSON object.

<ParamField path="market_id" type="object" required>
  The market identifier
</ParamField>

<ParamField path="tick_size" type="float" required>
  The tick size for price levels in the order book
</ParamField>

<ParamField path="usd_price" type="bool" required>
  Whether to size in USD or in raw format
</ParamField>

<ParamField path="levels" type="int">
  Optional number of price levels to return (default: all)
</ParamField>

<ParamField path="limit" type="int">
  Optional limit on number of orders to return (default: 50)
</ParamField>

<ParamField path="offset" type="int">
  Optional number of orders to skip for pagination (default: 0)
</ParamField>

<ResponseField name="returns" type="object" required>
  Dictionary containing order book data if found, None otherwise
</ResponseField>

**Raises**

* **ValueError** — If API\_SERVER\_URL environment variable is not set

**Example**

```python theme={null}
>>> import os
>>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
>>> order_book = await getOrderBook(market_id=1)
>>> print(len(order_book.get("longs", [])))
10
```

```python theme={null}
async def getOrderBook(market_id: Union[str, int], tick_size: float, usd_price: bool, levels: int=None, limit: int=50, offset: int=0) -> Optional[Dict[str, Any]]:
    base_url = os.getenv('API_SERVER_URL')
    if not base_url:
        raise ValueError('API_SERVER_URL environment variable not set')
    async with httpx.AsyncClient() as session:
        client = PerpetualsClient(base_url=base_url, session=session)
        try:
            order_book = await client.get_order_book(market_id, tick_size, usd_price, levels, limit, offset)
            print('raw order book', order_book)
            if order_book:
                order_book_dict = MessageToDict(order_book)
                return order_book_dict
            return None
        except Exception as e:
            print(f'Error getting order book: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Is this a valid market ID in the database?')
            return None
```
