Overview
In this section we will show you how to interact with network-specific APIs that are generated by the Polkadart CLI. These APIs are specific to the network you are connecting to and provide all the methods available on that network in a type-safe way.
Installation
Section titled “Installation”To have access to the network-specific APIs, you need to install the Polkadart CLI and generate the types for the network you are interested in. If you already did that on the Getting Started section, you can skip this step.
Add the following to your pubspec.yaml file:
dependencies: polkadart_cli: ^0.4.7Get the dependencies by running:
dart pub getGenerating the APIs
Section titled “Generating the APIs”Now let’s configure Polkadart CLI to generate the types for us, add the following configuration at the end of your pubspec file.
polkadart: output_dir: lib/generated chains: polkadot: wss://rpc.polkadot.io kusama: wss://kusama-rpc.polkadot.ioAnd finally let’s generate the types:
dart run polkadart_cli:generate -vYou will see that a folder lib/generated was created for you and it contains all APIs, types and extrinsics for the networks you specified.
We will provide the same example as the one in the Polkadart API section, but this time using the network-specific APIs.
Example: StateApi
Section titled “Example: StateApi”import 'package:demo/generated/polkadot/polkadot.dart';import 'package:polkadart/polkadart.dart' show Provider;
Future<void> main(List<String> arguments) async { final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io')); final polkadot = Polkadot(provider); final runtime = await polkadot.rpc.state.getRuntimeVersion();
print(runtime.toJson());}You will get the same output as the Polkadart API example on StateApi section:
{ specName: polkadot, implName: parity-polkadot, authoringVersion: 0, specVersion: 1003003, implVersion: 0, apis: [...], transactionVersion: 26, stateVersion: 1}Example: SystemApi
Section titled “Example: SystemApi”import 'package:polkadart/apis/apis.dart';import 'package:polkadart/polkadart.dart' show Provider;
void main() async { final provider = Provider.fromUri(Uri.parse('wss://rpc.polkadot.io')); final polkadot = Polkadot(provider); final chain = await polkadot.rpc.system.chain();
print(chain);}You will get the same output as the Polkadart API example on SystemApi section:
PolkadotNext steps
Section titled “Next steps”The following sections will provide more details on the available APIs for polkadot network. The APIs are generated based on a snapshot of the network in the time of writing, please keep in mind that the network may have changed since then.