Skip to content

Instantly share code, notes, and snippets.

@elyase
Last active February 25, 2025 12:26
Show Gist options
  • Save elyase/4084c07802759cc54ad6ead9891d475d to your computer and use it in GitHub Desktop.
Save elyase/4084c07802759cc54ad6ead9891d475d to your computer and use it in GitHub Desktop.
Fetch orderbook data from polymarket
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "requests",
# ]
# ///
import requests
def main():
# Subgraph query to get active markets
query = """
query {
markets(where: {resolved: false}) {
id
outcomes {
id
token {
id
}
}
}
}
"""
print("Fetching active markets from subgraph...")
subgraph_response = requests.post("https://api.thegraph.com/subgraphs/name/Polymarket/public-polymarket-subgraph", json={"query": query})
data = subgraph_response.json()["data"]["markets"]
if not data:
print("No active markets found.")
return
first_market = data[0]
first_outcome = first_market["outcomes"][0]
token_id = first_outcome["token"]["id"]
print(f"Selected market ID: {first_market['id']}")
print(f"Selected token ID: {token_id}")
# Fetch order book from CLOBL API
print("Fetching order book from CLOBL API...")
order_book_response = requests.get(f"https://clob.polymarket.com/book?asset_id={token_id}")
if order_book_response.status_code == 200:
print("Order book fetched successfully:")
print(order_book_response.json())
else:
print(f"Failed to fetch order book: {order_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