|
1 | | -TODO |
| 1 | +--- |
| 2 | +title: Send Transactions with SDKs |
| 3 | +description: Learn how to construct, sign, and submit transactions using PAPI, Polkadot.js, Dedot, Python Substrate Interface, and Subxt. |
| 4 | +categories: Chain Interactions |
| 5 | +--- |
| 6 | + |
| 7 | +# Send Transactions with SDKs |
| 8 | + |
| 9 | +## Introduction |
| 10 | + |
| 11 | +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. |
| 12 | + |
| 13 | +This guide demonstrates how to send transactions using five popular SDKs: |
| 14 | + |
| 15 | +- **[Polkadot API (PAPI)](/reference/tools/papi/){target=\_blank}** - Modern TypeScript library with type-safe APIs |
| 16 | +- **[Polkadot.js API](/reference/tools/polkadot-js-api/){target=\_blank}** - Comprehensive JavaScript library (maintenance mode) |
| 17 | +- **[Dedot](/reference/tools/dedot/){target=\_blank}** - Lightweight TypeScript library optimized for performance |
| 18 | +- **[Python Substrate Interface](/reference/tools/py-substrate-interface/){target=\_blank}** - Python library for Substrate chains |
| 19 | +- **[Subxt](/reference/tools/subxt/){target=\_blank}** - Rust library with compile-time type safety |
| 20 | + |
| 21 | +Select your preferred SDK below to see complete, runnable examples that send balance transfer transactions on Polkadot Hub. |
| 22 | + |
| 23 | +!!! note |
| 24 | + Ensure to replace `INSERT_WS_ENDPOINT` with the proper WebSocket endpoint, `INSERT_SENDER_MNEMONIC` with your account's mnemonic phrase, and `INSERT_DEST_ADDRESS` with the recipient address. For this example, you can use Polkadot Hub (`wss://polkadot-asset-hub-rpc.polkadot.io`). |
| 25 | + |
| 26 | +!!! warning |
| 27 | + Never share your mnemonic phrase or private keys. The examples below use mnemonics for demonstration purposes only. In production, use secure key management solutions. |
| 28 | + |
| 29 | +## Send Transactions |
| 30 | + |
| 31 | +=== "PAPI" |
| 32 | + |
| 33 | + [Polkadot API (PAPI)](/reference/tools/papi/){target=\_blank} is a modern, type-safe TypeScript library optimized for light-client functionality. |
| 34 | + |
| 35 | + **Prerequisites** |
| 36 | + |
| 37 | + - [Node.js](https://nodejs.org/){target=\_blank} v18 or higher |
| 38 | + - npm, pnpm, or yarn package manager |
| 39 | + |
| 40 | + **Environment Setup** |
| 41 | + |
| 42 | + 1. Create and initialize a new project: |
| 43 | + |
| 44 | + ```bash |
| 45 | + mkdir papi-send-tx-example && cd papi-send-tx-example && \ |
| 46 | + npm init -y && npm pkg set type=module |
| 47 | + ``` |
| 48 | + |
| 49 | + 2. Install dependencies: |
| 50 | + |
| 51 | + ```bash |
| 52 | + npm install polkadot-api @polkadot/util-crypto @polkadot/keyring && \ |
| 53 | + npm install --save-dev @types/node tsx typescript |
| 54 | + ``` |
| 55 | + |
| 56 | + 3. Generate types for Polkadot Hub TestNet: |
| 57 | + |
| 58 | + ```bash |
| 59 | + npx papi add polkadotTestNet -w wss://asset-hub-paseo.dotters.network |
| 60 | + ``` |
| 61 | + |
| 62 | + **Send Balance Transfer** |
| 63 | + |
| 64 | + The following example constructs, signs, and submits a balance transfer transaction. |
| 65 | + |
| 66 | + Create a file named `send-transfer.ts`: |
| 67 | + |
| 68 | + ```typescript title="send-transfer.ts" |
| 69 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/papi/send-transfer.ts" |
| 70 | + ``` |
| 71 | + |
| 72 | + Run the script: |
| 73 | + |
| 74 | + ```bash |
| 75 | + npx tsx send-transfer.ts |
| 76 | + ``` |
| 77 | + |
| 78 | + You should see output similar to: |
| 79 | + |
| 80 | + --8<-- 'code/chain-interactions/send-transactions/with-sdks/papi/send-transfer-ts.html' |
| 81 | + |
| 82 | +=== "Polkadot.js" |
| 83 | + |
| 84 | + !!! warning "Maintenance Mode Only" |
| 85 | + The Polkadot.js API is no longer actively developed. New projects should use [PAPI](/reference/tools/papi/){target=\_blank} or [Dedot](/reference/tools/dedot/){target=\_blank} as actively maintained alternatives. |
| 86 | + |
| 87 | + [Polkadot.js API](/reference/tools/polkadot-js-api/){target=\_blank} is a comprehensive JavaScript library with extensive ecosystem support. |
| 88 | + |
| 89 | + **Prerequisites** |
| 90 | + |
| 91 | + - [Node.js](https://nodejs.org/){target=\_blank} v18 or higher |
| 92 | + - npm, pnpm, or yarn package manager |
| 93 | + |
| 94 | + **Environment Setup** |
| 95 | + |
| 96 | + 1. Create and initialize a new project: |
| 97 | + |
| 98 | + ```bash |
| 99 | + mkdir pjs-send-tx-example && cd pjs-send-tx-example && \ |
| 100 | + npm init -y && npm pkg set type=module |
| 101 | + ``` |
| 102 | + |
| 103 | + 2. Install dependencies: |
| 104 | + |
| 105 | + ```bash |
| 106 | + npm install @polkadot/api @polkadot/keyring @polkadot/util-crypto |
| 107 | + ``` |
| 108 | + |
| 109 | + **Send Balance Transfer** |
| 110 | + |
| 111 | + The following example constructs, signs, and submits a balance transfer transaction. |
| 112 | + |
| 113 | + Create a file named `send-transfer.js`: |
| 114 | + |
| 115 | + ```javascript title="send-transfer.js" |
| 116 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/pjs/send-transfer.js" |
| 117 | + ``` |
| 118 | + |
| 119 | + Run the script: |
| 120 | + |
| 121 | + ```bash |
| 122 | + node send-transfer.js |
| 123 | + ``` |
| 124 | + |
| 125 | + You should see output similar to: |
| 126 | + |
| 127 | + --8<-- 'code/chain-interactions/send-transactions/with-sdks/pjs/send-transfer-js.html' |
| 128 | + |
| 129 | +=== "Dedot" |
| 130 | + |
| 131 | + [Dedot](/reference/tools/dedot/){target=\_blank} is a next-generation TypeScript client that's lightweight, tree-shakable, and maintains API compatibility with Polkadot.js. |
| 132 | + |
| 133 | + **Prerequisites** |
| 134 | + |
| 135 | + - [Node.js](https://nodejs.org/){target=\_blank} v18 or higher |
| 136 | + - npm, pnpm, or yarn package manager |
| 137 | + |
| 138 | + **Environment Setup** |
| 139 | + |
| 140 | + 1. Create and initialize a new project: |
| 141 | + |
| 142 | + ```bash |
| 143 | + mkdir dedot-send-tx-example && cd dedot-send-tx-example && \ |
| 144 | + npm init -y && npm pkg set type=module |
| 145 | + ``` |
| 146 | + |
| 147 | + 2. Install dependencies: |
| 148 | + |
| 149 | + ```bash |
| 150 | + npm install dedot @polkadot/keyring @polkadot/util-crypto && \ |
| 151 | + npm install --save-dev @dedot/chaintypes @types/node tsx typescript |
| 152 | + ``` |
| 153 | + |
| 154 | + **Send Balance Transfer** |
| 155 | + |
| 156 | + The following example constructs, signs, and submits a balance transfer transaction. |
| 157 | + |
| 158 | + Create a file named `send-transfer.ts`: |
| 159 | + |
| 160 | + ```typescript title="send-transfer.ts" |
| 161 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/dedot/send-transfer.ts" |
| 162 | + ``` |
| 163 | + |
| 164 | + Run the script: |
| 165 | + |
| 166 | + ```bash |
| 167 | + npx tsx send-transfer.ts |
| 168 | + ``` |
| 169 | + |
| 170 | + You should see output similar to: |
| 171 | + |
| 172 | + --8<-- 'code/chain-interactions/send-transactions/with-sdks/dedot/send-transfer-ts.html' |
| 173 | + |
| 174 | +=== "Python" |
| 175 | + |
| 176 | + [Python Substrate Interface](/reference/tools/py-substrate-interface/){target=\_blank} provides a Python library for interacting with Substrate-based chains. |
| 177 | + |
| 178 | + **Prerequisites** |
| 179 | + |
| 180 | + - [Python](https://www.python.org/){target=\_blank} 3.8 or higher |
| 181 | + - pip package manager |
| 182 | + |
| 183 | + **Environment Setup** |
| 184 | + |
| 185 | + 1. Create a new project directory and set up a virtual environment: |
| 186 | + |
| 187 | + ```bash |
| 188 | + mkdir psi-send-tx-example && cd psi-send-tx-example && \ |
| 189 | + python3 -m venv venv && source venv/bin/activate |
| 190 | + ``` |
| 191 | + |
| 192 | + 2. Install the substrate-interface package: |
| 193 | + |
| 194 | + ```bash |
| 195 | + pip install substrate-interface |
| 196 | + ``` |
| 197 | + |
| 198 | + **Send Balance Transfer** |
| 199 | + |
| 200 | + The following example constructs, signs, and submits a balance transfer transaction. |
| 201 | + |
| 202 | + Create a file named `send_transfer.py`: |
| 203 | + |
| 204 | + ```python title="send_transfer.py" |
| 205 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/psi/send_transfer.py" |
| 206 | + ``` |
| 207 | + |
| 208 | + Run the script: |
| 209 | + |
| 210 | + ```bash |
| 211 | + python send_transfer.py |
| 212 | + ``` |
| 213 | + |
| 214 | + You should see output similar to: |
| 215 | + |
| 216 | + --8<-- 'code/chain-interactions/send-transactions/with-sdks/psi/send-transfer-py.html' |
| 217 | + |
| 218 | +=== "Subxt" |
| 219 | + |
| 220 | + [Subxt](/reference/tools/subxt/){target=\_blank} is a Rust library that provides compile-time type safety through code generation from chain metadata. |
| 221 | + |
| 222 | + **Prerequisites** |
| 223 | + |
| 224 | + - [Rust](https://rustup.rs/){target=\_blank} toolchain (latest stable) |
| 225 | + - Cargo package manager |
| 226 | + |
| 227 | + **Environment Setup** |
| 228 | + |
| 229 | + 1. Create a new Rust project: |
| 230 | + |
| 231 | + ```bash |
| 232 | + cargo new subxt-send-tx-example && cd subxt-send-tx-example |
| 233 | + ``` |
| 234 | + |
| 235 | + 2. Install the Subxt CLI: |
| 236 | + |
| 237 | + ```bash |
| 238 | + cargo install subxt-cli@{{ dependencies.crates.subxt_cli.version }} |
| 239 | + ``` |
| 240 | + |
| 241 | + 3. Download the Polkadot Hub metadata: |
| 242 | + |
| 243 | + ```bash |
| 244 | + subxt metadata --url INSERT_WS_ENDPOINT -o asset_hub_metadata.scale |
| 245 | + ``` |
| 246 | + |
| 247 | + 4. Update `Cargo.toml` with the required dependencies: |
| 248 | + |
| 249 | + ```toml title="Cargo.toml" |
| 250 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/subxt/Cargo.toml" |
| 251 | + ``` |
| 252 | + |
| 253 | + **Send Balance Transfer** |
| 254 | + |
| 255 | + The following example constructs, signs, and submits a balance transfer transaction. |
| 256 | + |
| 257 | + Create a file at `src/bin/send_transfer.rs`: |
| 258 | + |
| 259 | + ```rust title="src/bin/send_transfer.rs" |
| 260 | + --8<-- "code/chain-interactions/send-transactions/with-sdks/subxt/src/bin/send_transfer.rs" |
| 261 | + ``` |
| 262 | + |
| 263 | + Run the script: |
| 264 | + |
| 265 | + ```bash |
| 266 | + cargo run --bin send_transfer |
| 267 | + ``` |
| 268 | + |
| 269 | + You should see output similar to: |
| 270 | + |
| 271 | + --8<-- 'code/chain-interactions/send-transactions/with-sdks/subxt/send-transfer-rs.html' |
| 272 | + |
| 273 | +## Where to Go Next |
| 274 | + |
| 275 | +Now that you understand how to send transactions, explore these related topics: |
| 276 | + |
| 277 | +- **[Query On-Chain State](/chain-interactions/query-data/query-sdks/)** - Learn to query storage and runtime data |
| 278 | +- **[Calculate Transaction Fees](/chain-interactions/send-transactions/calculate-transaction-fees/)** - Estimate fees before sending transactions |
| 279 | +- **[SDK Reference Pages](/reference/tools/papi/)** - Detailed documentation for each SDK |
0 commit comments