Skip to content

Send Transactions with SDKs

Sending transactions on Polkadot SDK-based blockchains involves constructing an extrinsic (transaction), signing it with your account's private key, and submitting it to the network. Each SDK provides different methods for transaction construction, signing, and submission.

This guide demonstrates how to send transactions using five popular SDKs:

Select your preferred SDK below to see complete, runnable examples that send balance transfer transactions on Polkadot Hub.

  • Access to a Polkadot-SDK-compatible wallet, with its mnemonic phrase or private key.
  • A funded account on Polkadot Hub, with some testnet tokens. You can use the Polkadot Faucet to obtain test tokens.

Prerequisites

  • Node.js v18 or higher
  • npm, pnpm, or yarn package manager

Environment Setup

  1. Create and initialize a new project:

    Terminal window
    mkdir papi-send-tx-example && cd papi-send-tx-example && \
    npm init -y && npm pkg set type=module
  2. Install dependencies:

    Terminal window
    npm install polkadot-api@3.3.0 @polkadot/util-crypto @polkadot/keyring && \
    npm install --save-dev @types/node tsx typescript
  3. Generate types for Polkadot Hub TestNet:

    Terminal window
    npx papi add polkadotTestNet -w wss://asset-hub-paseo.dotters.network

Send Balance Transfer

The following example constructs, signs, and submits a balance transfer transaction.

Create a file named send-transfer.ts and add the following code to it:

send-transfer.ts
import { createClient } from 'polkadot-api';
import { getWsProvider } from 'polkadot-api/ws';
import { polkadotTestNet } from '@polkadot-api/descriptors';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { Keyring } from '@polkadot/keyring';
import { getTxCreator } from 'polkadot-api/tx-creator';
const POLKADOT_TESTNET_RPC = 'INSERT_WS_ENDPOINT';
const SENDER_MNEMONIC = 'INSERT_MNEMONIC';
const DEST_ADDRESS = 'INSERT_DEST_ADDRESS';
const AMOUNT = 1_000_000_000n; // 1 PAS (adjust decimals as needed)
async function main() {
try {
await cryptoWaitReady();
const keyring = new Keyring({ type: 'sr25519' });
const sender = keyring.addFromMnemonic(SENDER_MNEMONIC);
console.log(`Sender address: ${sender.address}`);
// Create the client connection
const client = createClient(getWsProvider(POLKADOT_TESTNET_RPC));
// Get the typed API
const api = client.getTypedApi(polkadotTestNet);
console.log('Connected to Polkadot Testnet');
// Create signer using getTxCreator
const signer = getTxCreator(
sender.publicKey,
'Sr25519',
async (input) => sender.sign(input)
);
// Construct and submit the transfer transaction
const tx = api.tx.Balances.transfer_keep_alive({
dest: { type: 'Id', value: DEST_ADDRESS },
value: AMOUNT,
});
console.log('\nSigning and submitting transaction...');
// Polkadot Hub TestNet requires a value for its RestrictOrigins extension
const { txHash } = await tx.createAndSubmit(signer, {
customSignedExtensions: { RestrictOrigins: { value: true } },
});
console.log(`Transaction submitted with hash: ${txHash}`);
await client.destroy();
console.log('Disconnected');
} catch (error) {
console.error('Error:', error);
process.exit(1);
}
}
main();

Run the script:

Terminal window
npx tsx send-transfer.ts

You should see output similar to:

npx tsx send-transfer.ts Sender address: 5GgbDVeKZwCmMHzn58iFSgSZDTojRMM52arXnuNXto28R7mg Connected to Polkadot Testnet Signing and submitting transaction... Transaction submitted with hash: 0x45ce4b5223428d003ddeed5118182eede1224dd0b6e881d7ed40517d60aa1c57 Disconnected
  • Query On-Chain State


    Learn how to query storage and runtime data with the SDKs used in this guide.

    → Get Started

  • Calculate Transaction Fees


    Estimate fees before sending transactions

    → Get Started

Last updated: