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 Cube

    CSS cubes really showcase what CSS has become over the years, evolving from simple color and dimension directives to a language capable of creating deep, creative visuals.  Add animation and you've got something really neat.  Unfortunately each CSS cube tutorial I've read is a bit...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Using jQuery and MooTools Together

    There's yet another reason to master more than one JavaScript library: you can use some of them together! Since MooTools is prototype-based and jQuery is not, jQuery and MooTools may be used together on the same page. The XHTML and JavaScript jQuery is namespaced so the...

  • By
    CSS Ellipsis Beginning of String

    I was incredibly happy when CSS text-overflow: ellipsis (married with fixed width and overflow: hidden was introduced to the CSS spec and browsers; the feature allowed us to stop trying to marry JavaScript width calculation with string width calculation and truncation.  CSS ellipsis was also very friendly to...

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!