|
| 1 | +--- |
| 2 | +title: Querying Blockchain Data from Polymarket with Subgraphs on The Graph |
| 3 | +--- |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +## Polymarket Subgraph on Graph Explorer |
| 8 | + |
| 9 | +You can see an interactive query playground on the [Polymarket subgraph’s page on The Graph Explorer](https://thegraph.com/explorer/subgraphs/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp?view=Query&chain=arbitrum-one), where you can test any query. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +## How to use the Visual Query Editor |
| 14 | + |
| 15 | +The visual query editor helps you test sample queries from your subgraph. |
| 16 | + |
| 17 | +You can use the GraphiQL Explorer to compose your GraphQL queries by clicking on the fields you want. |
| 18 | + |
| 19 | +### Example Query: Get the top 5 highest payouts from Polymarket |
| 20 | + |
| 21 | +``` |
| 22 | +{ |
| 23 | + redemptions(orderBy: payout, orderDirection: desc, first: 5) { |
| 24 | + payout |
| 25 | + redeemer |
| 26 | + id |
| 27 | + timestamp |
| 28 | + } |
| 29 | +} |
| 30 | +``` |
| 31 | + |
| 32 | +### Example output |
| 33 | + |
| 34 | +``` |
| 35 | +{ |
| 36 | + "data": { |
| 37 | + "redemptions": [ |
| 38 | + { |
| 39 | + "id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b", |
| 40 | + "payout": "6274509531681", |
| 41 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 42 | + "timestamp": "1722929672" |
| 43 | + }, |
| 44 | + { |
| 45 | + "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7", |
| 46 | + "payout": "2246253575996", |
| 47 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 48 | + "timestamp": "1726701528" |
| 49 | + }, |
| 50 | + { |
| 51 | + "id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26", |
| 52 | + "payout": "2135448291991", |
| 53 | + "redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689", |
| 54 | + "timestamp": "1704932625" |
| 55 | + }, |
| 56 | + { |
| 57 | + "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa", |
| 58 | + "payout": "1917395333835", |
| 59 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 60 | + "timestamp": "1726701528" |
| 61 | + }, |
| 62 | + { |
| 63 | + "id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30", |
| 64 | + "payout": "1862505580000", |
| 65 | + "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c", |
| 66 | + "timestamp": "1722929866" |
| 67 | + } |
| 68 | + ] |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +## Polymarket's GraphQL Schema |
| 74 | + |
| 75 | +The schema for this subgraph is defined [here in Polymarket’s GitHub](https://github.com/Polymarket/polymarket-subgraph/blob/main/polymarket-subgraph/schema.graphql). |
| 76 | + |
| 77 | +### Polymarket Subgraph Endpoint |
| 78 | + |
| 79 | +https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp |
| 80 | + |
| 81 | +The Polymarket Subgraph endpoint is available on [Graph Explorer](https://thegraph.com/explorer). |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +## How to Get your own API Key |
| 86 | + |
| 87 | +1. Go to [https://thegraph.com/studio](http://thegraph.com/studio) and connect your wallet |
| 88 | +2. Go to https://thegraph.com/studio/apikeys/ to create an API key |
| 89 | + |
| 90 | +You can use this API key on any subgraph in [Graph Explorer](https://thegraph.com/explorer), and it’s not limited to just Polymarket. |
| 91 | + |
| 92 | +100k queries per month are free which is perfect for your side project! |
| 93 | + |
| 94 | +## Additional Polymarket Subgraphs |
| 95 | + |
| 96 | +- [Polymarket](https://thegraph.com/explorer/subgraphs/81Dm16JjuFSrqz813HysXoUPvzTwE7fsfPk2RTf66nyC?view=Query&chain=arbitrum-one) |
| 97 | +- [Polymarket Activity Polygon](https://thegraph.com/explorer/subgraphs/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp?view=Query&chain=arbitrum-one) |
| 98 | +- [Polymarket Profit & Loss](https://thegraph.com/explorer/subgraphs/6c58N5U4MtQE2Y8njfVrrAfRykzfqajMGeTMEvMmskVz?view=Query&chain=arbitrum-one) |
| 99 | +- [Polymarket Open Interest](https://thegraph.com/explorer/subgraphs/ELaW6RtkbmYNmMMU6hEPsghG9Ko3EXSmiRkH855M4qfF?view=Query&chain=arbitrum-one) |
| 100 | + |
| 101 | +## How to Query with the API |
| 102 | + |
| 103 | +You can pass any GraphQL query to the Polymarket endpoint and receive data in json format. |
| 104 | + |
| 105 | +This following code example will return the exact same output as above. |
| 106 | + |
| 107 | +### Sample Code from node.js |
| 108 | + |
| 109 | +``` |
| 110 | +const axios = require('axios'); |
| 111 | +
|
| 112 | +const graphqlQuery = `{ |
| 113 | + positions(first: 5) { |
| 114 | + condition |
| 115 | + outcomeIndex |
| 116 | + } |
| 117 | +}; |
| 118 | +
|
| 119 | +const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp' |
| 120 | +
|
| 121 | +const graphQLRequest = { |
| 122 | + method: 'post', |
| 123 | + url: queryUrl, |
| 124 | + data: { |
| 125 | + query: graphqlQuery, |
| 126 | + }, |
| 127 | +}; |
| 128 | +
|
| 129 | +// Send the GraphQL query |
| 130 | +axios(graphQLRequest) |
| 131 | + .then((response) => { |
| 132 | + // Handle the response here |
| 133 | + const data = response.data.data |
| 134 | + console.log(data) |
| 135 | +
|
| 136 | + }) |
| 137 | + .catch((error) => { |
| 138 | + // Handle any errors |
| 139 | + console.error(error); |
| 140 | + }); |
| 141 | +``` |
| 142 | + |
| 143 | +### Additional resources |
| 144 | + |
| 145 | +For more information about querying data from your subgraph, read more [here](/subgraphs/querying/introduction/). |
| 146 | + |
| 147 | +To explore all the ways you can optimize & customize your subgraph for a better performance, read more about [creating a subgraph here](/developing/creating-a-subgraph/). |
0 commit comments