API · Build
Copy-paste recipes for the things every Polymarket bot needs. All CLOB V2 / pUSD, Python (the reads need no key). Grab one, run it, move on.
1. Get a market's token IDs from a slug
import requests, json
m = requests.get("https://gamma-api.polymarket.com/markets",
params={"slug": SLUG}).json()[0]
outcomes = json.loads(m["outcomes"]) # ["Yes","No"]
token_ids = json.loads(m["clobTokenIds"]) # aligned with outcomes
yes_token = token_ids[outcomes.index("Yes")]
2. Read the book & estimate slippage
from py_clob_client_v2 import ClobClient
c = ClobClient(host="https://clob.polymarket.com", chain_id=137)
book = c.get_order_book(token_id)
def cost_to_buy(book, shares):
rem, spent = shares, 0.0
for lvl in book.asks:
take = min(rem, float(lvl.size)); spent += take*float(lvl.price); rem -= take
if rem <= 0: break
return spent/shares if shares else None # avg fill price
3. Place & cancel a limit order
from py_clob_client_v2 import (ClobClient, OrderArgs, OrderType,
PartialCreateOrderOptions, Side, OrderPayload)
c = ClobClient(host="https://clob.polymarket.com", chain_id=137, key=PK, creds=creds)
resp = c.create_and_post_order(
order_args=OrderArgs(token_id=token_id, price=0.45, side=Side.BUY, size=100),
options=PartialCreateOrderOptions(tick_size="0.01"),
order_type=OrderType.GTC,
)
c.cancel_order(OrderPayload(orderID=resp["orderID"]))
4. Track any wallet's positions & PnL (no key)
import requests
for p in requests.get("https://data-api.polymarket.com/positions",
params={"user": PROXY_ADDRESS}).json():
print(p["title"], p["outcome"], "PnL", p["cashPnl"])
5. Watch your orders over WebSocket
# wss://ws-subscriptions-clob.polymarket.com/ws/user (auth: api key/secret/passphrase)
# subscribe by condition_id; you get fills/cancels for your orders in real time.
# See the WebSocket page for the full reconnect + heartbeat pattern.
6. A minimal market-maker tick
mid = c.get_midpoint(token_id)
spread = 0.02 # quote 1 cent each side of mid
bid = round(mid - spread/2, 2); ask = round(mid + spread/2, 2)
c.create_and_post_order(order_args=OrderArgs(token_id, bid, Side.BUY, 50),
options=PartialCreateOrderOptions(tick_size="0.01"), order_type=OrderType.GTC)
c.create_and_post_order(order_args=OrderArgs(token_id, ask, Side.SELL, 50),
options=PartialCreateOrderOptions(tick_size="0.01"), order_type=OrderType.GTC)
# reprice on every book update; makers pay 0 fees and earn liquidity rewards.
Before any live order: collateral must be pUSD, and you must approve
pUSD + conditional tokens to the three V2 exchange contracts (see
the allowance fix). Default to paper mode.
Next: CLOB reference · Data API · Skip the plumbing — the starter kit.
Зрозуміло? Застосуйте на практиці





