Polkadart Logo
Getting Started

Type Generator

Generate type-safe APIs from blockchain metadata for any Polkadot or Substrate-based chain

Type generation is a crucial part of working with Polkadot and Substrate-based chains. Each network has its own unique set of types, APIs, and extrinsics. Polkadart's CLI tool automatically generates these types from chain metadata, providing:

  • Type safety - Catch errors at compile time
  • 💡 Auto-completion - IDE support for all chain-specific APIs
  • 🔄 Always up-to-date - Regenerate when chains upgrade
  • 🎯 Chain-specific - Accurate types for each network

Configuration

Setting Up Type Generation

Add the following configuration to your pubspec.yaml file:

pubspec.yaml
polkadart:
  output_dir: lib/generated      # Where to generate the code
  chains:
    polkadot: wss://rpc.polkadot.io           # Polkadot mainnet
    kusama: wss://kusama-rpc.polkadot.io      # Kusama canary network
    # Add more chains as needed:
    # moonbeam: wss://wss.api.moonbeam.network
    # acala: wss://acala-rpc.dwellir.com
You can add any Substrate-based chain by providing its WebSocket RPC endpoint.

Generating Types

Run the generator command:

dart run polkadart_cli:generate -v

The -v flag enables verbose output to see the generation progress.

Check the generated files:

A lib/generated folder will be created with a subfolder for each chain:

lib/
└── generated/
    ├── polkadot/
    │   ├── polkadot.dart        # Main entry point
    │   ├── types/               # Chain-specific types
    │   ├── pallets/             # Pallet APIs
    │   └── extrinsics/          # Transaction builders
    └── kusama/
        └── ...                 # Similar structure

Import and use the generated code:

import 'package:your_app/generated/polkadot/polkadot.dart';

Advanced Configuration

Custom Output Directory

pubspec.yaml
polkadart:
  output_dir: lib/blockchain  # Custom output path

Using Environment Variables

For security, you can use environment variables for RPC endpoints:

pubspec.yaml
polkadart:
  output_dir: lib/generated
  chains:
    polkadot: ${POLKADOT_RPC_URL}
    kusama: ${KUSAMA_RPC_URL}

Updating Generated Types

When a chain performs a runtime upgrade, regenerate the types:

# Clean old generated files
rm -rf lib/generated

# Regenerate with latest metadata
dart run polkadart_cli:generate -v
Always regenerate types after chain runtime upgrades to ensure compatibility.

Next Steps