Overview
The Go sample code demonstrates how to authenticate and use the Polymarket Exchange API, including REST endpoints, real-time gRPC streaming, and advanced features. It includes 20 example programs covering REST, gRPC streaming, Connect protocol, 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
- Connect Protocol - HTTP/2 alternative to native gRPC
- 20 Example Programs - Covering REST, gRPC streaming, Connect protocol, pagination, and advanced features
- Authentication - Private key JWT authentication flow
- Generated Protos - Pre-generated protocol buffer bindings
Prerequisites
- Go 1.21 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_GRPC_ADDR | (derived from API URL) | gRPC endpoint address |
POLYMARKET_VERBOSE | false | Enable verbose logging |
POLYMARKET_TIMEOUT | 30 | Request timeout (seconds) |
Running Examples
Using Make
Using Go Directly
REST API Examples (01-10)
| Example | Description |
|---|---|
01_health_check | Health check (no authentication required) |
02_whoami | Get current user info |
03_list_accounts | List trading accounts |
04_get_balance | Get account balance |
05_list_positions | List positions |
06_list_instruments | List tradeable instruments |
07_list_symbols | List all symbols |
08_search_orders | Search order history |
09_list_users | List users in firm |
10_place_and_cancel_order | Place and cancel order (safe demo) |
gRPC Streaming Examples (11-16)
These examples demonstrate real-time streaming connections for live market data and trading updates:| Example | Description |
|---|---|
11_grpc_market_data_stream | Real-time order book updates - Subscribe to bid/ask changes |
12_grpc_order_subscription | Order status notifications - Get live updates on your orders |
13_grpc_position_subscription | Position change updates - Track position changes in real-time |
14_grpc_funding_subscription | Funding notifications - Monitor deposits and withdrawals |
15_grpc_dropcopy_subscription | Execution reports (drop copy) - Receive trade confirmations |
16_connect_streaming | Connect protocol - Streaming via HTTP/2 (alternative to gRPC) |
Advanced Examples (17-20)
| Example | Description |
|---|---|
17_pagination | Paginating through large result sets |
18_historical_positions | Querying historical position data |
19_instrument_filters | Filtering instruments by criteria |
20_grpc_bidi_market_data | Bidirectional streaming market data |
Code Examples
REST API Client
gRPC Streaming - Market Data
gRPC Streaming - Order Subscription
Authentication Flow
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
gRPC Connection Issues
- Verify the gRPC address is correct (default derived from API URL)
- Check that port 443 is not blocked by a firewall
- Try setting
POLYMARKET_GRPC_ADDRexplicitly