Create an Account
Introduction
Section titled “Introduction”Creating accounts is a fundamental operation when building applications on Polkadot and its parachains. Accounts serve as the basis for identity, asset ownership, and transaction signing. Understanding how to generate and manage accounts programmatically enables you to build wallets, automate operations, and create seamless user experiences.
Polkadot accounts are based on the SR25519 signature scheme by default, though ED25519 and ECDSA are also supported. Each account consists of a public key (address) and a private key (seed/mnemonic). Keep your private keys secure and never share them.
This tutorial will guide you through creating accounts using different programming languages and libraries.
Prerequisites
Section titled “Prerequisites”Before starting, make sure you have:
- Basic understanding of public-key cryptography concepts
- Development environment set up for your chosen language
- Familiarity with the programming language you'll be using
Use JavaScript/TypeScript
Section titled “Use JavaScript/TypeScript”JavaScript/TypeScript developers can use the Polkadot.js API to create and manage Polkadot accounts.
-
Create a new project directory and initialize it:
Terminal window mkdir account-creatorcd account-creatornpm init -y && npm pkg set type=module -
Install the required packages:
Terminal window npm install @polkadot/util-crypto @polkadot/keyringnpm install --save-dev typescript tsx -
Create a file named
create-account.tsand add the following code to it:import { cryptoWaitReady, mnemonicGenerate } from '@polkadot/util-crypto';import { Keyring } from '@polkadot/keyring';async function main() {await cryptoWaitReady();const mnemonic = mnemonicGenerate(12);const keyring = new Keyring({ type: 'sr25519', ss58Format: 0 });const pair = keyring.addFromMnemonic(mnemonic);console.log(`Address: ${pair.address}`);console.log(`Mnemonic: ${mnemonic}`);}main().catch(console.error);Key aspects of the code:
- Mnemonic generation: Uses
mnemonicGenerate()to create a 12-word BIP39 mnemonic phrase for human-readable key backup. - Keyring: The
Keyringclass manages accounts with a specified signature scheme and address format. - SS58 format: Setting
ss58Format: 0configures addresses for the Polkadot relay chain.
- Mnemonic generation: Uses
-
Execute the script using
tsx:Terminal window npx tsx create-account.tsYou should see output similar to:
npx tsx create-account.ts Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5 Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...
Python
Section titled “Python”Python developers can use the substrate-interface library to create and manage Polkadot accounts.
-
Create a new project directory and set up a virtual environment:
Terminal window mkdir account-creator-pythoncd account-creator-pythonpython3 -m venv venvsource venv/bin/activate # On Windows: venv\Scripts\activate -
Install the required package:
Terminal window pip install substrate-interface -
Create a file named
create_account.py:from substrateinterface import Keypairmnemonic = Keypair.generate_mnemonic()keypair = Keypair.create_from_mnemonic(mnemonic)print(f"Address: {keypair.ss58_address}")print(f"Mnemonic: {mnemonic}")Key aspects of the code:
- Mnemonic generation: The
generate_mnemonic()function creates a BIP39-compatible phrase. - Keypair creation:
Keypair.create_from_mnemonic()derives keys from the mnemonic.
- Mnemonic generation: The
-
Execute the script:
Terminal window python create_account.pyYou should see output similar to:
python create_account.py Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5 Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...
Rust provides low-level access to Substrate primitives for account creation through the sp-core and sp-keyring crates.
-
Create a new Rust project:
Terminal window cargo new account-creator-rustcd account-creator-rust -
Add dependencies to your
Cargo.toml:[package]name = "account-creator-rust"version = "0.1.0"edition = "2021"[dependencies]sp-core = "28.0"sp-runtime = "31.0" -
Create your account generation code in
src/main.rs:use sp_core::{crypto::Ss58Codec, Pair};fn main() {let (pair, phrase, _) = sp_core::sr25519::Pair::generate_with_phrase(None);let address = pair.public().to_ss58check();println!("Address: {}", address);println!("Mnemonic: {}", phrase);}Key aspects of the code:
- Keypair generation:
sr25519::Pair::generate_with_phrase()creates a new key pair with mnemonic. - Public key extraction: The
public()method retrieves the public key from the pair. - SS58 encoding: Uses Polkadot's address format for the human-readable address.
- Keypair generation:
-
Build and run the project:
Terminal window cargo runYou should see output similar to:
cargo run Address: 15oF4uVJwmo4TdGW7VfQxNLavjCXviqxT9S1MgbjMNHr6Sp5 Mnemonic: cushion dog echo people vendor curve truck begin latin romance rebuild ...
Where to Go Next
Section titled “Where to Go Next”-
Send Transactions with SDKs
Learn how to send signed transactions using your newly created accounts with Polkadot-API and Polkadot.js API libraries.
-
Calculate Transaction Fees
Learn how to estimate transaction fees before sending transactions from your accounts.
-
Query Chain Data
Explore different methods for querying blockchain data, including account balances and other chain state.