Prediction Market API: Build Your Own Trading Bot
Key takeaway: Polymarket's CLOB (Central Limit Order Book) API lets you programmatically place orders, stream prices, and manage positions. Combined with the Gamma API for market data, you can build a fully automated prediction market trading bot.
Algorithmic trading is not just for Wall Street. The Polymarket API gives developers full access to the world's largest prediction market. Whether you want to automate a simple rebalancing strategy or build a sophisticated market-making bot, this guide covers everything you need to get started.
API Architecture Overview
Polymarket exposes two main APIs:
- Gamma API (
gamma-api.polymarket.com): Market metadata — events, markets, conditions, historical data. Public, no authentication needed - CLOB API (
clob.polymarket.com): Order placement, cancellation, position management, and real-time order book data. Requires EIP-712 derived API credentials
Authentication
CLOB API authentication uses two layers:
- L1 Authentication (EIP-712): Sign a typed-data message with your Ethereum private key to derive API credentials (apiKey, secret, passphrase)
- L2 Authentication (HMAC-SHA256): Sign each API request with the derived credentials. The signature covers the timestamp, method, path, and body
Example credential derivation (JavaScript):
import { ethers } from "ethers";
const wallet = new ethers.Wallet(PRIVATE_KEY);
const domain = { name: "ClobAuthDomain", ... };
const types = { ClobAuth: [{ name: "address", type: "address" }, ...] };
const signature = await wallet.signTypedData(domain, types, value);
// POST to /auth/derive-api-key with the signature
Fetching Market Data
The Gamma API provides all the market metadata you need:
// List active events
GET https://gamma-api.polymarket.com/events?active=true&limit=100
// Get specific market details
GET https://gamma-api.polymarket.com/markets/{conditionId}
// Historical price data
GET https://gamma-api.polymarket.com/markets/{conditionId}/prices
Placing Orders
The CLOB API supports market orders, limit orders, and multiple time-in-force options:
- GTC (Good-Till-Cancelled): Sits in the order book until filled or cancelled
- GTD (Good-Till-Date): Expires at a specified time
- FOK (Fill-Or-Kill): Must fill completely or not at all
- IOC (Immediate-Or-Cancel): Fills what it can, cancels the rest
WebSocket Streaming
For real-time data, connect to the CLOB WebSocket endpoint:
// Subscribe to order book updates
ws.send(JSON.stringify({
type: "subscribe",
channel: "market",
assets_id: TOKEN_ID
}));
Building a Simple Strategy
A basic mean-reversion bot might:
- Monitor prices for a set of markets via WebSocket
- Calculate a rolling average over the last 24 hours
- Buy when price drops 10%+ below the average
- Sell when price returns to the average
- Use Kelly criterion to size positions
Rate Limits and Best Practices
- CLOB API: 100 requests per 10 seconds per API key
- Always implement exponential backoff on 429 responses
- Use WebSockets for real-time data instead of polling
- Keep your private key in environment variables, never in code
- Test on small positions before scaling up
PolyGram users can access all these markets through a simplified interface — no API development required. Start trading on PolyGram →