Every private key has a balance property which is initially set to 0. This internal balance will always be in satoshi.

>>> from bit import PrivateKeyTestnet
>>> key = PrivateKeyTestnet('cU6s7jckL3bZUUkb3Q2CD9vNu8F1o58K5R5a3JFtidoccMbhEGKZ')
>>> key.balance

You can query the blockchain for the current balance by calling get_balance(). It takes an optional argument currency (see Supported Currencies) and returns a formatted string rounded down to the proper number of decimal places. By default it will return the balance in satoshi.

>>> key.get_balance('btc')
>>> key.balance

After you communicate with the network, you can view the internal balance in terms of other currencies using balance_as().

>>> key.balance_as('mbtc')
>>> key.balance_as('usd')
>>> key.balance_as('gbp')
>>> key.balance_as('cny')

See also Unsupported Currencies.


You can see what unspent transaction outputs (commonly referred to as UTXO) you have available to spend by calling get_unspents().

>>> key = PrivateKeyTestnet('cU6s7jckL3bZUUkb3Q2CD9vNu8F1o58K5R5a3JFtidoccMbhEGKZ')
>>> key.unspents
>>> key.get_unspents()
[Unspent(amount=82721202, confirmations=688, script='76a914990ef60d63b5b5964a1c2282061af45123e93fcb88ac', txid='2ae6f3cc21cf11cfc7ad5d79436ecf08521df6a106691dcd1672b076138ea6ff', txindex=1)]
>>> key.unspents
[Unspent(amount=82721202, confirmations=688, script='76a914990ef60d63b5b5964a1c2282061af45123e93fcb88ac', txid='2ae6f3cc21cf11cfc7ad5d79436ecf08521df6a106691dcd1672b076138ea6ff', txindex=1)]

As you can see, this address has 1 available UTXO to spend worth 82721202 satoshi. get_balance() uses this method by totalling the amount of all UTXO. You will never have to use this directly.

Transaction History

Get a list of all transactions from newest to oldest by calling get_transactions().

>>> key = PrivateKeyTestnet('cU6s7jckL3bZUUkb3Q2CD9vNu8F1o58K5R5a3JFtidoccMbhEGKZ')
>>> key.transactions
>>> key.get_transactions()

Presently this just returns each transaction’s hash for further lookup. In a future release they will become proper objects.


Bit communicates with the blockchain using trusted third-party APIs. Specifically, it can access:


Private key network operations use NetworkAPI. For each method, it polls a service and if an error occurs it tries another.


BlockchainAPI does only track up to 1000 unspent transaction outputs (UTXOs) and will raise ExcessiveAddress if polling unspents on an address with 1000 or more UTXOs.

BlockstreamAPI does only track up to 50 _unconfirmed_ transactions and will raise ExcessiveAddress if polling unspents on an address with 50 or more unconfirmed. It may occasionally also raise ExcessiveAddress on an address with a history of many (1000+) transactions.

In those cases where the API may raise errors due to ExcessiveAddress it is advised to use your own remote Bitcoin node to poll, see below.

Using a Remote Bitcoin Core Node

Bit can alternatively use a remote Bitcoin node to interact with the blockchain.

Instead of using web APIs to interact with the Bitcoin blockchain it is possible to connect to a remote Bitcoin Core node. Bitcoin Core however is not meant as a full-fledged blockchain explorer and does only keep track of addresses associated with its wallet.

Transaction Database Index and txindex

By default Bitcoin Core does not maintain any transaction-level data except for those transactions - in the mempool or relay set - pertinent to addresses in your wallet - pertinent to your “watch-only” addresses

If querying arbitrary transactions is important then the option txindex must be set to true (1) inside the Bitcoin Core configuration file. Setting this option does not allow querying for arbitrary data on addresses, but only for those that are added to the wallet in for Bitcoin Core to be fetched.

Configuring Bitcoin Core

To use Bitcoin Core as a remote node it must accept remote procedure call (RPC) methods from the host running Bit. A username and password for the RPC must be defined inside the Bitcoin Core configuration file.

Adding a RPC user and password can be done with the rpcauth option that uses a hashed password. The field comes in the format: <USERNAME>:<SALT>$<HASH>. A canonical python script is included inside Bitcoin Core’s share/rpcuser directory. This python script creates such a user/password combination (note that you are given the password, you do not get to specify it yourself).

Run the script, e.g.:

>>> python ./ username
String to be appended to bitcoin.conf:
Your password:

Note that this option can be specified multiple times.

Finally, make sure that Bitcoin Core will accept RPC methods from the host running Bit. The option rpcallowip=<ip> allows RPC connections from specified host IP. The default port used to listen to RPC methods can be set with the option rpcport=<port>; the default values being 8332 for mainnet, 18332 for testnet and 18443 for regtest.

A default configuration file can be found inside the Bitcoin Core directory under share/examples/bitcoin.conf.

Connecting To The Node

Connecting to a remote Bitcoin Core node from Bit is straight forward. It can be done by calling connect_to_node(), e.g.

>>> from import NetworkAPI
>>> NetworkAPI.connect_to_node(user='username', password='password', host='localhost', port=18443, use_https=False, testnet=True)

It is possible to connect to both a testnet and mainnet node by calling connect_to_node() twice with the arguments accordingly.

Using The Remote Bitcoin Core Node

After connecting to the remote node all API calls done by NetworkAPI are seamlessly redirected to it.

Adding An Address To The Internal Wallet Of A Node

Bit will poll the node for data on an address using Bitcoin Core’s internal wallet. An address to poll must therefore first be imported to Bitcoin Core’s wallet.

We can directly access the Bitcoin Core node’s RPC and then use importaddress to import a specific address as follows:

>>> import bit
>>> from import NetworkAPI
>>> # Get the `node` object for direct access:
>>> node = NetworkAPI.connect_to_node(user='username', password='password', host='localhost', port=18443, use_https=False, testnet=True)
>>> key = bit.PrivateKeyTestnet()
>>> # Import an address to the node's wallet:
>>> node.importaddress(key.segwit_address, "optional-label", False)

You can read more about the RPC importaddress here.

As we had just created the new address we set the last argument in importaddress to False, which defines that the node will not rescan the blockchain for the address as it will not have any transactions yet. If you are importing a used address you must set the rescan parameter to True, as you will otherwise receive incorrect information from your node!

Performing a rescan can take several minutes.