Build an Indexer from Scratch
This guide demonstrates how SDK packages can be combined into a working indexer (called a squid). This page goes through all the technical details to make the squid architecture easier to understand. If you want to get to a working indexer ASAP, bootstrap from a template.Prerequisites
Before you begin, ensure you have:- Node.js 20.x or newer - Download here
- Docker - Download here
Project: Polkadot Balance Transfers API
In this tutorial, you’ll build an indexer that tracks balance transfers on Polkadot, saves the data to PostgreSQL, and serves it as a GraphQL API.Required packages
From the requirements above, you’ll need these packages:@subsquid/substrate-processor- for retrieving Substrate chain data@subsquid/typeorm-store,@subsquid/typeorm-codegen, and@subsquid/typeorm-migration- for saving data to PostgreSQL
Optional packages
@subsquid/substrate-typegen- for generating type-safe wrappers for Substrate events and calls@subsquid/graphql-server/ OpenReader - for serving GraphQL API
Build the Indexer
Initialize the project
Create a new folder and initialize a Node.js project:Create a
.gitignore file:.gitignore
Define the data schema
Create a Generate TypeORM classes from the schema:
schema.graphql file to define your data model:schema.graphql
The TypeORM classes are now available at
src/model/index.ts.Set up the database
Create environment variables in a Create a Start the database:Compile the TypeORM classes:Generate and apply the database migration:
.env file:.env
docker-compose.yaml file for PostgreSQL:docker-compose.yaml
Generate type-safe wrappers
Create a Generate TypeScript type-safe wrappers:
typegen.json file to specify which Substrate types to generate:typegen.json
The type-safe wrappers are now available in
src/types.Run the processor
Compile the project:Start the processor:
The processor is now indexing balance transfers from the Polkadot blockchain.
Start the GraphQL server
Update your In a separate terminal, start the GraphQL server:
.env file to include the GraphQL server port:.env
The GraphQL API with GraphiQL is available at
localhost:4350/graphql.

