admin 25.10.2017

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.

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

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.

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.

use Coinbase\Wallet\Client;
use Coinbase\Wallet\Configuration;

$configuration = Configuration::apiKey($apiKey, $apiSecret);
$client = Client::create($configuration);
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));
}
account = client.create_account(name="New Wallet")
balance = account.balance
print "%s: %s %s" % (account.name, balance.amount, balance.currency)
client.createAccount({'name': 'New Wallet'}, function(err, acct) {
  console.log(acct.name + ': ' + acct.balance.amount + ' ' + acct.balance.currency);
});
# 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);
# 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";
  • Create a bitcoin wallet
  • Authenticate with the Coinbase API
  • Set appropriate permissions
  • Send bitcoin programmatically
  • Retrieve data from your bitcoin wallet