GraphQL Queries on Ethereum Nodes

Learn how to use GraphQL to query read-only data from your Ethereum node.

GraphQL endpoints allow users to query read-only data from their Ethereum node using a query language. This provides a more standardized output compared to JSON-RPC endpoints. You can gather various types of data, including:

  • Blocks
  • Pending transactions
  • Transactions
  • Logs
  • Gas price
  • Protocol version
  • Sync status
  • Chain ID

For details on specific subfields available in each schema, refer to the schema section of this document.

How to access GraphQL on my Ethereum node

Once you spin up a node with Blockdaemon, you automatically have access to GraphQL on your node. You will need to make calls to a specific endpoint using your node url e.g: https://<NODE XID>.bdnodes.net/graphql

# Try it now - enter a block number to get the block hash, number of transactions on the block, and the block timestamp.

curl -X POST 'https\://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: <AUTH TOKEN>' --data-raw '{"query":"{block(number: ######){hash transactionCount timestamp}}"

πŸ“˜

Note:

To ensure you are getting accurate and up-to-date information, ensure your node is fully synced before running your queries.

You also now have the option to switch to display WebSocket or JSON RPC endpoints:

How To Query a Node

You can run queries against your node using an API tool like Postman or with simple curl commands. When running queries, you will need to have your node url and your auth token in order to make queries.

Using Postman to Query a Node

Postman is an API tool that can make GraphQL requests on your node. Below you will find steps on how to create your first request:

  1. Create a new collection for your requests (optional)
  2. Create a new request
  3. Set your request as POST
  4. In the request url textbox, enter your node url e.g. https://<NODE XID>.bdnodes.net/graphql
  5. In the Headers section, create a new key-value pair
    1. Key -> X-Auth-Token
    2. Value -> <YOUR AUTH TOKEN>
  6. In the Body section, make sure GraphQL is selected
  7. In the Query section entry, your query statement
  8. Click send, and your resulting data will be in the response section.

Using cURL to Query a Node

You can access GraphQL on your Ethereum node using the terminal. You will need your node url and your auth token to make calls. You will hit the same node url endpoint but change your data payload with new queries. Below you will find some examples of curl commands with different queries. Ensure to set subfields in your queries in order to see the data you want.

You can query a specific block for the following information with the query below:

  • Block hex number
  • Block hash
  • Parent block hash and hex number
  • Transaction on block
  • Gas used on block
  • Timestamp of block
curl -X POST 'https\://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: \<AUTH_TOKEN>' --data-raw '{"query":"{block(number: 12345){number hash parent{number hash} transactionCount gasUsed timestamp}}"}'

You can query a specific transaction for the following information with the query below:

  • From address
  • To address
  • Value
  • Gas
  • Block hash where the transaction occurred
curl -X POST 'https://<NODE XID>.bdnodes.net/graphql' -H 'Content-Type: application/json' -H 'X-Auth-Token: <AUTH_TOKEN>' --data-raw '{"query":"{transaction( hash: \"0x0cd5e04fa2c421c0675fde354063dc25ae43decc970ff8f05d417d0a349e4970\"){index from{address} to{address} value gasPrice gas status block{hash}}}"}'

Reference Material

Above, you see a few examples of certain queries you can run on your Ethereum node. Below you can find links to additional information. Note in some of these examples, not all queries will work on your node, but you can get a general understanding of how to use GraphQL:

πŸ‘‹ Need Help?

Contact us through email or our support page for any issues, bugs, or assistance you may need.