How to use the command bitcoin-cli (with examples)
Bitcoin-cli is a command-line client that allows users to interact with the Bitcoin daemon via remote procedure call (RPC) calls. It utilizes the configuration specified in the bitcoin.conf
file and provides various functionalities to manage and interact with the Bitcoin network.
Use case 1: Send a transaction to a given address
Code:
bitcoin-cli sendtoaddress "address" amount
Motivation: This example is useful when you want to send bitcoins to a specific address. It allows you to transfer a certain amount of bitcoins from your own wallet to the provided address.
Explanation:
sendtoaddress
is the command used to send bitcoins to a given address."address"
refers to the recipient’s Bitcoin address where the funds will be sent.amount
is the number of bitcoins to be sent to the specified address.
Example output:
[
"transaction_id"
]
The example output will provide the transaction ID of the successfully sent transaction.
Use case 2: Generate one or more blocks
Code:
bitcoin-cli generate num_blocks
Motivation: Generating blocks is useful when you want to mine new bitcoins or test certain functionalities on a private Bitcoin network.
Explanation:
generate
is the command used to mine new blocks.num_blocks
is the number of blocks to be generated.
Example output:
[
"block_hash1",
"block_hash2",
"block_hash3"
]
The generated blocks’ hashes will be provided as the example output.
Use case 3: Print high-level information about the wallet
Code:
bitcoin-cli getwalletinfo
Motivation: This example is helpful when you want to obtain an overview of the wallet’s information, including the balance, number of transactions, and other relevant details.
Explanation:
getwalletinfo
is the command used to retrieve information about the wallet.
Example output:
{
"walletversion": 12345,
"balance": 0.12345678,
"txcount": 50,
...
}
The example output will include several key-value pairs providing details about the wallet, such as the wallet version, current balance, number of transactions, etc.
Use case 4: List all outputs from previous transactions available to fund outgoing transactions
Code:
bitcoin-cli listunspent
Motivation: Listing unspent transaction outputs (UTXOs) is useful when you want to identify the available balance in your wallet that can be used for future outgoing transactions.
Explanation:
listunspent
is the command used to retrieve a list of UTXOs.
Example output:
[
{
"txid": "transaction_id1",
"output_index": 1,
"address": "sender_address1",
"value": 0.12345678,
...
},
{
"txid": "transaction_id2",
"output_index": 0,
"address": "sender_address2",
"value": 0.98765432,
...
},
...
]
The example output will provide a list of UTXOs with their respective transaction IDs, output indexes, sender addresses, and values.
Use case 5: Export the wallet information to a text file
Code:
bitcoin-cli dumpwallet "path/to/file"
Motivation: Exporting the wallet information to a file can be useful for creating a backup or transferring the wallet to another machine.
Explanation:
dumpwallet
is the command used to export the wallet information."path/to/file"
specifies the file path where the wallet information will be saved.
Example output:
{
"filename": "path/to/file",
...
}
The example output will indicate the filename and other relevant details about the exported wallet information file.
Conclusion:
Bitcoin-cli is a versatile command-line client that enables users to interact with the Bitcoin network. Whether you want to send transactions, generate blocks, obtain wallet information, list unspent transaction outputs, or export wallet information, bitcoin-cli provides the necessary functionalities to manage your Bitcoin operations effectively.