> For clean Markdown of any page, append .md to the page URL. > For a complete documentation index, see https://docs.paradex.trade/ws/general-information/code-examples/llms.txt. > For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.paradex.trade/_mcp/server. # Code Examples This page provides complete code examples for common WebSocket operations using the Paradex API. Public market data channels are delivered as SBE binary frames. The examples below use the Paradex Python SDK, which negotiates the encoding and decodes frames for you. See [Binary Encoding (SBE)](/ws/general-information/binary-encoding-sbe) for the wire format, and for generating a decoder in another language. ## Prerequisites ```bash pip install "paradex-py>=0.7.0" ``` ## Establishing a Connection ```python import asyncio from paradex_py.api.ws_client import ParadexWebsocketClient from paradex_py.environment import TESTNET async def main(): ws_client = ParadexWebsocketClient(env=TESTNET, sbe_enabled=True) await ws_client.connect() print("Connected") asyncio.run(main()) ``` Passing `sbe_enabled=True` appends the schema parameters to the connection URL. Omitting it is not supported for public channels; as of 21 September 2026 SBE is required. ## Subscribing to a Channel ```python import asyncio from paradex_py.api.ws_client import ParadexWebsocketChannel, ParadexWebsocketClient from paradex_py.environment import TESTNET async def on_message(channel, message): print(channel, message["params"]["data"]) async def main(): ws_client = ParadexWebsocketClient(env=TESTNET, sbe_enabled=True) await ws_client.connect() await ws_client.subscribe( ParadexWebsocketChannel.TRADES, callback=on_message, params={"market": "ETH-USD-PERP"}, ) await asyncio.sleep(30) asyncio.run(main()) ``` Frames are decoded before your callback runs, so the payload arrives as a plain dictionary. Subscribe requests and their acknowledgements remain JSON-RPC text frames; only the channel payload is binary. ## Authenticating a Connection Private channels require authentication. Build the client with SBE enabled, then attach the account: ```python import asyncio from paradex_py import Paradex from paradex_py.api.ws_client import ParadexWebsocketChannel, ParadexWebsocketClient from paradex_py.environment import TESTNET async def on_message(channel, message): print(channel, message["params"]["data"]) async def main(): paradex = Paradex( env=TESTNET, l1_address="0xYOUR_L1_ADDRESS", l2_private_key="0xYOUR_L2_PRIVATE_KEY", ) ws_client = ParadexWebsocketClient(env=TESTNET, sbe_enabled=True) ws_client.init_account(paradex.account) await ws_client.connect() await ws_client.subscribe( ParadexWebsocketChannel.FILLS, callback=on_message, params={"market": "ETH-USD-PERP"}, ) await asyncio.sleep(30) asyncio.run(main()) ``` Private channels have no SBE encoder, so their payloads are JSON either way. `sbe_enabled=True` matters here only for the public channels the same connection subscribes to. ## Unsubscribing from a Channel ```python await ws_client.unsubscribe_by_name("trades.ETH-USD-PERP") ``` ## Implementation Notes * **Encoding**: Public channels are SBE binary. Use `sbe_enabled=True`, or generate a decoder from the schema if you are not using the Python SDK. * **Error Handling**: These examples include no error handling. In production, handle decode errors and connection loss explicitly. * **Authentication**: Never commit private keys. Load them from the environment or a secret store. * **Reconnection**: The SDK reconnects automatically. If you implement your own client, use exponential backoff and re-subscribe after reconnecting.