Skip to main content

Unified Polymarket v2 APIs with Pydantic data validation - Clob, Gamma, Data, Web3, Websockets, GraphQL clients.

Project description

pm-apis PyPI version

Unified Polymarket v2 APIs with Pydantic data validation - Clob, Gamma, Data, Web3, Websockets, GraphQL clients.

Polymarket Mental Models

Events, Markets and Outcomes

The Polymarket ecosystem is organized hierarchically:

flowchart LR
    A([Event]) --- B([Market A])
    A --- C([Market B])
    A ~~~ Dot1@{ shape: sm-circ}
    A ~~~ Dot2@{ shape: sm-circ}
    A ~~~ Dot3@{ shape: sm-circ}
    A -.- D([Market n])
    B --- E([Outcome 1])
    B --- F([Outcome 2])
    C --- G([Outcome 1])
    C --- H([Outcome 2])
  • Event — represents a proposition or question such as “How many Fed rate cuts in 2025?”.

    • Identified by a human-readable slug (e.g. how-many-fed-rate-cuts-in-2025) and an event id (e.g. 16085).
  • Market — represents a specific option for the related event (e.g. 1 rate cut in 2025). An Event has 1 or more corresponding Markets. (e.g. 9 options in this case - {0, 1, 2, ..., 7, 8 or more} rate cuts in 2025)

    • Identified by a condition id (e.g. 0x8e9b6942b4dac3117dadfacac2edb390b6d62d59c14152774bb5fcd983fc134e for 1 rate cut in 2025), a human-readable slug (e.g. 'will-1-fed-rate-cut-happen-in-2025') and a market id (e.g. 516724).
  • Outcome — represents a binary option related to a market. (most commonly Yes/No, but can be e.g. Paris Saint-Germain/Inter Milan in the case of a match where draws are not possible)

    • Identified by a token id (e.g. 15353185604353847122370324954202969073036867278400776447048296624042585335546 for the Yes outcome in the 1 rate cut in 2025 market)
  • The different APIs represent Events/Markets differently (e.g. Event, QueryEvent / ClobMarket, GammaMarket, RewardsMarket) but they all use to the same underlying identifiers.

Tokens

  • Tokens are the blockchain implementation of Outcomes - tradable digital assets on the Polygon blockchain that users buy, hold and sell on Polygon.
  • This helps ensure the logic of binary outcome prediction markets through smart contracts (e.g. collateralization, token prices going to $1.00 or $0.00 after resolution, splits/merges).

Splits and Merges

  • Holding 1 Yes share + 1 No share in a Market (e.g. 'will-1-fed-rate-cut-happen-in-2025') covers the entire universe of possibilities and guarantees a $1.00 payout regardless of outcome. This mathematical relationship enables Polymarket's core mechanisms: splitting (1 pUSD → 1 Yes + 1 No) and merging (1 Yes + 1 No → 1 pUSD) at any point before resolution.
  • Splits are the only way tokens are created. Either a user splits pUSD into equal shares of the complementary tokens or Polymarket automatically splits pUSD when it matches an Yes buy order at e.g. 30¢ with a No buy order at 70¢.

Unified Order Book

  • Polymarket uses traditional exchange mechanics - a Central Limit Order Book (CLOB), where users place buy and sell orders that get matched by price and time priority.
  • However, because the Yes and No outcomes form a complete probability universe, certain orders become mathematically equivalent - allowing the matching engine to find trades as exemplified above.
  • This unified structure means every BUY order for Outcome 1 at price X is simultaneously visible as a SELL order for Outcome 2 at price (100¢ - X), creating deeper liquidity and tighter spreads than separate order books would allow.

Negative Risk and Conversions

  • If the Markets in and Event collectively cover a complete universe of possibilities (e.g. {0, 1, 2, ..., 7, 8 or more} rate cuts in 2025) and only one winner is possible, two collections of positions (made up of tokens and pUSD) become mathematically equivalent and the Event is said to support negative risk.
    • e.g. Hold 1 No token in the 0 rate cuts in 2025. This is equivalent to holding 1 Yes token in each of the other Markets {1, 2, ..., 7, 8 or more}.
  • An interesting consequence is that holding No tokens in more than one Market is equivalent to Yes tokens and some pUSD.
    • e.g. Hold 1 No token on each of {0, 1, 2, ..., 7, 8 or more} rate cuts in 2025. Because only one winner is possible, this guarantees that 8 out of the 9 Markets resolve to No. This is equivalent to a position of 8 pUSD.
    • e.g. Hold 1 No token on each of {0, 1} rate cuts in 2025. This is equivalent to 1 Yes token in {2, ..., 7, 8 or more} and 1 pUSD.
  • Polymarket allows for the one way (for capital efficiency) conversion from No tokens to a collection of Yes tokens and pUSD before resolution through a smart contract.

Clients overview

PolymarketReadOnlyClobClient

Read-only order book related operations.

  • Order book
    • get one or more order books, best price, spread, midpoint, and last trade price by token_id
  • Miscellaneous
    • get market microstructure parameters by condition_id (get_clob_market_info) - tokens, tick size, minimum order size, fees, rewards, RFQ flags, and order-age settings
    • get condition_id and its corresponding token_ids for a market by token_id
    • get crypto outcomes by slug for up/down markets
    • get recent price history by token_id in the last 1h, 6h, 1d, 1w, 1m
    • get price history by token_id in a start/end interval
    • get all price history by token_id in 2-minute increments
    • get ClobMarket by condition_id
    • get all ClobMarkets

PolymarketClobClient

Order book related operations.

  • Authentication
    • private_key
    • address
    • signature_type is optional and can be derived from the address
      • signature_type=0 for EOAs (Externally Owned Accounts)
      • signature_type=1 for Email/Magic wallets
      • signature_type=2 for Safe/Gnosis wallets
      • signature_type=3 for Deposit wallets
  • All operations from PolymarketReadOnlyClobClient
  • Orders
    • create and post limit or market orders
    • cancel one or more orders by order_id
    • cancel all orders for a condition_id/token_id
    • cancel all orders
    • get active orders
    • send heartbeat to keep orders alive
  • Trades
    • get trade history for a user with filtering by condition_id, token_id, trade_id, and time window
  • Rewards
    • check if one or more orders are scoring for liquidity rewards by order_id
    • get daily earned rewards
    • check if a market offers rewards by condition_id with get_market_rewards()
    • get all active markets that offer rewards, sorted and filtered by multiple criteria, with get_reward_markets()
  • Miscellaneous
    • get pUSD balance
    • get token balance by token_id
    • detect wallet signature type

PolymarketGammaClient

Market and event related operations.

  • Market
    • get GammaMarket by market_id
    • get GammaMarket by slug
    • get GammaMarkets with pagination and filtering by slug, market_id, token_id, condition_id, tag_id, active/closed/archived status, liquidity window, volume window, start date window, end date window, and ordering
    • get tags for a market by market_id
  • Event
    • get Event by event_id
    • get Event by slug
    • get Events with pagination and filtering by slug, event_id, tag_id, active/closed/archived status, liquidity window, volume window, start date window, end date window, and ordering
    • get all Events given some filtration
    • search Events, Tags, and Profiles by text query, tags, status, recurrence, and multiple sort modes
    • grok an event summary by event slug
    • grok an election market explanation by candidate name and election title
    • get tags for an event by event_id
  • Tag
    • get Tags with pagination, ordered by any tag field
    • get all Tags
    • get Tag by tag_id
    • get Tag relations by tag_id or slug
    • get tags related to a tag by tag_id or slug
  • Sport
    • get Teams with pagination, filtered by league, name, and abbreviation
    • get all Teams given some filtration
    • get Sports with pagination, filtered by name
    • get sports metadata
  • Series
    • get Series with pagination, filtered by slug and closed status, and ordered by any series field
    • get all Series given some filtration
  • Comments
    • get comments by parent_entity_type and parent_entity_id with pagination, ordered by any comment field
    • get comments by comment_id, returning all comments in a thread
    • get comments by user base address with pagination, ordered by any comment field
  • Miscellaneous
    • get public profile by user address

PolymarketDataClient

Portfolio related operations.

  • Positions
    • get positions with pagination by user address, filtered by condition_id, position size, redeemability, mergeability, and title
  • Trades
    • get trades with pagination, filtered by condition_id, user address, side, taker-only status, cash amount, and token amount
  • Activity
    • get activity with pagination by user address, filtered by type, condition_id, time window, side, and sort order
  • Holders
    • get top holders by condition_id
  • Value
    • get positions value by user address and condition_ids
    • condition_ids is None means total value of positions
    • condition_ids is str means value of positions on one market
    • condition_ids is list[str] means sum of values of positions on multiple markets
  • Closed positions
    • get closed positions, filtered by condition_id
  • Miscellaneous
    • get total number of markets traded by user address
    • get open interest for a list of condition_ids
    • get live volume for an event by event_id
    • get pnl timeseries by user address for a period (1d, 1w, 1m, all) with frequency (1h, 3h, 12h, 1d)
    • get overall pnl/volume by user address for a recent window (1d, 7d, 30d, all)
    • get user rank on the profit/volume leaderboards by user address for a recent window (1d, 7d, 30d, all)
    • get top users on the profit/volume leaderboards for a recent window (1d, 7d, 30d, all)

PolymarketWeb3Client / PolymarketGaslessWeb3Client

Blockchain clients for blockchain based Polymarket operations. Both clients support the same core operations detailed below

  • PolymarketWeb3Client submits transactions directly on-chain, so the base EOA for the provided private_key must hold POL for gas, regardless of signature_type.

  • PolymarketGaslessWeb3Client submits transactions through the relayer, and Polymarket pays for gas

  • Authentication

    • PolymarketWeb3Client
      • private_key
      • signature_type
    • PolymarketGaslessWeb3Client
      • private_key
      • signature_type
      • either relayer_api_key (create here) - unlimited rate limit
        • the client derives RELAYER_API_KEY_ADDRESS from the wallet automatically
      • or builder_creds (create here) as ApiCreds
  • Supported wallet types

    • PolymarketWeb3Client
      • EOA wallets (signature_type=0)
      • Email/Magic proxy wallets (signature_type=1)
      • Safe/Gnosis wallets (signature_type=2)
    • PolymarketGaslessWeb3Client
      • EOA wallets (signature_type=0) are NOT supported for gasless transactions
      • Email/Magic proxy wallets (signature_type=1)
      • Safe/Gnosis wallets (signature_type=2)
      • Deposit wallets (signature_type=3)
  • Setup and deployment

    • set approvals/disapprovals for all needed pUSD and conditional token spenders
      • Safe/Gnosis and Deposit wallet holders need to run deploy_safe_wallet() or deploy_deposit_wallet() respectively before using Safe transactions if the Safe has not been deployed yet
  • Balances

    • get POL balance by user address
    • get pUSD balance by user address
    • get token balance by token_id and user address
  • Transfers

    • transfer pUSD to another address with recipient address and amount
    • transfer token to another address with token_id, recipient address, and amount
  • Token/pUSD conversions

    • split pUSD into complementary tokens with condition_id, amount, and neg_risk
    • merge complementary tokens into pUSD with condition_id, amount, and neg_risk
    • redeem tokens into pUSD with condition_id, amounts array of [Yes shares, No shares], and neg_risk
    • convert one or more No tokens in a negative-risk event into pUSD plus Yes tokens on the other markets in the event
    • enable/disable auto-redeem
  • Miscellaneous

    • detect wallet signature type
    • get base/poly proxy/safe proxy/deposit wallet address from private_key

PolymarketWebsocketsClient

Real-time data subscriptions.

  • Market socket
    • subscribe with a token_ids list
    • receive order book summary, price change, tick size change, last trade price, best bid/ask price change, market created, and market resolved events
  • User socket
    • subscribe with ApiCreds
    • receive order events (live, canceled, matched)
    • receive trade events (matched, mined, confirmed, retrying, failed)
  • Live data socket
    • subscribe with any combination described here
    • receive comment/reaction events (created, removed)
    • receive trades / orders_matched events filtered by event slug or market slug
    • receive crypto/equity prices
    • receive RFQ events (created, edited, canceled, expired)
  • Sports socket
    • receive sports game snapshots for game start, score change, period change, and game end

PolymarketGraphQLClient / AsyncPolymarketGraphQLClient

Goldsky-hosted subgraph queries.

  • Endpoints
    • activity_subgraph
    • fpmm_subgraph
    • open_interest_subgraph
    • orderbook_subgraph
    • pnl_subgraph
    • positions_subgraph
    • sports_oracle_subgraph
    • wallet_subgraph
  • Queries
    • query() takes a GraphQL query string and returns the raw JSON

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pm_apis-2.1.0.post7.tar.gz (267.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pm_apis-2.1.0.post7-py3-none-any.whl (117.8 kB view details)

Uploaded Python 3

File details

Details for the file pm_apis-2.1.0.post7.tar.gz.

File metadata

  • Download URL: pm_apis-2.1.0.post7.tar.gz
  • Upload date:
  • Size: 267.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pm_apis-2.1.0.post7.tar.gz
Algorithm Hash digest
SHA256 11745f2ce771423ce3a4a83a719bc6617daea07bb432ffd22b4736e6ef02a6a7
MD5 d89b3ec1da35d5ef35521aa15b04703b
BLAKE2b-256 22436d7fd039c0da4ef55071892e43b0d2b07af7cdaadb929410c8b63ee32ac8

See more details on using hashes here.

Provenance

The following attestation bundles were made for pm_apis-2.1.0.post7.tar.gz:

Publisher: publish-pypi.yml on mileswangs/pm-apis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pm_apis-2.1.0.post7-py3-none-any.whl.

File metadata

  • Download URL: pm_apis-2.1.0.post7-py3-none-any.whl
  • Upload date:
  • Size: 117.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for pm_apis-2.1.0.post7-py3-none-any.whl
Algorithm Hash digest
SHA256 35b9a76e2a883fe8743ed1231bebfbfdfd39a4a5ca1095c5c510df231f77a29b
MD5 e4b79f44dcd84b37f457198bf53fbab4
BLAKE2b-256 3b46473e72801a2c18d8deaee8c56af6c33da680638768ddabbe4d1748810b79

See more details on using hashes here.

Provenance

The following attestation bundles were made for pm_apis-2.1.0.post7-py3-none-any.whl:

Publisher: publish-pypi.yml on mileswangs/pm-apis

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page