Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,20 @@ See [this Python example](https://gist.github.com/poly-rodr/44313920481de58d5a3f

**Pro tip**: You only need to set these once per wallet. After that, you can trade freely.

## Examples

The `examples/` directory contains minimal Python scripts demonstrating common
CLOB workflows:

- `fetch_markets.py` – fetch active markets
- `get_orderbook.py` – fetch a single orderbook by token_id
- `get_orderbooks.py` – fetch multiple orderbooks in one request
- `get_orders.py` – fetch open orders for a market (requires API credentials)

These scripts are intentionally minimal and designed for quick experimentation.

## Notes
- To discover token IDs, use the Markets API Explorer: [Get Markets](https://docs.polymarket.com/developers/gamma-markets-api/get-markets).
- Prices are in dollars from 0.00 to 1.00. Shares are whole or fractional units of the outcome token.

See [/example](/examples) for more.
See [/example](/examples) for more.
34 changes: 34 additions & 0 deletions examples/fetch_markets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from py_clob_client.client import ClobClient
from py_clob_client.constants import POLYMARKET_HOST
from py_clob_client.credentials import Credentials


def main():
creds = Credentials(
api_key="YOUR_API_KEY",
api_secret="YOUR_API_SECRET",
api_passphrase="YOUR_API_PASSPHRASE",
)

client = ClobClient(
host=POLYMARKET_HOST,
key=creds.api_key,
creds=creds,
)

next_cursor = None

while True:
response = client.get_markets(next_cursor=next_cursor)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Passing None as cursor creates invalid API URL

Medium Severity

The pagination loop initializes next_cursor to None and passes it explicitly to client.get_markets(next_cursor=next_cursor). Since get_markets doesn't guard against None like other methods do, None gets string-interpolated into the URL as the literal "None", creating an invalid request like ?next_cursor=None. The first iteration will fail or return unexpected results. The fix is to either omit next_cursor on the first call or initialize it to "MA==".

Fix in Cursor Fix in Web


markets = response.get("data", [])
for market in markets:
print(market.get("question"))

next_cursor = response.get("next_cursor")
if not next_cursor:
break


if __name__ == "__main__":
main()
10 changes: 9 additions & 1 deletion examples/get_orderbook.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
"""
Example: Fetch a single orderbook by token_id

Requires:
- CLOB_API_URL (optional, defaults to Polymarket public endpoint)
"""

from py_clob_client.client import ClobClient
import os
from dotenv import load_dotenv
Expand All @@ -18,4 +25,5 @@ def main():
print("orderbook hash", hash)


main()
if __name__ == "__main__":
main()
11 changes: 10 additions & 1 deletion examples/get_orderbooks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
"""
Example: Fetch multiple orderbooks by token_id list

Requires:
- CLOB_API_URL (optional, defaults to Polymarket public endpoint)
"""

import os
from py_clob_client.client import ClobClient
from py_clob_client.clob_types import BookParams

Expand All @@ -20,4 +28,5 @@ def main():
print("Done!")


main()
if __name__ == "__main__":
main()
14 changes: 13 additions & 1 deletion examples/get_orders.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
"""
Example: Fetch open orders for a given market

Requires:
- CLOB_API_URL
- PK
- CLOB_API_KEY
- CLOB_SECRET
- CLOB_PASS_PHRASE
"""

import os

from py_clob_client.client import ClobClient
Expand Down Expand Up @@ -28,4 +39,5 @@ def main():
print("Done!")


main()
if __name__ == "__main__":
main()