> ## 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.

# getMarkets

> Python SDK reference for getMarkets.

Retrieve all market configurations and return them as a JSON object.

<ResponseField name="returns" type="object" required>
  Dictionary containing markets 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"
>>> markets = await getMarkets()
>>> print(len(markets.get("markets", [])))
3
```

```python theme={null}
async def getMarkets() -> 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:
            markets = await client.get_markets()
            if markets is not None:
                markets_list = [MessageToDict(market, always_print_fields_with_no_presence=True) for market in markets]
                return {'markets': markets_list}
            return None
        except Exception as e:
            print(f'Error getting markets: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Are there markets configured in the database?')
            return None
```
