Polymarket CLOB Python Client
A Python client library for interacting with the Polymarket Central Limit Order Book (CLOB). It provides functionalities for trading, fetching market data, and managing orders on Polymarket's platform. The library is actively developed with frequent patch and minor releases, reflecting ongoing enhancements and bug fixes.
Warnings
- breaking The RFQ quotes endpoint was split into distinct requester and quoter views. Direct calls to the old unified endpoint for RFQ quotes will break.
- breaking The logic for RFQ requester orders was changed to invert the price. This directly affects how prices are interpreted when a requester is creating an RFQ.
- gotcha The `ClobClient` requires a valid private key (for transaction signing) and an RPC URL for the Polygon network. Using an incorrect private key, an inactive key, or an invalid/unresponsive RPC URL will lead to authentication or connection errors.
- gotcha The library undergoes frequent updates and bug fixes. While beneficial, this can occasionally introduce subtle behavioral changes or new requirements for certain API calls, especially for advanced features like 'Post Only Orders' or 'HeartBeats'.
Install
-
pip install py-clob-client
Imports
- ClobClient
from py_clob_client.client import ClobClient
- POLYGON_CHAIN_ID
from py_clob_client.constants import POLYGON_CHAIN_ID
Quickstart
import os
from py_clob_client.client import ClobClient
from py_clob_client.constants import POLYGON_CHAIN_ID
from eth_account import Account
# WARNING: Store private keys securely, e.g., in environment variables.
# DO NOT hardcode private keys in production code.
pk = os.environ.get('POLYMARKET_PRIVATE_KEY', '0x123...abc') # Replace with your actual private key or fetch from env
rpc_url = os.environ.get('POLYMARKET_RPC_URL', 'https://rpc.ankr.com/polygon_mumbai') # Or your preferred RPC endpoint
if not pk.startswith('0x'):
raise ValueError("Private key must start with '0x'")
wallet = Account.from_key(pk)
clob_client = ClobClient(
wallet=wallet,
rpc_url=rpc_url,
chain_id=POLYGON_CHAIN_ID,
)
try:
markets = clob_client.get_markets()
print(f"Successfully fetched {len(markets)} markets:")
for market in markets[:2]: # Print first 2 markets for brevity
print(f" - {market.title} (ID: {market.market_id})")
except Exception as e:
print(f"Error fetching markets: {e}")
print("Please ensure your RPC URL and private key are correct and active.")