Create runtime API of Substrate based 2D barcode scanner Pallet for Querying

Reading Time: 2 minutes

In my previous post Integrate Substrate based 2D barcode scanner Pallet in your Substrate Runtime, I explained how to integrate Substrate pallet in your runtime. 

Substrate runtime is its State Transition Function, which contains business logic of a Blockchain that determines how the state changes when a block is processed and defines its behaviour of the Blockchain.

Substrate runtime can be accessed to outer node though an API, which is called substrate runtime API. A Substrate runtime API is an interface between node and the runtime.

Using Runtime API, node can query to runtime to fetch the information or provide the information. In this blog, I would explain how to create runtime API of your substrate runtime.

In my previous blog, we created a method in our pallet to verify barcode.

impl<T: Trait> Module<T> {
    pub fn is_valid_barcode(barcode: T::Hash) -> bool {

We will be creating runtime API to verify barcode.

As a first step, we will be defining interface here using decl_runtime_apis! macro

sp_api::decl_runtime_apis! {
    /// The API to verify barcode
    pub trait VerifyBarcodeApi<Hash> where
		Hash: codec::Codec, {
		/// Verify barcode
        fn is_valid_barcode(barcode: Hash) -> bool;

Next step is to implement this API interface impl_runtime_apis! macro in your runtime.

Before that, we need to import this runtime interface in our runtime. We will use Substrate Node Template as substrate project since we already imported our substrate-barcode-scanner-pallet in its runtime. Add dependency in runtime/Cargo.toml of Substrate Node Template.

default_features = false
git = ''
branch = 'main'

# toward the bottom
default = ['std']
std = [
    # --snip--

In runtime/, add below implementation details in impl_runtime_apis! macro.

impl_runtime_apis! {
  // --snip--

 	impl substrate_barcode_scanner_pallet_runtime_api::VerifyBarcodeApi<Block, Hash> for Runtime {
		fn is_valid_barcode(barcode: Hash) -> bool {

Now go back to Substrate Node Template‘s root directory and compile the node in release mode with:

cargo build --release

We have successfully added our runtime API in our runtime. Next step is to connect this API with RPC so that it can be called by end user.

Thanks for reading the blog!!

If you want to read more content like this?  Subscribe to Rust Times Newsletter and receive insights and latest updates, bi-weekly, straight into your inbox. Subscribe Rust Times Newsletter:

Written by 

Ayush is the Sr. Lead Consultant @ Knoldus Software LLP. In his 10 years of experience he has become a developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality.