Last active
August 14, 2025 12:04
-
-
Save elyase/4084c07802759cc54ad6ead9891d475d to your computer and use it in GitHub Desktop.
Fetch orderbook data from polymarket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /// script | |
# requires-python = ">=3.12" | |
# dependencies = [ | |
# "requests" | |
# ] | |
# /// | |
import requests | |
import json | |
GAMMA_API_URL = "https://gamma-api.polymarket.com" | |
CLOB_URL = "https://clob.polymarket.com" | |
def main(): | |
print("Fetching markets directly from CLOB API...") | |
# Get markets from CLOB API, filtering for open markets with order books | |
clob_markets_url = f"{CLOB_URL}/markets" | |
params = { | |
"closed": False, | |
"limit": 100 | |
} | |
clob_response = requests.get(clob_markets_url, params=params) | |
if clob_response.status_code != 200: | |
print(f"Error fetching markets from CLOB API: {clob_response.status_code}") | |
print(clob_response.text) | |
return | |
clob_markets_data = clob_response.json() | |
print(f"CLOB markets response structure: {json.dumps(clob_markets_data, indent=2)[:800]}...") | |
# Extract markets from the data field | |
if isinstance(clob_markets_data, dict) and 'data' in clob_markets_data: | |
clob_markets = clob_markets_data['data'] | |
else: | |
clob_markets = clob_markets_data | |
if not clob_markets: | |
print("No markets found in CLOB API") | |
return | |
# Find a market with tokens and active order book | |
market = None | |
working_token_id = None | |
for m in clob_markets: | |
# Skip if m is not a dict | |
if not isinstance(m, dict): | |
continue | |
# Only look at markets with order books enabled | |
if not m.get('enable_order_book', False): | |
continue | |
# Only look at open markets that accept orders | |
if m.get('closed', True) or not m.get('accepting_orders', False): | |
continue | |
print(f"Checking market: {m.get('question', 'Unknown')}") | |
tokens = m.get('tokens', []) | |
if tokens: | |
# Try to find a token with an active order book | |
for token in tokens: | |
token_id = token.get('token_id') | |
if token_id: | |
# Test if this token has an order book | |
book_url = f"{CLOB_URL}/book" | |
params = {"token_id": token_id} | |
test_response = requests.get(book_url, params=params) | |
if test_response.status_code == 200: | |
market = m | |
working_token_id = token_id | |
print(f"Found working token ID: {token_id}") | |
break | |
if market: | |
break | |
if not market: | |
print("No markets with tokens found in CLOB API") | |
# Let's just use the first market and try to use its condition_id | |
first_market = clob_markets[0] | |
if not isinstance(first_market, dict): | |
print("First market is not a dictionary") | |
return | |
print(f"Using first market: {first_market.get('question', 'Unknown')}") | |
condition_id = first_market.get('condition_id') | |
if not condition_id: | |
print("No condition_id found in market") | |
return | |
# Try to get tokens by looking for them using the condition_id | |
# For a binary market, tokens are typically derived from the condition_id | |
print(f"Condition ID: {condition_id}") | |
# For now, let's try to construct a token ID or find tokens | |
# We'll use a simple approach - just try to get order book for the first outcome | |
# This might require trying common token ID patterns | |
print("Trying to get detailed market info from Gamma API...") | |
# Try to get market tokens from Gamma API using condition_id | |
gamma_markets_url = f"{GAMMA_API_URL}/markets" | |
gamma_response = requests.get(gamma_markets_url, params={"limit": 100, "active": True}) | |
if gamma_response.status_code == 200: | |
gamma_markets = gamma_response.json() | |
# Look for a market with matching condition_id in Gamma API | |
matching_gamma_market = None | |
for gamma_market in gamma_markets: | |
if gamma_market.get('conditionId') == condition_id: | |
matching_gamma_market = gamma_market | |
break | |
if matching_gamma_market: | |
print(f"Found matching market in Gamma API: {matching_gamma_market.get('question')}") | |
# Get detailed market info | |
market_id = matching_gamma_market['id'] | |
detail_url = f"{GAMMA_API_URL}/markets/{market_id}" | |
detail_response = requests.get(detail_url) | |
if detail_response.status_code == 200: | |
detailed_market = detail_response.json() | |
print(f"Detailed market: {json.dumps(detailed_market, indent=2)[:1000]}...") | |
# Look for tokens in the detailed response | |
tokens = detailed_market.get('tokens', []) | |
if tokens: | |
token = tokens[0] | |
token_id = token.get('token_id') | |
print(f"Found token ID from Gamma: {token_id}") | |
# Try the order book with this token | |
book_url = f"{CLOB_URL}/book" | |
params = {"token_id": token_id} | |
book_response = requests.get(book_url, params=params) | |
if book_response.status_code == 200: | |
book_data = book_response.json() | |
print("Order book:") | |
print(json.dumps(book_data, indent=2)) | |
else: | |
print(f"Error fetching order book with Gamma token: {book_response.status_code}") | |
print(book_response.text) | |
return | |
else: | |
print("No tokens found in detailed Gamma market") | |
else: | |
print(f"Error fetching detailed market from Gamma: {detail_response.status_code}") | |
else: | |
print("No matching market found in Gamma API") | |
print("Trying to get order book using condition_id directly...") | |
# Get order book using condition_id (this might not work but let's try) | |
book_url = f"{CLOB_URL}/book" | |
params = {"token_id": condition_id} | |
book_response = requests.get(book_url, params=params) | |
if book_response.status_code == 200: | |
book_data = book_response.json() | |
print("Order book (using condition_id):") | |
print(json.dumps(book_data, indent=2)) | |
else: | |
print(f"Error fetching order book with condition_id: {book_response.status_code}") | |
print(book_response.text) | |
return | |
print(f"\nSelected market: {market.get('question', 'Unknown')}") | |
# Find the token that matches our working_token_id | |
working_token = None | |
tokens = market.get('tokens', []) | |
for token in tokens: | |
if token.get('token_id') == working_token_id: | |
working_token = token | |
break | |
if not working_token: | |
print("Could not find the working token in market data") | |
return | |
print(f"Token: {working_token.get('outcome', 'Unknown outcome')}") | |
print(f"Token ID: {working_token_id}") | |
print(f"Getting order book for token {working_token_id}...") | |
# Get order book (we already tested this works) | |
book_url = f"{CLOB_URL}/book" | |
params = {"token_id": working_token_id} | |
book_response = requests.get(book_url, params=params) | |
if book_response.status_code == 200: | |
book_data = book_response.json() | |
print("Order book:") | |
print(json.dumps(book_data, indent=2)) | |
else: | |
print(f"Error fetching order book: {book_response.status_code}") | |
print(book_response.text) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment