Skip to main content
Retrieve market summary statistics by market ID and return it as a JSON object.
market_id
object
required
The market identifier
returns
object
required
Dictionary containing market summary data if found, None otherwise
Raises
  • ValueError — If API_SERVER_URL environment variable is not set
Example
>>> import os
>>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
>>> summary = await getMarketSummary(market_id=1)
>>> print(summary["openInterest"])
1000.5
async def getMarketSummary(market_id: Union[str, int]) -> 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:
            market_summary = await client.get_market_summary(market_id)
            if market_summary:
                market_summary_dict = MessageToDict(market_summary, always_print_fields_with_no_presence=True)
                return market_summary_dict
            return None
        except Exception as e:
            print(f'Error getting market summary: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Is this a valid market ID in the database?')
            return None