Skip to main content
Retrieve complete portfolio for the authenticated caller.
returns
object
required
Dictionary containing portfolio data if found, None otherwise Portfolio includes:
  • Aggregate metrics (margin, PnL, equity, volume, fees, etc.)
  • All positions (open and closed)
  • All orders (filled, open, cancelled)
Raises
  • ValueError — If API_SERVER_URL or ACCOUNT environment variable is not set
Example
>>> import os
>>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
>>> os.environ["ACCOUNT"] = "0x1234..."
>>> portfolio = await getPortfolio()
>>> print(portfolio["totalEquity"])
15000.50
>>> print(len(portfolio["positions"]))
3
async def getPortfolio() -> Optional[Dict[str, Any]]:
    base_url = os.getenv('API_SERVER_URL')
    if not base_url:
        raise ValueError('API_SERVER_URL environment variable not set')
    address = os.getenv('ACCOUNT')
    if not address:
        raise ValueError('ACCOUNT environment variable not set')
    async with httpx.AsyncClient() as session:
        client = PerpetualsClient(base_url=base_url, session=session)
        try:
            portfolio = await client.get_portfolio(address)
            if portfolio:
                portfolio_dict = MessageToDict(portfolio, always_print_fields_with_no_presence=True)
                return portfolio_dict
            return None
        except Exception as e:
            print(f'Error getting portfolio: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Is ACCOUNT environment variable set correctly?')
            print('  - Check network connectivity')
            return None