DocumentationLog In

Buy and Sell BTC

Level: Basic

Prerequisites: Local development environment

If you are building a bitcoin application, chances are, you want your users to have some bitcoin to be able to use your app. You can use Coinbase API to help your users buy bitcoin. In this short guide you'll learn how to buy and sell bitcoin programmatically. We will set up very simple price thresholds to buy or sell bitcoin automatically.

Payment Methods

First, you need to make sure you have a verified payment method associated
with your Coinbase account.

require 'coinbase/wallet'

client = <api key>,
                                      api_secret: <api secret>,
                                      CB_VERSION: 'YYYY-MM-DD')
payment_methods = client.payment_methods
puts payment_methods
from coinbase.wallet.client import Client

client = Client(<api_key>,

payment_methods = client.get_payment_methods()
var Client = require('coinbase').Client;

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

client.getPaymentMethods(function(err, paymentMethods) {

Buy and Sell Bitcoin

Now that you've confirmed that you can buy and sell bitcoin, let's go ahead and set up simple price checks that will trigger either a buy or a sell when the price of bitcoin crosses the corresponding threshold:

account = client.primary_account
payment_method = client.payment_methods.first

buy_price_threshold  = 200
sell_price_threshold = 500

buy_price  = client.buy_price({currency: 'USD'})
sell_price = client.sell_price({currency: 'USD'})

if sell_price.amount.to_f <= sell_price_threshold
  sell = account.sell({"amount" => "1",
                       "currency" => "BTC",
                       "payment_method" =>})


if buy_price.amount.to_f <= buy_price_threshold
  buy ={"amount" => "1",
                     "currency" => "BTC",
                     "payment_method" =>})

account = client.get_primary_account()
payment_method = client.get_payment_methods()[0]

buy_price_threshold  = 200
sell_price_threshold = 500

buy_price  = client.get_buy_price(currency='USD')
sell_price = client.get_sell_price(currency='USD')

if float(sell_price.amount) <= sell_price_threshold:
  sell = account.sell(amount='1',

if float(buy_price.amount) <= buy_price_threshold:
  buy ='1',
var buyPriceThreshold  = 200;
var sellPriceThreshold = 500;

client.getAccount('primary', function(err, account) {

  client.getSellPrice({'currency': 'USD'}, function(err, sellPrice) {
    if (parseFloat(sellPrice['amount']) <= sellPriceThreshold) {
      account.sell({'amount': '1',
                    'currency': 'BTC'}, function(err, sell) {

  client.getBuyPrice({'currency': 'USD'}, function(err, buyPrice) {
    if (parseFloat(buyPrice['amount']) <= buyPriceThreshold) {{'amount': '1',
                   'currency': 'BTC'}, function(err, buy) {


In this guide you learned how to set up very simple price monitoring to automate buying and selling bitcoin. You can now come up with more sophisticated ways to track price changes and automate your trading.

Next: Integrate with Coinbase Connect (OAuth2) →