How to Tip Bitcoin With a Single Command

This article was prepared by a guest contributor to ProgrammableWeb. The opinions expressed in this article are the author's own and do not necessarily reflect the view of ProgrammableWeb or its editorial staff.

This article was prepared by a guest contributor to ProgrammableWeb. The opinions expressed in this article are the author's own and do not necessarily reflect the view of ProgrammableWeb or its editorial staff.

If you’re like me, you spend the majority of your time at the command line. A power user can write code, install packages and configure servers, all without leaving the terminal. If I wanted to send a friend a candy’s worth of bitcoin from the command line, here’s how:

$ ./send_a_candy.sh 17astdTmG8zzVmry8mV8A7atAr3XefEgRX

This script sends 200 bits (0.0002 bitcoins, or about 4 cents) to any provided bitcoin address. How does it work? It runs a slightly longer but simple command using BlockCypher’s microtransaction API. Here’s my script to send a candy’s worth of bitcoin for Ubuntu (should be similar for any *nix):

The third line uses OpenSSL to decrypt a password-protected file that contains a JSON document (you can see how to create the file below). The document describes a bitcoin microtransaction in just three lines. Once decrypted, the document is piped into a curl command that will send it using an HTTP POST on the BlockCypher API. The API then does all the grunt work of building a full bitcoin transaction, minimizing the fees to fractions of a cent and sending the transaction all over the world on the bitcoin network.

To send money with bitcoin, all that’s required is a private key, which is like a very long, random password. The private key is used to digitally sign a transaction, proving that the money belongs to you. With that private key comes a public key and an address. To receive bitcoin, all you need is a person’s address. Because the private key can be used by anyone to spend money, it’s sensitive data, which is why we only store it encrypted.

All the microtransaction API needs is four fields:

from_private: the private key for the address you’re sending from (for more advanced scenarios, you can also send a public key and sign yourself)

value_satoshis: the amount sent in satoshis; 100 million satoshis make a bitcoin

to_address: the bitcoin address to send the money to

token: the BlockCypher API token

Each field can be provided either in a JSON document or as a URL property. To make tipping from the command line easy, the to_address field is passed as an argument to the script and then added as a URL property. All the other fields are in a precreated JSON document that doesn’t need to change.

Setting this up for yourself requires only a few steps. We’ll start with send_a_candy.sh, which sends 20,000 satoshis (0.0002 bitcoins, or about 4 cents).

5. Finally, create send_a_candy.sh and run it (passing an address) to share some bitcoins!

And for friends who are extra nice, I also have a send_a_beer.sh script sending 0.02 bitcoins (2,000,000 satoshis) just by replacing the amount in the JSON document.

For more details, check out the BlockCypher microtransaction API. It works for any type of microtransaction. Also make sure to check all the other BlockCypher bitcoin APIs — maybe one of them will give you an idea for a fun project.

About the author:Matthieu Riou
Matthieu Riou, CTO and Founder of BlockCypher. BlockCypher’s web services enable block chain applications to be built easily and reliably. Matthieu has architected many software systems at MommaZoo, C3, Intalio, BEA, Reef, BNP, and Societe Generale. He's a member and former VP at the Apache Software Foundation and has founded and mentored many different open source projects, from distributed databases to build tools. Matthieu knows 10+ programming languages and all tools ranging from Lisp to Java and Node.js. He holds a BS and MS in Computer Science and Artificial Intelligence from Ecole des Mines d'Ales.