Overview
The Python sample code demonstrates how to authenticate and use the Polymarket Exchange API. It includes a complete client library with 20 example scripts covering REST endpoints, gRPC streaming, and advanced features like pagination and historical data.Download Sample Code
Download the complete sample code package including Python and Go examples.
What’s Included
- REST API Client - Complete client for all REST endpoints
- gRPC Client - Streaming client for real-time market data and order updates
- 20 Example Scripts - Covering REST, gRPC streaming, pagination, and advanced features
- Authentication - Private key JWT authentication flow
- Generated Protos - Pre-generated protocol buffer bindings
Prerequisites
- Python 3.6 or higher
- Credentials (Client ID and private key) provided by Polymarket
- A registered trading account
Installation
Configuration
Copy the sample environment file and fill in your credentials:Required Environment Variables
| Variable | Description |
|---|---|
POLYMARKET_CLIENT_ID | Your Client ID (provided by Polymarket) |
POLYMARKET_PRIVATE_KEY_PATH | Path to your RSA private key (PEM format) |
POLYMARKET_API_URL | API base URL (e.g., https://api.preprod.polymarketexchange.com) |
POLYMARKET_AUTH_DOMAIN | Authentication domain (e.g., pmx-preprod.us.auth0.com) |
POLYMARKET_AUTH_AUDIENCE | Authentication audience (matches API URL) |
POLYMARKET_PARTICIPANT_ID | Your participant ID (e.g., firms/Your-Firm/users/your-user) |
POLYMARKET_ACCOUNT | Your trading account (e.g., firms/Your-Firm/accounts/your-account) |
Optional Environment Variables
| Variable | Default | Description |
|---|---|---|
POLYMARKET_TEST_SYMBOL | SBLIX-KC-YES | Symbol for order placement demo |
POLYMARKET_VERBOSE | false | Enable verbose logging |
POLYMARKET_TIMEOUT | 30 | Request timeout (seconds) |
REST API Examples
| Example | Description |
|---|---|
01_health_check.py | Health check (no authentication required) |
02_whoami.py | Get current user info |
03_list_accounts.py | List trading accounts |
04_get_balance.py | Get account balance |
05_list_positions.py | List positions |
06_list_instruments.py | List tradeable instruments |
07_list_symbols.py | List all symbols |
08_search_orders.py | Search order history |
09_list_users.py | List users in firm |
10_place_and_cancel_order.py | Place and cancel order (safe demo) |
gRPC Streaming Examples (11-15)
| Example | Description |
|---|---|
11_grpc_market_data_stream.py | Real-time L2 order book updates |
12_grpc_order_subscription.py | Live order status and execution updates |
13_grpc_position_subscription.py | Real-time position changes |
14_grpc_funding_subscription.py | Funding notifications |
15_grpc_dropcopy_subscription.py | Execution reports (drop copy feed) |
Advanced Examples (16-20)
| Example | Description |
|---|---|
16_pagination.py | Paginating through large result sets |
17_historical_positions.py | Querying historical position data |
18_instrument_filters.py | Filtering instruments by criteria |
19_grpc_bidi_market_data_stream.py | Bidirectional streaming market data |
20_sports_refdata.py | Sports reference data queries |
Running Examples
Run All Examples
Run Individual Examples
Code Examples
Authentication Flow
The client uses private_key_jwt authentication:Using the API Client
Placing and Canceling Orders
File Structure
Troubleshooting
”Private key file not found”
EnsurePOLYMARKET_PRIVATE_KEY_PATH points to a valid PEM file:
“Missing required environment variables”
Source your environment file before running:“Authentication server returned 401”
- Verify your Client ID is correct
- Ensure your private key matches the public key registered with Polymarket
- Check that your credentials haven’t expired