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
    I’m an Impostor

    This is the hardest thing I've ever had to write, much less admit to myself.  I've written resignation letters from jobs I've loved, I've ended relationships, I've failed at a host of tasks, and let myself down in my life.  All of those feelings were very...

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

Incredible Demos

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!