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

# getOpenPositions

> Python SDK reference for getOpenPositions.

Retrieve a list of open positions for the authenticated caller.

<ParamField path="market_id" type="object">
  Optional market ID to filter positions. If None, returns all positions.
</ParamField>

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

**Raises**

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

**Example**

```python theme={null}
>>> import os
>>> os.environ["API_SERVER_URL"] = "http://localhost:8080"
>>> os.environ["ACCOUNT"] = "0x1234..."
>>> # Get all positions
>>> positions = await getOpenPositions()
>>> print(len(positions.get("positions", [])))
2
>>> # Get positions for specific market
>>> positions = await getOpenPositions(market_id=10)
>>> print(len(positions.get("positions", [])))
1
```

```python theme={null}
async def getOpenPositions(market_id: Optional[Union[str, int]]=None) -> 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:
            positions = await client.get_open_positions(address)
            if positions is not None:
                positions_list = [MessageToDict(position, always_print_fields_with_no_presence=True, preserving_proto_field_name=False) for position in positions]
                if market_id is not None:
                    for p in positions_list:
                        print(p.get('marketId'))
                    positions_list = [p for p in positions_list if int(p.get('marketId')) == market_id]
                    for position in positions_list:
                        if 'associatedOrders' in position:
                            position['associatedOrders'] = [order for order in position['associatedOrders'] if int(order.get('marketId')) == market_id]
                else:
                    positions_list.sort(key=lambda p: p.get('marketId', 0))
                return {'positions': positions_list}
            return None
        except Exception as e:
            print(f'Error getting open positions: {e}')
            print('\nTroubleshooting:')
            print('  - Is the server running?')
            print('  - Is ACCOUNT environment variable set correctly?')
            return None
```
