Overview
The anchor shell command starts an interactive Node.js REPL (Read-Eval-Print Loop) with an Anchor client pre-configured according to your workspace’s Anchor.toml settings.
Command Syntax
Description
This command launches a Node.js shell with:
- Anchor framework loaded and ready to use
- Connection to the configured cluster
- Wallet loaded from configuration
- All workspace programs initialized as Anchor Program clients
- Solana web3.js utilities available
The shell provides these objects automatically:
anchor
The main Anchor framework:
anchor.web3 // Solana web3.js
anchor.Program // Program client class
anchor.AnchorProvider // Provider class
anchor.workspace // Workspace programs
web3
Solana web3.js library:
web3.PublicKey // PublicKey class
web3.Keypair // Keypair class
web3.Transaction // Transaction class
web3.SystemProgram // System program
PublicKey
Shortcut to web3.PublicKey:
const pubkey = new PublicKey("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS")
Keypair
Shortcut to web3.Keypair:
const keypair = Keypair.generate()
provider
Pre-configured Anchor provider:
provider.connection // RPC connection
provider.wallet // Wallet from config
provider.opts // Provider options
anchor.workspace
All workspace programs loaded as clients:
anchor.workspace.myProgram // Your program client
anchor.workspace.otherProgram // Another program client
Examples
Start the Shell
Output:
Welcome to the Anchor shell
>
Check Provider Configuration
> provider.connection.rpcEndpoint
'http://localhost:8899'
> provider.wallet.publicKey.toString()
'3Z9vL1zjN6qyAFHhHQdWYRTFAcy69pJydkZmSFBKHg1R'
Interact with a Program
> const program = anchor.workspace.myProgram
> // View program methods
> program.methods
{ initialize: [Function], ... }
> // Call an instruction
> const tx = await program.methods.initialize().rpc()
'5j8...' // Transaction signature
Check Balance
> const balance = await provider.connection.getBalance(provider.wallet.publicKey)
> balance / web3.LAMPORTS_PER_SOL
10.5 // SOL balance
Fetch Account Data
> const account = await program.account.myAccount.fetch(
new PublicKey("Account111111111111111111111111111111111111")
)
> account
{ field1: ..., field2: ..., ... }
Create and Send Transaction
> const user = Keypair.generate()
> const tx = await program.methods
.createUser()
.accounts({ user: user.publicKey })
.signers([user])
.rpc()
> console.log("Transaction:", tx)
Check Cluster Connection
> const version = await provider.connection.getVersion()
> version
{ 'solana-core': '2.0.6', 'feature-set': ... }
> const slot = await provider.connection.getSlot()
> slot
12345678
Airdrop (Localnet/Devnet)
> const signature = await provider.connection.requestAirdrop(
provider.wallet.publicKey,
web3.LAMPORTS_PER_SOL
)
> await provider.connection.confirmTransaction(signature)
Explore Program IDL
> program.idl.instructions
[
{ name: 'initialize', accounts: [...], args: [...] },
...
]
> program.idl.accounts
[
{ name: 'MyAccount', type: {...} },
...
]
Use Cases
Development and Testing
- Quick testing of program instructions
- Debugging account states
- Experimenting with transactions
- Prototyping client code
Account Management
- Fetching and inspecting account data
- Creating derived addresses (PDAs)
- Checking account balances and states
Transaction Testing
- Building complex transactions
- Testing different instruction combinations
- Verifying transaction signatures
Cluster Interaction
- Checking cluster status
- Requesting airdrops
- Monitoring slots and epochs
Configuration
The shell uses your Anchor.toml configuration:
[provider]
cluster = "localnet" # Or devnet, mainnet-beta, custom URL
wallet = "~/.config/solana/id.json"
[programs.localnet]
my_program = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
Override cluster:
anchor shell --provider.cluster devnet
Override wallet:
anchor shell --provider.wallet /path/to/wallet.json
Advanced Examples
Working with PDAs
> const [pda, bump] = await PublicKey.findProgramAddress(
[Buffer.from("seed"), provider.wallet.publicKey.toBuffer()],
program.programId
)
> pda.toString()
'PDA111111111111111111111111111111111111111'
Multiple Signers
> const user1 = Keypair.generate()
> const user2 = Keypair.generate()
> const tx = await program.methods
.multiSig()
.accounts({ user1: user1.publicKey, user2: user2.publicKey })
.signers([user1, user2])
.rpc()
Fetching Multiple Accounts
> const accounts = await program.account.myAccount.all()
> accounts.length
5
> accounts.forEach(acc => {
console.log(acc.publicKey.toString(), acc.account)
})
Shell Commands
// Exit the shell
.exit
// or press Ctrl+D
// Clear the console
console.clear()
// Load external modules
const fs = require('fs')
const axios = require('axios')
// Multiline input (press Enter for new line)
> const result = await program.methods
... .initialize()
... .rpc()
Notes
Use the shell for rapid prototyping and testing without writing separate script files.
The shell automatically loads all programs defined in your workspace, making them available under anchor.workspace.
All async operations must use await or .then() in the REPL. Top-level await is supported.
Be careful when using the shell with mainnet-beta. All transactions are real and irreversible.
See Also