Polymarket has become one of the most important on-chain prediction markets, enabling users to trade event outcomes with real liquidity. Under the hood, its trading system is powered by a Central Limit Order Book (CLOB) architecture, which allows algorithmic trading, market making, and automated strategies.
This article provides a deep technical breakdown of Polymarket API authentication, order execution, and bot architecture design, with real-world examples and production-ready insights.
We will also explore:
- How authentication really works (L1 + L2 model)
- How orders are signed and executed
- How trading bots interact with the CLOB
- Common pitfalls in production systems
- Strategy insights from real trading bots
Official docs:
π https://docs.polymarket.com ([Polymarket Documentation][1])
π§ 1. Understanding Polymarket Architecture
Polymarket is not a simple REST API exchange. It consists of three distinct layers:
ββββββββββββββββββββββββββββββββ
β Gamma API β
β Market Data / Metadata β
βββββββββββββββ¬βββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β Data API β
β Positions / Trades / PnL β
βββββββββββββββ¬βββββββββββββββββ
β
βΌ
ββββββββββββββββββββββββββββββββ
β CLOB API β
β Order Book + Trading Engine β
ββββββββββββββββββββββββββββββββ
Key insight:
Only the CLOB API is used for trading execution. Everything else is informational.
π Source: Polymarket API overview (https://docs.polymarket.com/api-reference)
π 2. Polymarket Authentication Model (CRITICAL)
Polymarket uses a two-layer authentication system:
πΉ Layer 1 (L1): Wallet Signature Authentication
L1 is based on:
- Your Ethereum/Polygon wallet
- EIP-712 signed messages
Used for:
- Creating API keys
- Verifying ownership of wallet
- Bootstrapping trading credentials
π Think of it as:
βProving you own the walletβ
πΉ Layer 2 (L2): API Key Authentication
Once L1 is completed, you receive:
{
"apiKey": "uuid",
"secret": "base64_secret",
"passphrase": "random_string"
}
These are used for fast trading requests.
Used for:
- Placing orders
- Cancelling orders
- Fetching account state
π L2 Required Headers
Every trading request must include:
POLY_ADDRESS
POLY_API_KEY
POLY_PASSPHRASE
POLY_SIGNATURE
POLY_TIMESTAMP
Signature = HMAC-SHA256(secret, request_payload)
βοΈ 3. Authentication Flow (Step-by-Step)
Wallet (Private Key)
β
βΌ
L1 Signature (EIP-712)
β
βΌ
POST /auth/api-key
β
βΌ
Receive API credentials
β
βΌ
L2 HMAC signing
β
βΌ
Trading via CLOB API
π§ͺ 4. Python Example: Authentication + Client Setup
Using official SDK:
from py_clob_client_v2 import ClobClient
import os
client = ClobClient(
host="https://clob.polymarket.com",
chain_id=137,
key=os.getenv("PRIVATE_KEY")
)
credentials = client.create_or_derive_api_key()
print(credentials)
π 5. Order Execution Lifecycle (VERY IMPORTANT)
A Polymarket order is NOT a simple API call.
It goes through:
1. Build order intent
2. Sign order locally (wallet)
3. Attach API headers (L2)
4. Submit to CLOB engine
5. Match against order book
6. Settlement recorded on-chain
π§Ύ Order Flow Diagram
Trader Bot
β
βΌ
Create Order (token_id, price, size)
β
βΌ
Sign with wallet (EIP-712)
β
βΌ
Attach L2 headers
β
βΌ
POST /order
β
βΌ
CLOB Matching Engine
β
βΌ
Matched / Partial Fill / Open Order
β
βΌ
On-chain settlement (Polygon)
π» 6. Example: Placing an Order (Python)
from py_clob_client_v2 import OrderArgs, BUY
order = client.create_and_post_order(
OrderArgs(
token_id="123456",
price=0.65,
size=100,
side=BUY
),
options={
"tick_size": "0.01",
"neg_risk": False
}
)
print(order)
β‘ 7. Node.js Example (Trading Bot Style)
import { ClobClient, Side } from "@polymarket/clob-client-v2";
import { privateKeyToAccount } from "viem/accounts";
import { createWalletClient, http } from "viem";
const account = privateKeyToAccount(process.env.PRIVATE_KEY);
const signer = createWalletClient({
account,
transport: http()
});
const client = new ClobClient({
host: "https://clob.polymarket.com",
chain: 137,
signer
});
const order = await client.createAndPostOrder({
token_id: "123456",
price: 0.70,
size: 50,
side: "BUY"
});
console.log(order);
π§ 8. Trading Bot Architecture (Production Design)
A serious Polymarket bot is structured like:
ββββββββββββββββββββββ
β Market Data Feed β
β (Gamma API) β
βββββββββββ¬βββββββββββ
βΌ
ββββββββββββββββββββββ
β Strategy Engine β
β - signals β
β - pricing models β
βββββββββββ¬βββββββββββ
βΌ
ββββββββββββββββββββββ
β Risk Manager β
β - exposure limits β
βββββββββββ¬βββββββββββ
βΌ
ββββββββββββββββββββββ
β Execution Engine β
β (CLOB API) β
βββββββββββ¬βββββββββββ
βΌ
ββββββββββββββββββββββ
β PnL Tracker β
ββββββββββββββββββββββ
π 9. Common Issues in Production Bots
β 1. Signature mismatch
- Wrong wallet used
- API key tied to different address
β 2. Order signer mismatch error
βorder signer address has to be API key addressβ
This is common in fresh accounts.
β 3. Missing deposit wallet alignment
Some accounts require:
- deposit wallet β EOA wallet mismatch handling
β 4. No historical orderbook data
Important limitation:
Polymarket does NOT provide full historical orderbook state.
Only fills are stored on-chain.
π 10. Strategy Insights from Real Trading Bots
From open-source bot implementations like:
π https://github.com/mateosoul/Polymarket-Trading-Bot-Python
And live trading profiles:
π https://polymarket.com/@mateosoul
We can extract real-world strategies:
π’ 1. Market making
- Place both bid/ask
- Profit from spread
π‘ 2. Momentum trading
- Follow probability spikes
π΅ 3. Event arbitrage
- Cross-market inefficiencies
π΄ 4. Resolution betting
- High confidence near event expiry
π 11. Performance Tracking (PnL System)
Typical bot PnL structure:
PnL = realized gains + unrealized position value - fees
Tracked via:
- Data API positions endpoint
- On-chain fills
- Local ledger system
π§Ύ 12. Security Best Practices
- Never hardcode private keys
- Use
.envor vault systems - Rotate API keys regularly
- Limit bot permissions
β FAQ (SEO BOOST SECTION)
β What is Polymarket API used for?
It is used for:
- Trading prediction markets
- Fetching market data
- Building automated trading bots
β Is Polymarket API free?
Yes, but trading requires authenticated credentials.
β Can I build a trading bot with Polymarket API?
Yes. The CLOB API is designed specifically for algorithmic trading.
β Why is authentication complex?
Because it uses:
- Wallet-based L1 security
- API-key-based L2 speed layer
β Can I get historical orderbook data?
No. Only trade fills are stored on-chain.
π Official Resources
- Docs: https://docs.polymarket.com
- GitHub Bot: https://github.com/mateosoul/Polymarket-Trading-Bot-Python
- Bot Trading Profile: https://polymarket.com/@mateosoul
- Contact info: https://polymarket.com/@mateosoul
π§ Final Thoughts
Polymarketβs API is not just a trading interfaceβit is a hybrid decentralized execution system combining off-chain matching with on-chain settlement.
Understanding:
- L1 authentication (wallet trust)
- L2 authentication (execution speed)
- CLOB matching engine (execution layer)
β¦is essential for building serious trading bots and quantitative strategies.
Top comments (0)