Docs
subgraphs > Cookbook > Query Polymarket Data

Querying Blockchain Data from Polymarket with Subgraphs on The Graph

Reading time: 2 min

Query Polymarket’s onchain data using GraphQL via subgraphs on The Graph Network. Subgraphs are decentralized APIs powered by The Graph, a protocol for indexing & querying data from blockchains.

Polymarket Subgraph on Graph Explorer

Link to this section

You can see an interactive query playground on the Polymarket subgraph’s page on The Graph Explorer, where you can test any query.

Polymarket Playground

How to use the Visual Query Editor

Link to this section

The visual query editor helps you test sample queries from your subgraph.

You can use the GraphiQL Explorer to compose your GraphQL queries by clicking on the fields you want.

Example Query: Get the top 5 highest payouts from Polymarket

Link to this section
{
redemptions(orderBy: payout, orderDirection: desc, first: 5) {
payout
redeemer
id
timestamp
}
}

Example output

Link to this section
{
"data": {
"redemptions": [
{
"id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b",
"payout": "6274509531681",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1722929672"
},
{
"id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7",
"payout": "2246253575996",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1726701528"
},
{
"id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26",
"payout": "2135448291991",
"redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689",
"timestamp": "1704932625"
},
{
"id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa",
"payout": "1917395333835",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1726701528"
},
{
"id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30",
"payout": "1862505580000",
"redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",
"timestamp": "1722929866"
}
]
}
}

Polymarket's GraphQL Schema

Link to this section

The schema for this subgraph is defined here in Polymarket’s GitHub.

Polymarket Subgraph Endpoint

Link to this section

https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp

The Polymarket Subgraph endpoint is available on Graph Explorer.

Polymarket Endpoint

How to Get your own API Key

Link to this section
  1. Go to https://thegraph.com/studio and connect your wallet
  2. Go to https://thegraph.com/studio/apikeys/ to create an API key

You can use this API key on any subgraph in Graph Explorer, and it’s not limited to just Polymarket.

100k queries per month are free which is perfect for your side project!

Additional Polymarket Subgraphs

Link to this section
  • Polymarket
  • Polymarket Activity Polygon
  • Polymarket Profit & Loss
  • Polymarket Open Interest

How to Query with the API

Link to this section

You can pass any GraphQL query to the Polymarket endpoint and receive data in json format.

This following code example will return the exact same output as above.

Sample Code from node.js

Link to this section
const axios = require('axios');
const graphqlQuery = `{
positions(first: 5) {
condition
outcomeIndex
}
};
const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp'
const graphQLRequest = {
method: 'post',
url: queryUrl,
data: {
query: graphqlQuery,
},
};
// Send the GraphQL query
axios(graphQLRequest)
.then((response) => {
// Handle the response here
const data = response.data.data
console.log(data)
})
.catch((error) => {
// Handle any errors
console.error(error);
});

Additional resources

Link to this section

For more information about querying data from your subgraph, read more here.

To explore all the ways you can optimize & customize your subgraph for a better performance, read more about creating a subgraph here.

Edit page

Previous
How to Secure API Keys Using Next.js Server Components
Next
Quick Start
Edit page