Quickstart Client

import asyncio, json, websockets, os

WS_URL = "wss://ws.pfex.io/v4/ws"
API_KEY = os.getenv("QFEX_API_KEY")


async def main():
    async with websockets.connect(WS_URL, ping_interval=20) as ws:  # connected msg
        print(json.loads(await ws.recv()))

        # subscribe to public BBO
        await ws.send(
            json.dumps({"type": "subscribe", "channel": "v4_bbo", "id": "bbo"})
        )

        # subscribe to your balances
        await ws.send(
            json.dumps({"type": "subscribe", "channel": "v4_balances", "id": API_KEY})
        )

        while True:
            msg = json.loads(await ws.recv())
            if msg["channel"] == "v4_bbo":
                print("BBO", msg["contents"])
            elif msg["channel"] == "v4_balances":
                print("Balance", msg["contents"])


asyncio.run(main())


Error Codes & Reconnects

ScenarioErrorAction
Invalid API keyError in order stream: UNAUTHENTICATEDVerify key in Settings › API keys
Socket closedRuntimeError: WebSocket is closedReconnect & re‑subscribe (server stateless)
Snapshot timeouttype: "error" envelopeRetry with back‑off; check symbol spelling