Guide: Create a Bitcoin Wallet

In this short guide, you’ll learn how to programmatically create a bitcoin wallet, how to send bitcoin into that wallet, and how to retrieve data from that wallet.

Tasks we will cover in this short tutorial:

  • Authenticate your account (create an API Key)
  • Select appropriate permissions
  • Create a bitcoin wallet
  • Send and receive bitcoin
  • Retrieve data from your bitcoin wallet

Authentication

Create an API key for your account at https://coinbase.com/settings/api

Each API key is associated with a set of permissions, also called scopes. When you generate the API key, you can select the permissions that you need for your application to work. You should get in the habit of requesting only the permissions that your app needs. Requesting unnecessary permissions may not explicitly harm your app, but it’s good security practice not to authorize more access than you need.

Permissions

In order to be able to create new wallets, send funds and to check balances, you will need the following permissions authorized for your API key:

  • wallet:accounts:create
  • wallet:accounts:read
  • wallet:transactions:send
  • wallet:transactions:read
  • wallet:addresses:create

For more information about permissions, read here.

Install Coinbase API client

The easiest way to get started with Coinbase Wallet API is to use an official client library:

pip install coinbase

# or

easy_install coinbase
gem install coinbase
npm install coinbase
composer require coinbase/coinbase

For other languages, see full list here.

Create a Bitcoin Wallet

You can use the Wallet API to create new bitcoin wallets on Coinbase or to get information about your existing wallets, such as balance and transaction history. You can also receive notifications when funds arrive to your wallet. Today we will create a new Coinbase wallet.

What is a bitcoin wallet? A bitcoin wallet is a collection of bitcoin addresses. A bitcoin address is used to specify where to send bitcoin. An address can be used multiple times, but by default Coinbase uses a new address for every transaction.

Advanced: You can also securely interact with other users’ wallets (with their permission) by using Coinbase Connect (OAuth2)

First, let’s authenticate with the Wallet API using our newly created API key.

from coinbase.wallet.client import Client

client = Client(
    <api_key>,
    <api_secret>)
require 'coinbase/wallet'

client = Coinbase::Wallet::Client.new(api_key: <api key>,
                                      api_secret: <api secret>)
var Client = require('coinbase').Client;

var client = new Client({
  'apiKey': 'API KEY',
  'apiSecret': 'API SECRET',
});
use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);

Now that we’ve authenticated, we can list the wallets and transactions in our account. A default bitcoin wallet is created for you to get started.

accounts = client.get_accounts()
for account in accounts.data:
  balance = account.balance
  print "%s: %s %s" % (account.name, balance.amount, balance.currency)
  print account.get_transactions()
client.accounts.each do |account|
  balance = account.balance
  puts "#{account.name}: #{balance.amount} #{balance.currency}"
  puts account.transactions
end
client.getAccounts({}, function(err, accounts) {
  accounts.forEach(function(acct) {
    console.log(acct.name + ': ' + acct.balance.amount + ' ' + acct.balance.currency);
    acct.getTransactions(null, function(err, txns) {
      txns.forEach(function(txn) {
        console.log('txn: ' + txn.id);
      });
    });
  });
});
$accounts = $client->getAccounts();
foreach ($accounts as &$account) {
  $balance = $account->getBalance();
  echo $account->getName() . ": " . $balance->getAmount() . $balance->getCurrency() .  "\r\n";
  print_r($client->getAccountTransactions($account));
}

Now let’s create a new wallet.

account = client.create_account(name="New Wallet")
balance = account.balance
print "%s: %s %s" % (account.name, balance.amount, balance.currency)
account = client.create_account(name: "New Wallet")
balance = account.balance
puts "#{account.name}: #{balance.amount} #{balance.currency}"
client.createAccount({'name': 'New Wallet'}, function(err, acct) {
  console.log(acct.name + ': ' + acct.balance.amount + ' ' + acct.balance.currency);
});
use Coinbase\Wallet\Resource\Account;

$account = new Account();
$account->setName('New Wallet');
$client->createAccount($account);

Now that we’ve created a new bitcoin wallet, let’s see what we can do with it.

Sending and receiving bitcoin

# Generate a new bitcoin address for your primary account:
primary_account = client.get_primary_account()
address = account.create_address() # You created this account in the previous step

# Send coins to the new account from your primary account:
primary_account.send_money(to=address.address, amount='0.01', currency='BTC', description='For being awesome!')
# Generate a new bitcoin address for your primary account:
primary_account = client.primary_account
address = account.create_address # You created this account in the previous step

# Send coins to the new account from your primary account:
primary_account.send(to: address.address, amount: '0.01', currency: 'BTC', description: 'For being awesome!')
client.getAccount('primary', function(err, primaryAccount) {
  // Generate a new bitcoin address for the account from previous steps:
  account.createAddress(null, function(err, address) {
    // Send coins to the new account from your primary account:
    primaryAccount.sendMoney({'to': address.address,
                              'amount': '0.01',
                              'currency': 'BTC',
                              'description': 'For being awesome!'}, function(err, tx) {
       console.log(tx);
    });
  });
});
$primaryAccount = $client->getPrimaryAccount();

// Generate a new bitcoin address for your primary account:
use Coinbase\Wallet\Resource\Address;

$address = new Address();
$client->createAccountAddress($account, $address);

// Send coins from the new account to your primary account:
use Coinbase\Wallet\Enum\CurrencyCode;
use Coinbase\Wallet\Resource\Transaction;
use Coinbase\Wallet\Value\Money;

$transaction = Transaction::send();
$transaction->setToBitcoinAddress($address->getAddress());
$transaction->setAmount(new Money(0.01, CurrencyCode::BTC));
$transaction->setDescription('For being awesome!');

$client->createAccountTransaction($primaryAccount, $transaction);
print_r($transaction);

Now you can check your primary account balance. Notice how the balance changed?

# View the last transaction
print primary_account.get_transactions()[-1]

# After some time, the transaction should complete and your balance should update
primary_account.refresh()
balance = primary_account.balance
print "%s: %s %s" % (primary_account.name, balance.amount, balance.currency)
# View the last transaction
puts primary_account.transactions.last

# After some time, the transaction should complete and your balance should update
primary_account.refresh!
balance = primary_account.balance
puts "#{primary_account.name}: #{balance.amount} #{balance.currency}"
// refresh the account
client.getAccount(primaryAccount.id, function(err, acct) {
  console.log(acct.name + ': ' + acct.balance.amount + ' ' + acct.balance.currency);
});
// After some time, the transaction should complete and your balance should update
$client->refreshAccount($primaryAccount);

$balance = $primaryAccount->getBalance();
echo $primaryAccount->getName() . ": " . $balance->getAmount() . $balance->getCurrency() .  "\r\n";

Congratulations!

You’ve learned how to:

  • Create a bitcoin wallet
  • Authenticate with the Coinbase API
  • Set appropriate permissions
  • Send bitcoin programmatically
  • Retrieve data from your bitcoin wallet

Next: Send and receive bitcoin →