Bitcoin-CLI Commands And API Methods

By Nicholas Brown.

A list of some frequently-needed Bitcoin API commands to interact with the Bitcoin Daemon/Bitcoin Core. These can be used with the Bitcoin command line interface (Bitcoin-CLI) on Linux to interact with the network, pull in blockchain data, view transaction status, initiate transactions and much more. I like to keep these handy for future reference.

If you get the following error, use the console built into the Bitcoin-Core GUI. Go to Window > Console.

‘error: Could not locate RPC credentials. No authentication cookie could be found, and RPC password is not set.’

Bitcoin-CLI Commands

If a command doesn’t work due to a ‘not found’ error, try prefixing ./ to it if you’re on Linux. Also ensure that the Bitcoin daemon (run bitcoind) is running while you’re executing these commands. You can also go to the Console tab in the Bitcoin Core GUI to execute these commands, but exclude the prefixed ‘bitcoin-cli’ when doing so.

This will list the commands available to interact with the CLI and their parameters (if any).

bitcoin-cli help

Display Bitcoin Core RPC client version and display several commands you can use to set RPC API options.

bitcoin-cli -?

Create a new Bitcoin wallet:

bitcoin-cli createwallet "walletname"

Load a Bitcoin wallet so you can interact with it in the CLI:

bitcoin-cli loadwallet "walletfile.dat"

List Bitcoin transactions associated with the wallet you loaded:

bitcoin-cli listtransactions

Get the balance of the loaded Bitcoin account/wallet (not for a specific wallet address, but for all addresses under that account):

bitcoin-cli getbalance

List wallets:

bitcoin-cli listwallets

Retrieve the details of a Bitcoin transaction (only the ones associated with your wallet):

bitcoin-cli gettransaction transactionid

Get general blockchain info, including mining difficulty, the best block hash, which chain you’re on, forks, and more:

bitcoin-cli getblockchaininfo

Abandon a Bitcoin transaction:

bitcoin-cli abandontransaction transactionid

Set transaction fee (in BTC/kvB):

bitcoin-cli settxfee

Encrypt your Bitcoin wallet:

bitcoin-cli encryptwallet "passphrase"

Check the validity of an address:

bitcoin-cli validateaddress address

Get the number of coins received by an address (works only on your addresses):

bitcoin-cli getreceivedbyaddress address

Display your private key (don’t let anyone see it):

bitcoin-cli dumpprivkey walletaddress

Export private keys to a file (ensure that you copy it to a safe place where no one can steal it):

bitcoin-cli dumpwallet "filename"

When done for the day, run history -c to clear your terminal history (considering that your passphrase was typed there).

List unspent transaction outputs (UTXO’s, or coins available for spending). This returns the txid, confirmations, amount, and more for each UTXO in a JSON Array. You can use one of these to create a new transaction (using the createrawtransaction command):

bitcoin-cli listunspent

Create raw transaction — the first step towards creating a Bitcoin transaction. Will return a hex value to use in the upcoming ‘createrawtransaction‘ command below:

bitcoin-cli createrawtransaction "[{\"txid\":\"utxo_transaction_id\", \"vout\": 0}]" "[{\"wallet_address\": btc_amount}]"

Sign a raw transaction — the second step towards creating a Bitcoin transaction:

You may be prompted to authenticate with your passphrase if your wallet is encrypted. If so,  authenticate by typing:

bitcoin-cli walletpassphrase password timeout_in_seconds.

Then run:

bitcoin-cli signrawtransactionwithwallet hex_value_returned_by_createrawtransaction

The third step — sending off your transaction. Now you can send the transaction using ‘sendrawtransaction’. Use the hex code returned by the ‘signrawtransactionwithwallet’ command above:

bitcoin-cli sendrawtransaction hex_code_from_signed_transaction

Get blockchain info:

bitcoin-cli getblock blockhash

Get mining info, including mining difficulty, hashes per second, pooled transactions, and more:

bitcoin-cli getmininginfo

Bitcoin JSON RPC API Methods

There are many JSON RPC API methods supported by the Bitcoin Core API that can be used to send it commands if you’re writing an app that needs to interact with it. You can send those JSON objects to the Bitcoin API via HTTP ‘POST’ requests (it does not accept ‘GET’ requests).

For example, you can get blockchain info by sending it the following JSON object, the method is highlighted with bold text:

{"jsonrpc":"1.0","id":"rpc","method":"getblockchaininfo", "params":[]}

You can send this using a server-side language or framework such as Node.js, for example:

request.write('{"jsonrpc":"1.0","id":"rpc","method":"getblockchaininfo", "params":[]}');

Get memory pool info via the JSON RPC API:

{"jsonrpc":"1.0","id":"rpc","method":"getmempoolinfo", "params":[]}

Get a transaction’s data (in its raw hex form, which needs to be decoded by the method after this:

{"jsonrpc":"1.0","id":"rpc","method":"getrawtransaction", "params":["' + transactionid + '"]}

Decode the raw transaction data so that you can read and parse it easily:

{"jsonrpc":"1.0","id":"rpc","method":"decoderawtransaction", "params":["' + rawtransactiondata + '"]}