Skip to main content

Authentication for gRPC Streaming

gRPC streaming connections use JWT (JSON Web Token) authentication obtained from the REST API. This guide explains the complete authentication flow using Python.

Authentication Architecture

Step 1: Obtain JWT Token from REST API

Before establishing any gRPC streaming connection, you must first authenticate via the REST API.

Login Request

curl "https://traderapi.us-east-1.privatelink.preprod.polymarketexchange.com/auth/v1beta1/login" -H "Host: rest.preprod.polymarketexchange.com" -H "accept: application/json" -H "content-type: application/json" --data '{"password":"<password>","username":"<username>"}'

Login Response

{
  "access_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "refresh_token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "access_expiration_time": "2025-01-18T12:00:00Z",
  "refresh_expiration_time": "2025-01-19T12:00:00Z"
}
Token Fields:
  • access_token: Use this for gRPC authentication
  • refresh_token: Use this to obtain a new access token before expiration
  • access_expiration_time: When the access token expires
  • refresh_expiration_time: When the refresh token expires

Step 2: Attach Token to gRPC Metadata

Once you have the access token, include it in the gRPC metadata for every streaming connection.
import grpc
from connamara.ep3.v1beta1 import market_data_pb2_grpc

# Create secure channel
credentials = grpc.ssl_channel_credentials()
channel = grpc.secure_channel('traderapi.us-east-1.privatelink.preprod.polymarketexchange.com:443', credentials)

# Create stub
stub = market_data_pb2_grpc.MarketDataSubscriptionAPIStub(channel)

# Create metadata with authorization token
metadata = [
    ('authorization', access_token)
]

# Make streaming call with metadata
response_stream = stub.CreateMarketDataSubscription(request, metadata=metadata)
The metadata key must be authorization (lowercase). The value is the raw JWT token string.

Handling Authentication Errors

Common Authentication Errors

Error CodeDescriptionSolution
UNAUTHENTICATEDToken is invalid or missingVerify token in metadata, re-login if expired
PERMISSION_DENIEDToken is valid but lacks permissionsCheck user permissions with support
UNAVAILABLECannot reach authentication serviceCheck network connectivity, retry with backoff

Next Steps