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) for the wire format, and for generating a decoder in another language.
Prerequisites
Establishing a Connection
Passing sbe_enabled=True appends the schema parameters to the connection URL. Omitting it falls back to JSON, which is deprecated for public channels.
Subscribing to a Channel
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:
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
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.