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
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

  • By
    Create Twitter-Style Buttons with the Dojo Toolkit

    I love that JavaScript toolkits make enhancing web pages incredibly easy. Today I'll cover an effect that I've already coded with MooTools: creating a Twitter-style animated "Sign In" button. Check out this five minute tutorial so you can take your static...

  • By
    Jack Rugile’s Favorite CodePen Demos

    CodePen is an amazing source of inspiration for code and design. I am blown away every day by the demos users create. As you'll see below, I have an affinity toward things that move. It was difficult to narrow down my favorites, but here they are!

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!