Polymarket API Python Guide: Build Your Own Trading Bot
A comprehensive technical guide to the Polymarket CLOB API using Python. Learn to fetch market data, place orders, and build automated trading systems.
Polymarket provides a robust API that enables programmatic trading. The official Python client, py-clob-client, makes it easy to integrate Polymarket into your trading systems.
Installation
Basic Setup
Initialize the client with your wallet's private key. For read-only operations, you can skip authentication.
Ready to Start Trading?
PredictEngine lets you create automated trading bots for Polymarket in seconds. No coding required.
Get Started FreeRead-Only Client
from py_clob_client.client import ClobClient
# Read-only client - no auth needed
client = ClobClient("https://clob.polymarket.com")
# Fetch markets
markets = client.get_simplified_markets()
print(markets)Trading Client (with auth)
from py_clob_client.client import ClobClient
# Trading client - requires private key
client = ClobClient(
host="https://clob.polymarket.com",
key="YOUR_PRIVATE_KEY",
chain_id=137, # Polygon mainnet
signature_type=2, # Proxy wallet
funder="YOUR_PROXY_ADDRESS"
)
# Create/derive API credentials
creds = client.create_or_derive_api_creds()
client.set_api_creds(creds)Fetching Market Data
Get Market Information
# Get all markets
markets = client.get_simplified_markets()
# Get specific market by condition ID
market = client.get_market("CONDITION_ID")
# Get orderbook for a token
book = client.get_order_book("TOKEN_ID")
print(f"Best bid: {book['bids'][0]}")
print(f"Best ask: {book['asks'][0]}")
# Get midpoint price
mid = client.get_midpoint("TOKEN_ID")
print(f"Midpoint: {mid}")Placing Orders
Limit Order (GTC)
from py_clob_client.clob_types import OrderArgs, OrderType
from py_clob_client.order_builder.constants import BUY, SELL
# Create a buy order
order_args = OrderArgs(
price=0.45, # Price per share
size=100.0, # Number of shares
side=BUY, # BUY or SELL
token_id="TOKEN_ID"
)
# Sign the order
signed_order = client.create_order(order_args)
# Submit the order
resp = client.post_order(signed_order, OrderType.GTC)
print(f"Order ID: {resp['orderID']}")Market Order (FOK)
from py_clob_client.clob_types import MarketOrderArgs
# Create a market buy order
mo_args = MarketOrderArgs(
token_id="TOKEN_ID",
amount=25.0, # Dollar amount to spend
side=BUY
)
signed_order = client.create_market_order(mo_args)
resp = client.post_order(signed_order, OrderType.FOK)Order Management
Cancel Orders
from py_clob_client.clob_types import OpenOrderParams
# Get open orders
orders = client.get_orders(OpenOrderParams())
# Cancel specific order
client.cancel("ORDER_ID")
# Cancel all orders
client.cancel_all()Complete Trading Bot Example
Simple Arbitrage Scanner
import asyncio
from py_clob_client.client import ClobClient
async def scan_for_arbitrage():
client = ClobClient("https://clob.polymarket.com")
while True:
markets = client.get_simplified_markets()
for market in markets:
if len(market.get('tokens', [])) == 2:
yes_token = market['tokens'][0]
no_token = market['tokens'][1]
yes_price = float(client.get_midpoint(yes_token['token_id']))
no_price = float(client.get_midpoint(no_token['token_id']))
combined = yes_price + no_price
if combined < 0.98:
print(f"ARBITRAGE: {market['question']}")
print(f"YES: {yes_price}, NO: {no_price}")
print(f"Combined: {combined}")
print(f"Profit: {((1/combined) - 1) * 100:.2f}%")
await asyncio.sleep(30)
asyncio.run(scan_for_arbitrage())Best Practices
Handle Rate Limits
The API has rate limits. Implement exponential backoff and caching for market data.
Secure Your Keys
Never hardcode private keys. Use environment variables or a secrets manager.
Test on Small Amounts
Always test new strategies with small position sizes before scaling up.
Implement Error Handling
Network requests fail. Wrap API calls in try/except and implement retry logic.
Skip the Coding
PredictEngine lets you build trading bots without writing code. Just describe your strategy and our AI builds it for you.
Try No-Code BotsFrequently Asked Questions
Is the API free to use?
Yes, the Polymarket API is free. You only pay trading fees (2% on winning positions, 0% maker fees).
What are the rate limits?
Rate limits are generous for most use cases. For high-frequency trading, implement caching and respect 429 errors.
Can I use other languages?
Yes - the REST API works with any language. There's also an unofficial TypeScript client. Python is most popular for trading bots.