Submit Extrinsic from Custom Pallets using Polkadot Js API

Reading Time: 3 minutes

Substrate is a next-generation framework for blockchain innovation. It comes with everything you need to build your blockchain. Substrate is a completely free and open-source project. It is built using Rust and WebAssembly. Rust is designed for creating fast and inherently safe software. Substrate is like a template from which you can make your own application-specific blockchain to add custom functionalities specific to one use case or group of use cases. So, In this blog, I will show you how can you submit an extrinsic using your own Polkadot API.

First Of all, you need to know what is Extrinsic. So, a piece of data that is bundled into a block in order to express something from the “external” (i.e. off-chain) world is called an extrinsic. So, before building the Polkadot API let us see how to make a dispatch call in your own custom pallet.

polkadot{.js} · GitHub
Polkadot Js

Create a Dispatch call in your pallet

A dispatch call is like a function that changes the state of the blockchain by changing the storage of your substrate chain. It fires an event to let all the other nodes know about the change in the blockchain state.

A dispatch call looks like this.

    impl<T: Config> Pallet<T> {

        pub fn update_storage(
            origin: OriginFor<T>,
            key: u32,
            value: u32,
        ) -> DispatchResult {

            let who = ensure_signed(origin)?;
            <TryMap<T>>::insert(&key, value);
            Self::deposit_event(Event::StorageUpdated(who, key));

The #[pallet::call] macro tells that the following implementation contains dispatch calls. The function update storage is a dispatch call to update a StorageMap in the storage of the blockchain.

The #[pallet::weight(10_000)] macro is used to identify the resources a call will be needing. These are called Transactional weights. Weights are the mechanism used to manage the time it takes to validate a block. Generally speaking, this comes from limiting the storage I/O and computation.

So, the sole purpose of this function is to make changes in the blockchain state and fire an event to let everyone know about the changes by submitting a transaction.

Create a Polkadot-Js API

We will be creating a node app to submit this extrinsic. So, to create a node app follow the following steps in the terminal. First, you need to get your system ready. So install some packages to begin.

  1. npm init -y
  2. npm i express
  3. yarn add @polkadot/api

This will install all the things we need to build our own app including Polkadot API tools. If you look closely this would create a package.json file in your project directory. It would look something like this.

  "name": "app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.17.1"

This includes all the npm dependencies we need for our project. Now we need to run one more command to get going.

npm install

This would install all the dependencies and create a package-lock.json file. We need not worry about this file. We will be writing our code in the index.js file that we will create.

Create a new src folder and in that folder create a new index.js file. Now write the following code in it to submit an extrinsic to our blockchain from our custom pallet.

// Import
const express = require('express');
const { ApiPromise, WsProvider } = require('@polkadot/api');
const { Keyring } = require('@polkadot/keyring');

const app = express();

app.get('/index', (req, res) =>{

    async function main(){
        // Construct
        const wsProvider = new WsProvider('ws://');
        const keyring = new Keyring({ type: 'sr25519' });
        const alice = keyring.addFromUri('//Alice');
        const api = await ApiPromise.create({ provider: wsProvider });
                const submitExtrinsic = api.tx.custom_pallet.updateStorage(15, 30);
    main().then(() => console.log('completed'));


Code Break-down

The above code basically imports different modules like

  1. express to create a node app
  2. @polkadot/api to submit extrinsic
  3. @polkadot/keyring to handle the accounts while performing transaction

The api.tx.custom_pallet.updateStorage(15, 30) command is used to create a submittable. We can use this submittable to perform the transaction.

After that the submitExtrinic.signAndSend(alice) command uses Alice’s account to do the transaction and sign the transaction. The keyring module of Polkadot API helps us to achieve this.

This was all about making your own API to submit an extrinsic.

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 to Rust Times Newsletter:


Leave a Reply