Docs
Search⌘ K
  • Home
  • حول The Graph
  • الشبكات المدعومة
  • Protocol Contracts
  • Subgraphs
    • متعدد-السلاسل
      • Token API
        • Hypergraph
          • AI Suite
            • Indexing
              • Resources
                Subgraphs > How-to Guides

                2 minutes

                Querying Blockchain Data from Polymarket with Subgraphs on The Graph

                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

                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

                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

                1{2  redemptions(orderBy: payout, orderDirection: desc, first: 5) {3    payout4    redeemer5    id6    timestamp7  }8}

                Example output

                1{2  "data": {3    "redemptions": [4      {5        "id": "0x8fbb68b7c0cbe9aca6024d063a843a23d046b5522270fd25c6a81c511cf517d1_0x3b",6        "payout": "6274509531681",7        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",8        "timestamp": "1722929672"9      },10      {11        "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0x7",12        "payout": "2246253575996",13        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",14        "timestamp": "1726701528"15      },16      {17        "id": "0x983b71c64b5075fc1179f4e03849af9c727be60de71c9e86e37ad0b3e43f9db9_0x26",18        "payout": "2135448291991",19        "redeemer": "0x5a181dcf3eb53a09fb32b20a5a9312fb8d26f689",20        "timestamp": "1704932625"21      },22      {23        "id": "0x2b2826448fcacde7931828cfcd3cc4aaeac8080fdff1e91363f0589c9b503eca_0xa",24        "payout": "1917395333835",25        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",26        "timestamp": "1726701528"27      },28      {29        "id": "0xfe82e117201f5169abc822281ccf0469e6b3740fcb4e799d1b599f83b8f11656_0x30",30        "payout": "1862505580000",31        "redeemer": "0xfffe4013adfe325c6e02d36dc66e091f5476f52c",32        "timestamp": "1722929866"33      }34    ]35  }36}

                Polymarket’s GraphQL Schema

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

                Polymarket Subgraph Endpoint

                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

                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

                • Polymarket
                • Polymarket Activity Polygon
                • Polymarket Profit & Loss
                • Polymarket Open Interest

                How to Query with the API

                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

                1const axios = require('axios');23const graphqlQuery = `{4  positions(first: 5) {5    condition6    outcomeIndex7  }8};910const queryUrl = 'https://gateway.thegraph.com/api/{api-key}/subgraphs/id/Bx1W4S7kDVxs9gC3s2G6DS8kdNBJNVhMviCtin2DiBp'1112const graphQLRequest = {13  method: 'post',14  url: queryUrl,15  data: {16    query: graphqlQuery,17  },18};1920// Send the GraphQL query21axios(graphQLRequest)22  .then((response) => {23    // Handle the response here24    const data = response.data.data25    console.log(data)2627  })28  .catch((error) => {29    // Handle any errors30    console.error(error);31  });

                Additional resources

                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 on GitHub⁠

                How to Secure API Keys Using Next.js Server ComponentsSmart Contract Analysis with Cana CLI
                On this page
                • Polymarket Subgraph on Graph Explorer
                • How to use the Visual Query Editor
                • Example Query: Get the top 5 highest payouts from Polymarket
                • Example output
                • Polymarket’s GraphQL Schema
                • Polymarket Subgraph Endpoint
                • How to Get your own API Key
                • Additional Polymarket Subgraphs
                • How to Query with the API
                • Sample Code from node.js
                • Additional resources
                The GraphStatusTestnetBrand AssetsForumSecurityPrivacy PolicyTerms of Service