Skip to main content

Starknet Quickstart

This 5-minute tutorial shows you how to build a Starknet indexer using Squid SDK. You’ll create a complete blockchain indexer that fetches, decodes, and serves Starknet transaction and event data.

What you’ll build

Your Starknet indexer (squid) will:
  • Fetch historical Starknet transactions and events from the SQD Network
  • Decode Cairo contract events and function calls
  • Save the data to a local PostgreSQL database
  • Start a GraphQL server with a rich API to query the indexed data
This tutorial focuses on Starknet. For other chains, see the EVM Quickstart or Substrate Quickstart.

Prerequisites

Before you begin, ensure you have:
1

Install Squid CLI

Install the Squid CLI globally:
npm i -g @subsquid/cli
Verify installation by running sqd --version
Squid CLI is a multi-purpose utility tool for scaffolding and managing indexers, both locally and in SQD Cloud.
2

Scaffold the indexer project

Create a new Starknet squid project:
sqd init hello-starknet-squid -t starknet
cd hello-starknet-squid
The template provides a pre-configured project structure for indexing Starknet transactions and Cairo contract events.
3

Inspect the project structure

Explore the ./src folder:
src/
├── main.ts
├── processor.ts
└── model
    ├── generated
   ├── index.ts
   └── event.model.ts
    └── index.ts
Key files explained: - src/model/ - TypeORM model classes auto-generated from schema.graphql for database operations - processor.ts - Processor configuration for data retrieval - main.ts - Main executable containing processing logic
The processor configuration for Starknet:
processor.ts
export const processor = new StarknetBatchProcessor()
  // SQD Network gateway for Starknet
  .setGateway("https://v2.archive.subsquid.io/network/starknet-mainnet")
  // Optional RPC endpoint for real-time data
  .setRpcEndpoint("<starknet-rpc-endpoint>")
  // Configure block range
  .setBlockRange({ from: 100000 })
  // Add events to index
  .addEvent({
    address: ["<contract-address>"],
    keys: ["<event-key>"],
  })
  // Select fields to retrieve
  .setFields({
    event: {
      keys: true,
      data: true,
    },
    transaction: {
      hash: true,
    },
  });
The data processing and storage logic:
main.ts
processor.run(new TypeormDatabase({ supportHotBlocks: true }), async (ctx) => {
  const events = [];

  for (let block of ctx.blocks) {
    for (let event of block.events) {
      // Decode Cairo contract event
      const decodedEvent = decodeEvent(event);

      events.push(
        new Event({
          id: event.id,
          blockNumber: block.header.height,
          timestamp: new Date(block.header.timestamp),
          transactionHash: event.transactionHash,
          contractAddress: event.address,
          keys: event.keys,
          data: decodedEvent.data,
        })
      );
    }
  }

  // Batch insert all events
  await ctx.store.save(events);
});
4

Install dependencies and build

Install dependencies and build the project:
npm i
npm run build
Verify the build completed successfully by checking for the lib/ directory.
5

Start the database and processor

The processor continuously fetches data, decodes it, and stores it in PostgreSQL. All logic is defined in main.ts and is fully customizable.First, start a local PostgreSQL database (the template includes a Docker Compose file):
docker compose up -d
The processor connects to PostgreSQL using connection parameters from .env. Ensure the database is running before proceeding.
Apply database migrations:
npx squid-typeorm-migration apply
Then start the processor:
node -r dotenv/config lib/main.js
The indexer is now running and will begin processing Starknet blocks.
6

Start the GraphQL API

Start the GraphQL API to serve the indexed data:
npx squid-graphql-server
The GraphQL playground is available at localhost:4350/graphql
7

Query the data

You can now query your indexed Starknet data! Try this example query in the GraphQL playground:
{
  events(limit: 10) {
    id
    blockNumber
    timestamp
    contractAddress
    transactionHash
  }
}
Congratulations! You’ve successfully built and deployed your first Starknet blockchain indexer.