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.

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:

$ ./ 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):

1 $ cat ./
2 #!/bin/bash
3 openssl enc -d -aes-256-cbc -nosalt -base64 -in send_a_candy.json.aes256 2>&1 | \
4  CURL -H "Content-Type: application/ JSON" -d @-$1

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, which sends 20,000 satoshis (0.0002 bitcoins, or about 4 cents).

1. Get a new tipping address (do not reuse the one below):

$ curl -X POST
 "private": "da01587298ff1721a968539e87329abe18f9b20cb46124eb650671662d3acd1a",
 "public": "02d47eaaca7f9b96a977c996396d1668a729155c02a18a37429ca5be9cfda91893",
 "address": "1PjfjXBM4GQs8FaNFESiUZzQwVxMHXdBXG",
 "wif": "L4XV4V5ACQmrtJn7ombma3sdqNrn1T9FFfwjLUaHVmd7Qz5iNHTt"

2. Send a small amount of money to the new address using your regular wallet, just enough for tips.

3. Create a JSON document to describe the transaction, replacing the private key. Let’s start with 20,000 satoshis (~ 4 cents).

$ cat send_a_candy.json
 "from_private": "da01587298ff1721a968539e87329abe18f9b20cb46124eb650671662d3acd1a",
 "value_satoshis": 2000000,
 "token": "37048cd01f250eca918ab55b658de3ec"

4. Encrypt it so the private key is not in clear on your machine.

$ openssl enc -aes-256-cbc -nosalt -base64 -in send_a_candy.json -out send_a_candy.json.aes256

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

And for friends who are extra nice, I also have a 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.

Be sure to read the next Bitcoin article: BlockCypher Microtransaction API Lowers Micropayments Cost