Calling real-time bitcoin price information

Level: Basic

Prerequisites: Local development environment

Objective: The Coinbase API offers a variety of useful bitcoin-related data. One simple datapoint that is often of interest is the price of bitcoin. In this guide we’ll walk through how to make a request to Coinbase’s prices endpoint in order to retrieve current bitcoin price information.

Featured Developer: Lawnmower

Authentication

The prices endpoint is part of a broader category of unauthenticated, read-only endpoints called Data Endpoints. As a result, authentication is not required to retrieve price information.

Grabbing price information

Before jumping into the code, know that there are three different prices available from the prices endpoint:

  • Buy price
  • Sell price
  • Spot price

As with any data, it is important to understand the difference between these prices and to request the data that is most appropriate for your needs. In this case, let’s imagine that you want simply to show your users the current bitcoin market price (also known as the “bitcoin spot price”), reflected in the US dollar equivalent.

Getting the spot price is quite simple. First, let’s take a look at a naked cURL request. Note that you can substitute any supported currency code for USD.

curl https://api.coinbase.com/v2/prices/spot?currency=USD

Example response:

{
  "data": {
    "amount": "1015.00",
    "currency": "USD"
  }
}

Easy, right? However, in most situations, you’re going to want to be able to grab the price information from within your code (i.e., not using cURL). Let’s take a look at using one of the offical coinbase libraries. First, install the library:

gem install coinbase
pip install coinbase

# or

easy_install coinbase
npm install coinbase

Now, let’s retrieve that price data:

require 'coinbase/wallet'

client = Coinbase::Wallet::Client.new(api_key: <api key>,
                                      api_secret: <api secret>,
                                      CB_VERSION: 'YYYY-MM-DD')

currency_code = 'USD'  # can also use EUR, CAD, etc.

# Make the request
price = client.spot_price({currency: currency_code})

puts "Current bitcoin price in #{currency_code}: #{price.amount}"

from coinbase.wallet.client import Client

client = Client(<api_key>, <api_secret>, api_version='YYYY-MM-DD')

currency_code = 'USD'  # can also use EUR, CAD, etc.

# Make the request
price = client.get_spot_price(currency=currency_code)

print 'Current bitcoin price in %s: %s' % (currency_code, price.amount)
var Client = require('coinbase').Client;

var client = new Client({
  'apiKey': 'API KEY',
  'apiSecret': 'API SECRET',
  'version':'YYYY-MM-DD'
});

currencyCode = 'USD'  // can also use EUR, CAD, etc.

// Make the request
client.getSpotPrice({'currency': currencyCode}, function(err, price) {
  console.log('Current bitcoin price in ' + currencyCode + ': ' +  price.data.amount);
});

If everything went smoothly, you should see the current spot price of bitcoin in USD. Now, let’s see what we can do with that information.

Displaying price information

You can style your display of price data however you’d like. However, when choosing how to display bitcoin price information, it’s important to consider what your users’ needs are.

Lawnmower is a bitcoin investment app built on the Coinbase API. As with most investment apps, their users are interested in stats regarding the performance of their investments. Particularly, users are most interested in the current value of their bitcoin holdings (essentially, how much they’d be able to sell their bitcoin for at the current moment).

As you can see above, Lawnmower’s app displays the current bitcoin sell price as Current Coinbase exchange rate. This way, users can quickly estimate the current value of their holdings and also quickly get a finger on the pulse of the bitcoin market.


Congratulations! In this short guide, you have:

  • Analyzed the Data category of API endpoints and selected Spot Price
  • Executed a cURL request for Spot Price
  • Installed a Coinbase client library to run the same request from code
  • Considered how to display the information retrieved

For additional information, don’t hesitate to visit our various API help channels:

  • Stack Overflow where we monitor the Coinbase tag
  • Our Developer Blog where you can find product updates, tutorials and other helpful tips. Scroll down to subscribe to blog updates.

To contact us about confidential or otherwise private information, email us at api@coinbase.com.