Get Bitcoin Value with curl or Node.js

By  on  
bitcoin

Last year I started dabbling in bitcoin.  Of course I was immediately cocky as bitcoin value went up 20% as soon as I bought it, then was humbled as bitcoin's value tumbled down 50%.  From boathouse to outhouse.  From caviar to ramen noodles.  It was brutal.

Anyways, I was often checking the price of bitcoin because it would move up and down quite quickly.  I was going to Coinbase to check but as a developer I prefer to do something nerdy to get the value.  I've taken a few moments to get the value of bitcoin in a few programmatic ways.

Get Bitcoin Value via Shell + curl

If I want to be low-level "nerd alert" mode, I'll use this command:

curl -s http://api.coindesk.com/v1/bpi/currentprice.json | python -c "import json, sys; print(json.load(sys.stdin)['bpi']['USD']['rate'])"

That command will provide the USD value of a single bitcoin.  You can use GBP or EUR if you prefer those currencies. Services other than CoinDesk's main feed may provide another currency value.

Get Bitcoin Value via Node.js

The lowest level server-side JavaScript would look like this:

var http = require('http');

http.get({
        host: 'api.coindesk.com',
        path: '/v1/bpi/currentprice.json'
        },
        function(response) {
                // Continuously update stream with data
                var body = '';
                response.on('data', function(d) { body += d; });
                response.on('end', function() {

                        // Data reception is done, do whatever with it!
                        var parsed = JSON.parse(body);
                        console.log(parsed.bpi.USD.rate);
                });
        }
);

As you probably know, this post is less about the code and more about working with the CoinDesk API endpoint.  CoinDesk does provide other endpoints to get historical bitcoin data, but I'm more concerned about my money now.

Recent Features

  • By
    Create a CSS Flipping Animation

    CSS animations are a lot of fun; the beauty of them is that through many simple properties, you can create anything from an elegant fade in to a WTF-Pixar-would-be-proud effect. One CSS effect somewhere in between is the CSS flip effect, whereby there's...

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

Incredible Demos

  • By
    Fancy Navigation with MooTools JavaScript

    Navigation menus are traditionally boring, right? Most of the time the navigation menu consists of some imagery with a corresponding mouseover image. Where's the originality? I've created a fancy navigation menu that highlights navigation items and creates a chain effect. The XHTML Just some simple...

  • By
    Table Cell and Position Absolute

    If you follow me on Twitter, you saw me rage about trying to make position: absolute work within a TD element or display: table-cell element.  Chrome?  Check.  Internet Explorer?  Check.  Firefox?  Ugh, FML.  I tinkered in the console...and cussed.  I did some researched...and I...

Discussion

  1. Andrew

    You may want to be compatible with Python 3 there (add () for print, and it is still compatible with Python 2):

    python -c "import json, sys; print(json.load(sys.stdin)['bpi']['USD']['rate'])"
  2. MaxArt

    No wonder you’re worried about your money. I’ve always had the hunch that cryptocurrencies are nothing more than a fad.
    That’s why I’ve never joined the party, even though I may be wrong.

    • I own 10 bitcoin at an average of ~$340, so I didn’t bet the farm on it, but I am down a decent amount. Just an experiment. :)

  3. This python thing is awful, please try jq (http://stedolan.github.io/jq/):

    http -b get http://api.coindesk.com/v1/bpi/currentprice.json | jq .bpi.USD.rate
    

    http is the httpie tool http://httpie.org

  4. Use curl -s for curl silent mode just to make it cleaner :)

  5. Tada! Groovy one-liner:

    new groovy.json.JsonSlurper().parse('http://api.coindesk.com/v1/bpi/currentprice.json'.toURL()).bpi.USD.rate
    

    or from the command line:

    groovy -e "println (new groovy.json.JsonSlurper().parse('http://api.coindesk.com/v1/bpi/currentprice.json'.toURL()).bpi.USD.rate)"
    
  6. I preferred to just use awk and tr to trim the output:

    curl -s http://api.coindesk.com/v1/bpi/currentprice.json | awk -F ':' '{print $17}' | awk -F ',' '{print $1}' | tr -d "\""

    and I set it up in ~/.bash_profile to create an alias:

    alias btc='curl -s http://api.coindesk.com/v1/bpi/currentprice.json | awk -F '"'"':'"'"' '"'"'{print $17}'"'"' | awk -F '"'"','"'"' '"'"'{print $1}'"'"' | tr -d "\""'
  7. Chris Kaltwasser

    If you have json_pp available, here’s a quick recipe that works and not subject to some issues I had with other examples using python.

    curl -s http://api.coindesk.com/v1/bpi/currentprice.json | json_pp | grep -A5 "\"USD\"" | grep rate_float
    

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!