Cover Image for Eating blockchain data using GraphQL spoon

Eating blockchain data using GraphQL spoon

GraphQL Tutorials

In this article, we will talk about how to get blockchain data (on-chain data) using Bitquery GraphQL APIs and why GraphQL is a better fit for this job than REST.

Note: The article is focused on developers and engineering teams.

Data problems in the Blockchain space

Blockchains only emit transactions and events. You can run a blockchain node, consume this raw data, store it, and then query it based on your application needs.

However, if you are a developer, you just want to focus on your application. Running nodes, then storing, indexing, and maintaining the database is overhead. This extra effort kills many blockchain ideas every day.

Many blockchain data providers offer simple APIs to consume blockchain data. Most of them are REST APIs. REST is good enough to get a job done, but it is not the best choice for complex blockchain data.

In this article, I will argue why GraphQL APIs are better for building complex blockchain products. If you already know GraphQL, get started with the first Blockchain GraphQL query.

What is GraphQL?

GraphQL is a query language for APIs. It describes how to ask for data, and it lets you specify your exact data needs.

GraphQL beats REST APIs for several reasons.

Get exactly what you ask for

With GraphQL, you get what you describe in your queries. Nothing more, nothing less. This reduces payload size and improves performance.

Let’s take an example where you just need the latest block number, nothing else.

In a REST API you get an endpoint like /get/latestblock, and it returns every detail of the latest block, even if you only need the number.

With GraphQL you write something like this.

Note: You can test these queries in the Bitquery IDE. The API endpoint is https://streaming.bitquery.io/graphql, and requests are authorized with an OAuth access token sent as an Authorization: Bearer <token> header. You can generate a token from your Bitquery account. See the authorization docs for details.

{
  EVM(network: eth) {
    Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
      Block {
        Number
      }
    }
  }
}

And you get the following result:

{
  "data": {
    "EVM": {
      "Blocks": [
        {
          "Block": {
            "Number": "23514392"
          }
        }
      ]
    }
  }
}

Let’s say you also want the transaction count of that block. Add TxCount to the query.

{
  EVM(network: eth) {
    Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
      Block {
        Number
        TxCount
      }
    }
  }
}

Run the query above and you get:

{
  "data": {
    "EVM": {
      "Blocks": [
        {
          "Block": {
            "Number": "23514392",
            "TxCount": 142
          }
        }
      ]
    }
  }
}

Did you see it? We are only getting what we need. Nothing more, nothing less.

One request, many resources

Let’s say you want the latest block on both Ethereum and BNB Chain. In a REST API you would call two endpoints. With GraphQL, you write a single query using aliases and get data for both networks in one request.

{
  ethereum: EVM(network: eth) {
    Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
      Block {
        Number
        Time
      }
    }
  }
  bsc: EVM(network: bsc) {
    Blocks(limit: {count: 1}, orderBy: {descending: Block_Number}) {
      Block {
        Number
        Time
      }
    }
  }
}

One network round trip instead of two. This is a real performance win, and it also makes applications easier to build and extend.

Single endpoint for every request

Unlike REST, where you manage multiple resource endpoints, GraphQL gives you a single endpoint for all queries. Bitquery's endpoint is https://streaming.bitquery.io/graphql. A second endpoint, https://streaming.bitquery.io/eap, serves some chains that are on the Early Access Program.

Real-time data with subscriptions

GraphQL is not limited to request-response. The same queries can run as subscriptions over WebSocket: change the query keyword to subscription and you receive new trades, transfers, or blocks as they happen. For heavier pipelines, Bitquery also offers Kafka and gRPC streams that push raw data with lower latency than any polling setup.

Scale, documentation, integration, and readability

GraphQL scales well across data sources. Bitquery currently supports more than 40 blockchains, and you query all of them through the same endpoint and schema conventions. GraphQL also generates schema documentation automatically, which makes API integration easier: the IDE lets you browse the schema, autocomplete fields, and run queries in place.

The descriptive query syntax gives you better code readability too. Your query reads like the shape of the response.

There are trade-offs. Browser caching does not work the way it does with REST, because everything goes through one endpoint. Error handling is also different from REST. You can read more about why GraphQL is better than REST APIs.

Also, Read

About Bitquery

Bitquery provides blockchain data APIs and streams across more than 40 chains. The current lineup includes GraphQL APIs and subscriptions for historical and real-time data, Kafka and gRPC data streams for low-latency pipelines, an MCP server that lets AI agents query on-chain data directly, and Coinpath MoneyFlow for fund-flow tracing and investigations.

If you have any questions about our products, ask them on our Telegram channel. Also, subscribe to our newsletter below, we will keep you updated with the latest in the cryptocurrency world.

Subscribe to our newsletter

Subscribe and never miss any updates related to our APIs, new developments & latest news etc. Our newsletter is sent once a week on Monday.