Guide: Send and Receive Bitcoin

Level: Basic

Prerequisites: Local development environment

Ability to send and receive bitcoin is one of the core building blocks for any bitcoin application. Coinbase API provides a very easy way to do that either by using an API Key to send/receive funds using your own wallets, or by using Coinbase Connect (OAuth2) to do it on behalf of other Coinbase users.

In this short guide, you will learn how to send and receive money using Coinbase API. Here are the steps we will go through:

  • Generating your own bitcoin addresses to receive funds
  • Sending funds from your Coinbase wallet to a bitcoin address or an email
  • Requesting funds using email addresses

Receive Funds

First, let’s generate a bitcoin address for your primary wallet, so that you can receive money from other people(or bots). We strongly encourage that you don’t reuse your addresses and generate a new one every time instead. Follow the steps below to create a new address for your primary coinbase wallet:

require 'coinbase/wallet'

client = Coinbase::Wallet::Client.new(api_key: <api key>,
                                      api_secret: <api secret>)

# Get your primary coinbase account
primary_account = client.primary_account
address = primary_account.create_address
puts address
from coinbase.wallet.client import Client

client = Client(<api_key>,
                <api_secret>)

# Get your primary coinbase account
primary_account = client.get_primary_account()
address = primary_account.create_address()
print address
var Client = require('coinbase').Client;

var client = new Client({
  'apiKey': 'API KEY',
  'apiSecret': 'API SECRET',
});

var address = null;

client.getAccount('primary', function(err, account) {
  account.createAddress(function(err, addr) {
    console.log(addr);
    address = addr;
  });
});

You can now set up a webhook notification for the address that you just created to get notified when there are incoming transactions. You can learn more about notifications here.

Send Funds

Now that you have a bitcoin address from the previous step, let’s send some funds to it (since it is your own address, the funds will just come back to your primary wallet). We are going to send 0.01 BTC using the API. Please follow the steps below:

# Get your primary coinbase account
primary_account = client.primary_account

tx = primary_account.send({'to' => address,
                           'amount' => '0.01',
                           'currency' => 'BTC'})
puts tx
# Get your primary coinbase account
primary_account = client.get_primary_account()

tx = primary_account.send_money(to=address,
                                amount='0.01',
                                currency='BTC')
print tx

client.getAccount('primary', function(err, account) {
  account.sendMoney({'to': address,
                     'amount': '0.01',
                     'currency': 'BTC'}, function(err, tx) {
    console.log(tx);
  });
});

Request Funds

In addition to sending and receiving money, you can also request funds from other Coinbase users by using their email address. Below we are going to request 0.1 BTC from bitdiddle@example.com:

# Get your primary coinbase account
primary_account = client.primary_account
tx = primary_account.request({'to' => 'bitdiddle@example.com',
                              'amount' => '0.1',
                              'currency' => 'BTC'})
puts tx
# Get your primary coinbase account
primary_account = client.get_primary_account()
tx = primary_account.request_money(to='bitdiddle@example.com',
                                   amount='0.1',
                                   currency='BTC')

client.getAccount('primary', function(err, account) {
  account.requestMoney({'to': 'bitdiddle@example.com',
                        'amount': '0.1',
                        'currency': 'BTC'}, function(err, tx) {
    console.log(tx);
  });
});

Free Transactions

Coinbase allows developers that want to build micropayments applications to facilitate instant, free transactions between Coinbase accounts with off-blockchain transactions. Off-blockchain transactions are settled on our internal ledger rather than on the Bitcoin blockchain and have cost and speed benefits over on-blockchain transactions.

If you would prefer to do on-blockchain micropayments (sending to bitcoin addresses outside of Coinbase), we offer 25 free on-blockchain transactions per day.

After 25 free transactions, you must pay a fee for each transaction. This can be done by including a fee argument on a Send Money request.

Next Steps

In this guide you learned how to send and receive bitcoin using your Coinbase wallets. Next, learn how to buy and sell bitcoin using our API.