Code Examples

This page provides complete code examples for common WebSocket operations using the Paradex API.

Prerequisites

For Python examples, you’ll need to install the required dependencies:

$pip install websocket-client

Establishing a Connection

This example demonstrates how to establish a basic WebSocket connection:

1import websocket
2import json
3
4websocket_url = "wss://ws.api.testnet.paradex.trade/v1"
5
6# Define a callback to check connection success
7def on_open(ws):
8 print('Connected')
9
10# Connect to the WebSocket server
11ws = websocket.WebSocketApp(websocket_url, on_open=on_open)
12
13# Wait for a response
14ws.run_forever()

Authenticating a Connection

This example demonstrates how to authenticate a WebSocket connection:

1import websocket
2import json
3
4websocket_url = "wss://ws.api.testnet.paradex.trade/v1"
5
6# Define the message to send
7message = {
8 "jsonrpc": "2.0",
9 "method": "auth",
10 "params": {
11 "bearer": "JWcgwMbK0bx1uFFef0Lri35ZDwypmCG0isuBv"
12 },
13 "id": 0
14}
15
16# Define a callback to check connection success
17def on_open(ws):
18 # Send the message
19 ws.send(json.dumps(message))
20
21# Define a callback to handle the response
22def on_message(ws, message):
23 response = json.loads(message)
24 print(response)
25
26# Connect to the WebSocket server
27ws = websocket.WebSocketApp(websocket_url, on_open=on_open, on_message=on_message)
28
29# Wait for a response
30ws.run_forever()

Subscribing to a Channel

This example demonstrates how to subscribe to the trades.ETH-USD-PERP channel:

1import websocket
2import json
3
4websocket_url = "wss://ws.api.testnet.paradex.trade/v1"
5
6# Define the message to send
7auth = {
8 "jsonrpc": "2.0",
9 "method": "auth",
10 "params": {
11 "bearer": "JWcgwMbK0bx1uFFef0Lri35ZDwypmCG0isuBv"
12 },
13 "id": 0
14}
15message = {
16 "jsonrpc": "2.0",
17 "method": "subscribe",
18 "params": {
19 "channel": "trades.ETH-USD-PERP"
20 },
21 "id": 1
22}
23
24# Define a callback to check connection success
25def on_open(ws):
26 # Auth first
27 ws.send(json.dumps(auth))
28 # Send the message
29 ws.send(json.dumps(message))
30
31# Define a callback to handle the response
32def on_message(ws, message):
33 response = json.loads(message)
34 print(response)
35
36# Connect to the WebSocket server
37ws = websocket.WebSocketApp(websocket_url, on_open=on_open, on_message=on_message)
38
39# Wait for a response
40ws.run_forever()

Unsubscribing from a Channel

This example demonstrates how to unsubscribe from the trades.ETH-USD-PERP channel:

1import websocket
2import json
3
4websocket_url = "wss://ws.api.testnet.paradex.trade/v1"
5
6# Define the message to send
7auth = {
8 "jsonrpc": "2.0",
9 "method": "auth",
10 "params": {
11 "bearer": "JWcgwMbK0bx1uFFef0Lri35ZDwypmCG0isuBv"
12 },
13 "id": 0
14}
15message = {
16 "jsonrpc": "2.0",
17 "method": "unsubscribe",
18 "params": {
19 "channel": "trades.ETH-USD-PERP"
20 },
21 "id": 2
22}
23
24# Define a callback to check connection success
25def on_open(ws):
26 # Auth first
27 ws.send(json.dumps(auth))
28 # Send the message
29 ws.send(json.dumps(message))
30
31# Define a callback to handle the response
32def on_message(ws, message):
33 response = json.loads(message)
34 print(response)
35
36# Connect to the WebSocket server
37ws = websocket.WebSocketApp(websocket_url, on_open=on_open, on_message=on_message)
38
39# Wait for a response
40ws.run_forever()

Complete Example: Authentication, Subscription, and Message Handling

This example demonstrates a complete workflow including authentication, subscription, and message handling:

1import websocket
2import json
3import time
4
5# WebSocket URL
6websocket_url = "wss://ws.api.testnet.paradex.trade/v1"
7
8# Your JWT token
9jwt_token = "JWcgwMbK0bx1uFFef0Lri35ZDwypmCG0isuBv"
10
11# Message ID counter
12message_id = 0
13
14# Get next message ID
15def get_next_id():
16 global message_id
17 message_id += 1
18 return message_id
19
20# Connection opened callback
21def on_open(ws):
22 print("Connection established")
23
24 # Authenticate
25 auth_message = {
26 "jsonrpc": "2.0",
27 "method": "auth",
28 "params": {
29 "bearer": jwt_token
30 },
31 "id": get_next_id()
32 }
33 ws.send(json.dumps(auth_message))
34
35# Message received callback
36def on_message(ws, message):
37 data = json.loads(message)
38 print(f"Received: {data}")
39
40 # Check if this is an authentication response
41 if "result" in data and data.get("id") == 1:
42 print("Authentication successful, subscribing to channel")
43
44 # Subscribe to trades channel
45 subscribe_message = {
46 "jsonrpc": "2.0",
47 "method": "subscribe",
48 "params": {
49 "channel": "trades.ETH-USD-PERP"
50 },
51 "id": get_next_id()
52 }
53 ws.send(json.dumps(subscribe_message))
54
55# Error callback
56def on_error(ws, error):
57 print(f"Error: {error}")
58
59# Close callback
60def on_close(ws, close_status_code, close_msg):
61 print(f"Connection closed: {close_status_code} - {close_msg}")
62
63# Create WebSocket connection
64ws = websocket.WebSocketApp(
65 websocket_url,
66 on_open=on_open,
67 on_message=on_message,
68 on_error=on_error,
69 on_close=on_close
70)
71
72# Start WebSocket connection
73ws.run_forever()

Implementation Notes

  • Error Handling: The examples include basic error handling. In a production environment, implement more robust error handling and reconnection logic.
  • Authentication: Replace the placeholder JWT token with your actual token.
  • Ping/Pong: Most WebSocket libraries automatically handle ping/pong messages. If you’re using a library that doesn’t, you’ll need to implement this functionality manually.
  • Reconnection: Implement proper reconnection logic with exponential backoff for production use.