Polymarket CLOB Python Client

0.34.6 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

Initializes the ClobClient with a private key (for signing transactions) and an RPC URL. It then fetches and prints the first two available markets. Remember to manage your private key securely, preferably via environment variables.

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.")

view raw JSON →