Welcome to Bitcoinlib’s documentation!

Bitcoin cryptocurrency Library writen in Python.

Allows you to create a fully functional wallet with a single line of code. Use this library to create and manage transactions, addresses/keys, wallets, mnemonic password phrases and blocks with simple and straightforward Python code.

You can use this library at a high level and create and manage wallets from the command line or at a low level and create your own custom made transactions, scripts, keys or wallets.

The BitcoinLib connects to various service providers automatically to update wallets, transaction and blockchain information.


This Bitcoin Library contains a wallet implementation using SQLAlchemy and SQLite3, MySQL or PostgreSQL to import, create and manage keys in a Hierarchical Deterministic way.

Example: Create wallet and generate new address (key) to receive bitcoins

>>> from bitcoinlib.wallets import Wallet
>>> w = Wallet.create('Wallet1')
>>> key1 = w.get_key()
>>> key1.address

Now send a small transaction to your wallet and use the scan() method to update transactions and UTXO’s

>>> w.scan()
>>> w.info()  # Shows wallet information, keys, transactions and UTXO's

When your wallet received a payment and has unspent transaction outputs, you can send bitcoins easily. If successful a transaction ID is returned

>>> t = w.send_to('1PWXhWvUH3bcDWn6Fdq3xhMRPfxRXTjAi1', '0.001 BTC')
>>> t.info()  # Shows transaction information and send results

Segregated Witness Wallet

Easily create and manage Segwit wallets. Both native Segwit with base32/bech32 addresses and P2SH nested Segwit wallets with traditional addresses are available.

Create a native single key P2WPKH wallet:

>>> from bitcoinlib.wallets import Wallet
>>> w = Wallet.create('segwit_p2wpkh', witness_type='segwit')
>>> w.get_key().address

Or create a P2SH nested single key P2SH_P2WPKH wallet:

>>> from bitcoinlib.wallets import Wallet
>>> w = Wallet.create('segwit_p2sh_p2wpkh', witness_type='p2sh-segwit')
>>> w.get_key().address

Wallet from passphrase with accounts and multiple currencies

The following code creates a wallet with two bitcoin and one litecoin account from a Mnemonic passphrase. The complete wallet can be recovered from the passphrase which is the masterkey.

from bitcoinlib.wallets import Wallet, wallet_delete
from bitcoinlib.mnemonic import Mnemonic

passphrase = Mnemonic().generate()
w = Wallet.create("Wallet2", keys=passphrase, network='bitcoin')
account_btc2 = w.new_account('Account BTC 2')
account_ltc1 = w.new_account('Account LTC', network='litecoin')

Multi Signature Wallets

Create a Multisig wallet with 2 cosigners which both need to sign a transaction.

from bitcoinlib.wallets import Wallet
from bitcoinlib.keys import HDKey

NETWORK = 'testnet'
k1 = HDKey('tprv8ZgxMBicQKsPd1Q44tfDiZC98iYouKRC2CzjT3HGt1yYw2zuX2awTotzGAZQEAU9bi2M5MCj8iedP9MREPjUgpDEBwBgGi2C8eK'
           '5zNYeiX8', network=NETWORK)
k2 = HDKey('tprv8ZgxMBicQKsPeUbMS6kswJc11zgVEXUnUZuGo3bF6bBrAg1ieFfUdPc9UHqbD5HcXizThrcKike1c4z6xHrz6MWGwy8L6YKVbgJ'
           'MeQHdWDp', network=NETWORK)
w1 = Wallet.create('multisig_2of2_cosigner1', sigs_required=2,
                     keys=[k1, k2.public_master(multisig=True)], network=NETWORK)
w2 = Wallet.create('multisig_2of2_cosigner2',  sigs_required=2,
                     keys=[k1.public_master(multisig=True), k2], network=NETWORK)
print("Deposit testnet bitcoin to this address to create transaction: ", w1.get_key().address)

Create a transaction in the first wallet

t = w1.sweep('mwCwTceJvYV27KXBc3NJZys6CjsgsoeHmf', min_confirms=0)

And then import the transaction in the second wallet, sign it and push it to the network

t2 = w2.transaction_import(t)

Command Line Tool

With the command line tool you can create and manage wallet without any Python programming.

To create a new Bitcoin wallet

$ python bitcoinlib/tools/clw.py new -w newwallet
Command Line Wallet - BitcoinLib 0.6.14

CREATE wallet 'newwallet' (bitcoin network)
Passphrase: sibling undo gift cat garage survey taxi index admit odor surface waste
Please write down on paper and backup. With this key you can restore your wallet and all keys

Type 'yes' if you understood and wrote down your key: yes
Wallet info for newwallet
=== WALLET ===
 ID                             21
 Name                           newwallet
 Scheme                         bip32
 Multisig                       False
 Witness type                   segwit
 Main network                   bitcoin
 Latest update                  None

= Wallet Master Key =
 ID                             177
 Private                        True
 Depth                          0

- NETWORK: bitcoin -
- - Keys
  182 m/84`/0`/0`/0/0              bc1qza24j7snqlmx7603z8qplm4rzfkr0p0mneraqv    address index 0                        0.00000000 ₿

- - Transactions Account 0 (0)

= Balance Totals (includes unconfirmed) =

You can use the command line wallet ‘clw’ to create simple or multisig wallets for various networks, manage public and private keys and managing transactions.

For the full command line wallet documentation please read


Service providers

Communicates with pools of bitcoin service providers to retreive transaction, address, blockchain information. To push a transaction to the network. To determine optimal service fee for a transaction. Or to update your wallet’s balance.

Example: Get estimated transactionfee in Sathosis per Kb for confirmation within 5 blocks

>>> from bitcoinlib.services.services import Service
>>> Service().estimatefee(5)

Other Databases

Bitcoinlib uses the SQLite database by default but other databases are supported as well. See http://bitcoinlib.readthedocs.io/en/latest/_static/manuals.databases.html for instructions on how to use MySQL or PostgreSQL.

More examples

For more examples see https://github.com/1200wd/bitcoinlib/tree/master/examples


This library is still in development, please use at your own risk and test sufficiently before using it in a production environment.

Schematic overview

_images/classes-overview.jpg _images/classes-overview-detailed.jpg

Indices and tables