Getting historical data on a smart contract can be frustrating when you’re building a dapp. The Graph provides an easy & decentralized option to query smart contract data through APIs known as subgraphs.  Its infrastructure relies on a decentralized network of indexers, enabling your dapp to achieve true decentralization.

These subgraphs only take a few minutes to set up. To get started, follow these three steps:

  1. Initialize your subgraph project
  2. Deploy & Publish
  3. Query from your dapp

Pricing: All developers receive 100K free queries per month on the decentralized network. After these free queries, you only pay based on usage at $4 for every 100K queries.

Here’s a step by step walk through:

1. Initialize your subgraph project

Create a subgraph on Subgraph Studio⁠

Go to the Subgraph Studio and connect your wallet. Once your wallet is connected, you can begin by clicking “Create a Subgraph”. When choosing a name for your subgraph, it is recommended to use Title Case: “Subgraph Name Chain Name.”

https://lh7-us.googleusercontent.com/docsz/AD_4nXf8OTdwMxlKQGKzIF_kYR7NPKeh9TmWnZBYxb7ft_YbdOdx_VVtbp6PslN7N1KGUzNpIDCmaXppdrllM1cw_J4L8Na03BXOWzJTK1POCve0nkRjQYgWJ60QHAdtQ4Niy83SMM8m0F0f-N-AJj4PDqDPlA5M?key=fnI6SyFgXU9SZRNX5C5vPQ

You will then land on your subgraph’s page. All the CLI commands you need will be visible on the right side of the page:

https://lh7-us.googleusercontent.com/docsz/AD_4nXe3YvCxiOH_LupSWe8zh9AmP-VrV4PlOq3f7Ix6hNlBUYcANUFuLuVIWR74OGiBs0nrugTyT0v3o6RPmTsgHONdv_ZJNWtcDWEkRntXPHlQGFcqmEBa-D6j4aoIPzUKYdOJMVUPu8O3fwjdZ4IaXXZoTzY?key=fnI6SyFgXU9SZRNX5C5vPQ

Install the Graph CLI⁠

On your local machine run the following:

npm install -g @graphprotocol/graph-cli

Initialize your Subgraph⁠

You can copy this directly from your subgraph page to include your specific subgraph slug:

graph init --studio <SUBGRAPH_SLUG>