Granite Upgrade Activates in06d:23h:31m:07s
Account ManagementLocal Accounts

Local Accounts

Learn how to create and manage local accounts in the Avalanche Client SDK with private keys, mnemonics, and HD keys.

Overview

Local accounts store keys on your machine and sign transactions before broadcasting. Use these for server-side apps, bots, or when you need full control.

Security: Never expose private keys or mnemonics in client-side code or commit them to version control. Use environment variables.

Quick Start

import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";

const account = privateKeyToAvalancheAccount(process.env.PRIVATE_KEY!);

console.log(account.getEVMAddress()); // 0x742d35Cc...
console.log(account.getXPAddress("X")); // X-avax1...

Account Types

Private Key

Simplest option—create an account directly from a private key.

import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";

const account = privateKeyToAvalancheAccount("0x...");

Private Key Accounts →

Mnemonic

User-friendly option—create an account from a seed phrase.

import { mnemonicsToAvalancheAccount } from "@avalanche-sdk/client/accounts";

const account = mnemonicsToAvalancheAccount("abandon abandon abandon...");

Mnemonic Accounts →

HD Key

Advanced option—create accounts from HD keys with custom derivation paths.

import { hdKeyToAvalancheAccount, HDKey } from "@avalanche-sdk/client/accounts";

const hdKey = HDKey.fromMasterSeed(seed);
const account = hdKeyToAvalancheAccount(hdKey, { accountIndex: 0 });

HD Key Accounts →

Instantiation

Setup Wallet Client

import { createAvalancheWalletClient } from "@avalanche-sdk/client";
import { avalanche } from "@avalanche-sdk/client/chains";
import { privateKeyToAvalancheAccount } from "@avalanche-sdk/client/accounts";
import { avaxToWei } from "@avalanche-sdk/client/utils";

const account = privateKeyToAvalancheAccount(process.env.PRIVATE_KEY!);

const walletClient = createAvalancheWalletClient({
  account, // Hoist account to avoid passing it to each method
  chain: avalanche,
  transport: { type: "http" },
});

// Use wallet methods
const txHash = await walletClient.send({
  to: "0x742d35Cc6634C0532925a3b8D4C9db96C4b4d8b6",
  amount: avaxToWei(0.001),
});

// Use public methods
const balance = await walletClient.getBalance({
  address: account.getEVMAddress(),
});

Account Generation

Generate Private Key

import {
  generatePrivateKey,
  privateKeyToAvalancheAccount,
} from "@avalanche-sdk/client/accounts";
import type { AvalancheAccount } from "@avalanche-sdk/client/accounts";

const privateKey: string = generatePrivateKey();
const account: AvalancheAccount = privateKeyToAvalancheAccount(privateKey);

Generate Mnemonic

import {
  generateMnemonic,
  mnemonicsToAvalancheAccount,
} from "@avalanche-sdk/client/accounts";
import type { AvalancheAccount } from "@avalanche-sdk/client/accounts";

const mnemonic: string = generateMnemonic();
const account: AvalancheAccount = mnemonicsToAvalancheAccount(mnemonic);

Address Management

Get All Addresses

const addresses = {
  evm: account.getEVMAddress(),
  xChain: account.getXPAddress("X"),
  pChain: account.getXPAddress("P"),
};

console.log("EVM Address:", addresses.evm);
console.log("X-Chain Address:", addresses.xChain);
console.log("P-Chain Address:", addresses.pChain);

Learn more: For network-specific addresses and detailed address management examples, see Network-Specific Addresses.

Security

Never expose private keys or mnemonics in client-side code or commit them to version control. Always use environment variables.

// ✅ Good: Use environment variables
const account = privateKeyToAvalancheAccount(process.env.PRIVATE_KEY!);

// ❌ Bad: Hardcoded private key
const account = privateKeyToAvalancheAccount("0x1234...");

Learn More

Is this guide helpful?