CRITICAL: Tokens must be refreshed every 3 minutes.Access tokens have a short expiration. Your application MUST implement automatic token refresh before expiration to maintain uninterrupted streaming connections.
import grpcfrom datetime import datetimefrom polymarket.v1 import marketdatasubscription_pb2from polymarket.v1 import marketdatasubscription_pb2_grpc# Create secure channelcredentials = grpc.ssl_channel_credentials()channel = grpc.secure_channel('grpc-api.preprod.polymarketexchange.com:443', credentials)# Create stubstub = marketdatasubscription_pb2_grpc.MarketDataSubscriptionAPIStub(channel)# Create requestrequest = marketdatasubscription_pb2.CreateMarketDataSubscriptionRequest( symbols=["tec-nfl-sbw-2026-02-08-kc"], unaggregated=False, depth=10, snapshot_only=False)# Set up metadata with authorizationmetadata = [ ('authorization', f'Bearer {access_token}')]# Start streamingprint("Starting market data stream...")response_stream = stub.CreateMarketDataSubscription(request, metadata=metadata)for response in response_stream: if response.HasField('heartbeat'): timestamp = datetime.now().strftime('%H:%M:%S') print(f"[{timestamp}] Heartbeat received") elif response.HasField('update'): update = response.update timestamp = datetime.now().strftime('%H:%M:%S') print(f"\n[{timestamp}] Market Update for {update.symbol}") print(f" Bids: {len(update.bids)}") print(f" Offers: {len(update.offers)}") # Get price_scale from instrument metadata (via list_instruments API) # You must implement this function to fetch from your instrument cache price_scale = get_instrument_price_scale(update.symbol) # implement this if update.bids: top_bid_px = update.bids[0].px / price_scale print(f" Top Bid: ${top_bid_px:.4f} x {update.bids[0].qty}") if update.offers: top_offer_px = update.offers[0].px / price_scale print(f" Top Offer: ${top_offer_px:.4f} x {update.offers[0].qty}")
Price Representation: All prices are int64 values. Divide by the instrument’s price_scale to get the decimal price.price_scale varies by instrument. Get it from:
Instrument metadata via list_instruments or get_instrument_metadata