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.
You can see an interactive query playground on the , where you can test any query.
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.
{redemptions(orderBy: payout, orderDirection: desc, first: 5) {payoutredeemeridtimestamp}}
{"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"}]}}
The schema for this subgraph is defined .
The Polymarket Subgraph endpoint is available on .
You can use this API key on any subgraph in , and it’s not limited to just Polymarket.
100k queries per month are free which is perfect for your side project!
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.
const axios = require('axios');const graphqlQuery = `{positions(first: 5) {conditionoutcomeIndex}};const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp'const graphQLRequest = {method: 'post',url: queryUrl,data: {query: graphqlQuery,},};// Send the GraphQL queryaxios(graphQLRequest).then((response) => {// Handle the response hereconst data = response.data.dataconsole.log(data)}).catch((error) => {// Handle any errorsconsole.error(error);});
For more information about querying data from your subgraph, read more .
To explore all the ways you can optimize & customize your subgraph for a better performance, read more about .