> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/solana-foundation/anchor/llms.txt
> Use this file to discover all available pages before exploring further.

# anchor shell

> Start an interactive Node.js shell with Anchor client setup

## 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

```bash theme={null}
anchor shell
```

## 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

## Pre-configured Objects

The shell provides these objects automatically:

### anchor

The main Anchor framework:

```javascript theme={null}
anchor.web3          // Solana web3.js
anchor.Program       // Program client class
anchor.AnchorProvider // Provider class
anchor.workspace     // Workspace programs
```

### web3

Solana web3.js library:

```javascript theme={null}
web3.PublicKey       // PublicKey class
web3.Keypair         // Keypair class
web3.Transaction     // Transaction class
web3.SystemProgram   // System program
```

### PublicKey

Shortcut to `web3.PublicKey`:

```javascript theme={null}
const pubkey = new PublicKey("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS")
```

### Keypair

Shortcut to `web3.Keypair`:

```javascript theme={null}
const keypair = Keypair.generate()
```

### provider

Pre-configured Anchor provider:

```javascript theme={null}
provider.connection   // RPC connection
provider.wallet       // Wallet from config
provider.opts         // Provider options
```

### anchor.workspace

All workspace programs loaded as clients:

```javascript theme={null}
anchor.workspace.myProgram    // Your program client
anchor.workspace.otherProgram // Another program client
```

## Examples

### Start the Shell

```bash theme={null}
anchor shell
```

**Output:**

```
Welcome to the Anchor shell
> 
```

### Check Provider Configuration

```javascript theme={null}
> provider.connection.rpcEndpoint
'http://localhost:8899'

> provider.wallet.publicKey.toString()
'3Z9vL1zjN6qyAFHhHQdWYRTFAcy69pJydkZmSFBKHg1R'
```

### Interact with a Program

```javascript theme={null}
> 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

```javascript theme={null}
> const balance = await provider.connection.getBalance(provider.wallet.publicKey)
> balance / web3.LAMPORTS_PER_SOL
10.5 // SOL balance
```

### Fetch Account Data

```javascript theme={null}
> const account = await program.account.myAccount.fetch(
    new PublicKey("Account111111111111111111111111111111111111")
  )
> account
{ field1: ..., field2: ..., ... }
```

### Create and Send Transaction

```javascript theme={null}
> const user = Keypair.generate()

> const tx = await program.methods
    .createUser()
    .accounts({ user: user.publicKey })
    .signers([user])
    .rpc()

> console.log("Transaction:", tx)
```

### Check Cluster Connection

```javascript theme={null}
> 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)

```javascript theme={null}
> const signature = await provider.connection.requestAirdrop(
    provider.wallet.publicKey,
    web3.LAMPORTS_PER_SOL
  )

> await provider.connection.confirmTransaction(signature)
```

### Explore Program IDL

```javascript theme={null}
> 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:

```toml theme={null}
[provider]
cluster = "localnet"  # Or devnet, mainnet-beta, custom URL
wallet = "~/.config/solana/id.json"

[programs.localnet]
my_program = "Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS"
```

Override cluster:

```bash theme={null}
anchor shell --provider.cluster devnet
```

Override wallet:

```bash theme={null}
anchor shell --provider.wallet /path/to/wallet.json
```

## Advanced Examples

### Working with PDAs

```javascript theme={null}
> const [pda, bump] = await PublicKey.findProgramAddress(
    [Buffer.from("seed"), provider.wallet.publicKey.toBuffer()],
    program.programId
  )

> pda.toString()
'PDA111111111111111111111111111111111111111'
```

### Multiple Signers

```javascript theme={null}
> 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

```javascript theme={null}
> const accounts = await program.account.myAccount.all()
> accounts.length
5

> accounts.forEach(acc => {
    console.log(acc.publicKey.toString(), acc.account)
  })
```

## Shell Commands

```javascript theme={null}
// 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

<Tip>
  Use the shell for rapid prototyping and testing without writing separate script files.
</Tip>

<Info>
  The shell automatically loads all programs defined in your workspace, making them available under `anchor.workspace`.
</Info>

<Note>
  All async operations must use `await` or `.then()` in the REPL. Top-level await is supported.
</Note>

<Warning>
  Be careful when using the shell with mainnet-beta. All transactions are real and irreversible.
</Warning>

## See Also

* [Node.js REPL](https://nodejs.org/api/repl.html) - Node.js REPL documentation
* [Solana Web3.js](https://solana-labs.github.io/solana-web3.js/) - Solana JavaScript library
* [Anchor Client](https://www.anchor-lang.com/docs/clients) - Anchor client documentation
