Career, Tech Knowledge, Web3

Web 3.0 development with Substrate. Part 7: APIs.

This post is part of a series on Web 3.0 Development with Substrate.

APIs are light-weight software that run between applications and are designed to help them consume data. APIs play a crucial role in blockchain ecosystems because they allow a multitude of DApps to seamlessly interact with each other via pre-defined endpoints. In this article, we consider how Substrate-based APIs establish a robust programming layer for DApps that persists on top of recurring runtime upgrades.

Polkadot-JS API

This Javascript-specific library is a point of reference for developers when looking to interact with Substrate nodes and chains. It comes in the standalone yarn package called @polkadot/api and relies on ApiPromise to wrap Substrate networks’ RPC and interfaces into an API object. Installation is done in a single command and always returns the latest release: this makes it easier to remain up-to-date with network upgrades and avoid issues with versioning.

Basic instantiation of an API and definition of an endpoint.
A basic Polkadot-JS API instance.

With Polkadot-JS API, interfaces are generated dynamically by connecting to a node and retrieving the metadata to populate set categories such as runtime constants, chain state, extrinsics and events. It is the go-to for tinkering with real-time DApps, because it allows developers to pick up the state of any Substrate chain and single-handedly consume its data. This popular library also gives plenty of room for flexibility and creativity, since a DApp can subscribe to multiple chain states and submit custom-made transactions.

Substrate RPC API

Substrate nodes expose a set of metadata via HTTP and WS endpoints that developers can access through RPC connections. Substrate RPC API contains the default JSON-RPC methods used to submit queries related to modules such as contracts, eth, off-chain and system. Web developers who are used to a REST-based approach to client-server operations will find the RPC-based approach to DApp-node operations more open-ended. This is because REST primarily deals with long-term database design and CRUD; whereas RPC focuses on making data available so that any method can be called on it at a later time.

Architecture: Rest APIs vs RPC APIs
Two different approaches to API architecture: REST vs RPC.

Several teams working within the Substrate ecosystem have supplemented the Substrate RPC API in programming languages other than Javascript. Real-Word Assets DeFi platform Centrifuge has created a Substrate RPC client in Go, and Entreprise blockchain services provider Usetech has developed a Substrate RPC client for C# and C++ programmers. The Polkascan Foundation has created a Python library for Substrate RPC interactions and Supercomputing Systems is maintaining a Substrate library for Rust.

Contracts API

Substrate chains that have included the Contracts pallet in their runtime require a specific API to manage SC in addition to the standard API that is used for transaction management. Contracts API interfaces allow developers to use ABIs when encoding or decoding names, arguments, messages and results from Smart Contract-specific languages like Ink! into wasm byte code, and vice-versa. It is recommended to install and upgrade @polkadot/api-contracts alongside the standard Polkadot-JS API package to avoid version mismatch during development.

Edgeware was first to implement Contracts pallets and APIs.
Edgeware is the first live implementation of the Contracts pallet and API.

Although the Contracts API comes with a variety of helpers, they provide the same core interface functionalities: handling and deploying new wasm code, templating contracts from on-chain code hashes, reading values and executing transactions on on-chain contracts. It is important to note that Relay (Layer 0) chains like Polkadot and Kusama do not and will never host SC the way legacy (Layer 1) blockchains like Ethereum or Algorand do. Instead, SC will gradually become available on parachains, well after they have obtained a slot. Ultimately, this means that the existing SC infrastructure will likely evolve and change for the better in upcoming months.

Whether you choose to build on an application-specific blockchain or a Smart Contract, you will find plenty of support in the multi-language client libraries that come with Substrate. Just as developers rely on APIs to manage blockchains data, users also need interfaces to interact with blockchain networks. In Part 8, we will review some UI components and libraries that have become essential parts of the Substrate Framework.


Smashing magazine: Understanding RPC vs REST for HTTP APIs

Shawn Tabrizi: Porting Web3.js to Polkadot.js

Polkadot{.js}: Docs