Skip to main content

Sample Code

This section provides complete, working Python code samples to help you test trading functionality using the Polymarket Trading Gateway pre-production environment.
These code samples are provided for testing purposes only and connect to the pre-production environment. Use these samples to familiarize yourself with the API and test your integration before going live. They are NOT production-ready code and should not be taken as complete, secure, fault-tolerant code for production use.

About the Trading Gateway

The Trading Gateway provides core trading functionality. It uses:
  • Username/password authentication (not API keys)
  • Token-based authorization with access_token and refresh_token
  • REST API for trading operations
  • gRPC streaming for real-time market data and order updates
These samples are for the Trading Gateway (core trading APIs). For the Exchange Gateway (comprehensive trading and non-trading functionality including payments, KYC, and broader account management), visit apidocs.polymarketexchange.com.

Available Samples

REST API Client

polymarketus_client.py - Complete REST API client with interactive CLI Features:
  • Username/password authentication
  • Account management
  • Order placement and cancellation
  • Order queries
  • Bulk operations

gRPC Streaming Clients

market_data_stream.py - Stream real-time market data
  • Order book updates
  • Trade data
  • Instrument statistics
order_stream.py - Stream order and execution updates
  • New order confirmations
  • Fill notifications
  • Order state changes

Prerequisites

Install Dependencies

pip install grpcio grpcio-tools protobuf requests

Download Proto Files

Download the protocol buffer definitions: Download trading-gateway-protos.zip Extract and generate Python code:
# Extract proto files
unzip trading-gateway-protos.zip

# Generate Python code
python -m grpc_tools.protoc \
  --python_out=. \
  --grpc_python_out=. \
  --proto_path=. \
  connamara/ep3/v1beta1/market_data.proto \
  connamara/ep3/v1beta1/order_entry.proto \
  connamara/ep3/orders/v1beta1/orders.proto \
  connamara/ep3/instruments/v1beta1/instruments.proto \
  connamara/ep3/type/v1beta1/common.proto

Pre-Production Endpoints

All sample code connects to the pre-production testing environment: REST API:
https://rest.preprod.polymarketexchange.com
gRPC Server:
traderapi.us-east-1.privatelink.preprod.polymarketexchange.com:443
These samples are configured for pre-production testing only. Contact your account manager for production credentials and endpoints when you’re ready to go live.