21 is an Open Source Library for the Machine-Payable Web
by John Granata, Ali Fathalian, Matthew Storus, Jian Li, and Balaji S. Srinivasan
Today we are open sourcing 21, a software package that makes it extremely easy to work with Bitcoin over HTTP.
21 facilitates seamless machine-to-machine payments by allowing you to quickly:
- Get bitcoin on any device
- Add bitcoin to any application
- Earn bitcoin on every HTTP request
To install 21, get the Docker image or simply run the following command at the terminal:
curl https://21.co | sh
Feel free to preview the script with ‘less’ before running the installer.
After installing 21, if you want to get started immediately, set up a profile at 21.co/signup, connect your LinkedIn and Github accounts to enable the free faucet, and then go through the introductory tutorial. Alternatively, continue reading to get a sense of why we built 21 — and what you can build with it.
But in short, we believe that a third web is coming. The first was the World Wide Web of documents, with hyperlinks between nodes. The second was the Social Web, with links between nodes representing friend relationships. And the third is the Machine-Payable Web, where the links between nodes represent payments between machines.
Why would such a web be desirable?
We start with the observation that you don’t want to go through a paywall every time you visit a new website. Instead, you just want to click a link. Similarly, you don’t want to enter in your credit card every time you try out a new paid API. Instead, you just want to send it some digital currency.
If you could do this, it would solve several problems in one stroke. First, it would provide a way to monetize APIs on a per-request basis without requiring developers to provide or accept credit cards. Second, it would reduce the siloization of APIs, as any developer with some bitcoin could now trivially call out to another API as easily as importing a new library call. And third, it would unify fulfillment and billing, with the HTTP request and the corresponding digital currency payment occurring within the same series of packets.
Every instance of this kind — in which a program calls a remote API and pays for it with digital currency — is a link in the Machine-Payable Web.
You couldn’t really have had a Machine-Payable Web before Bitcoin, because the ability of a machine to possess, send, and receive currency autonomously depends critically on the nature of Bitcoin private keys as bearer instruments. That is, an internet-connected machine with a private key to a Bitcoin wallet has everything it needs to transact with any other such machine in the world without human intervention.
By contrast, a machine that has only the API keys to (say) a PayPal account rather than a Bitcoin private key is hamstrung in several fundamental ways:
- First, a human must spend several days to set up a normal bank account and link it to PayPal, rather than fractions of a second to set up a Bitcoin address.
- Second, either the PayPal account or the associated bank account can be shut off at any time without the machine’s involvement.
- Third, we cannot be certain that the machine can pay any other machine on the internet due to the vagaries of PayPal’s international banking support.
- Fourth, any kind of payment that is considered atypical for a human — including particularly large, small, frequent, international, or multiplex payments — will rapidly get either the PayPal account or the bank account closed or suspended for suspected fraud, even if these are standard use cases for machine payments.
- Finally, unlike the arbitrary relationship between a user’s credentials and their PayPal balance, the relationship between a Bitcoin private key and the associated address is cryptographically irrevocable. As such it is not possible for an administrator to “reset the password” and thereby deny a machine access to its bitcoin if it possesses the correct private key.
As such, Bitcoin specifically (and digital currency more generally) is uniquely suited for handling machine-to-machine payments, thereby making it possible for developers to call new paid web services as easily as they link to new websites.
However, there are several problems that the core Bitcoin protocol does not solve, including (a) acquiring bitcoin in the first place even without mining, (b) easily setting up bitcoin-payable web services over HTTP, and (c) discovering other developers and their bitcoin-payable web services so you can buy and sell digital goods from them. Solving those problems is why we built 21.
Get Bitcoin on any device
The first problem that 21 solves after installation is that it makes it easy to get bitcoin on any device in any country (aside from those sanctioned by the US). Just install 21 from source, get it pre-installed on a Bitcoin Computer, or launch it from a Docker image. Then get bitcoin through one of five methods:
- Request bitcoin from the 21 faucet. If you link a social account (like a LinkedIn or Github account) at 21.co/signup, you can get free bitcoin for programming purposes by simply running “21 faucet” at the command line. This method works internationally and on any device and is the fastest way to start learning how to program with bitcoin. You don’t need a credit card or bank account, just a (legitimate) social networking profile.
- Buy bitcoin. If you do want to get bitcoin with a credit card or bank account, we also provide that option through our Coinbase integration. Simply link your Coinbase account to your 21.co profile and you can start buying Bitcoin at the command line with “21 buybitcoin”.
- Mine bitcoin. If you have a 21 Bitcoin Computer with a built-in mining chip, you can mine bitcoin in the foreground or background with “21 mine”.
- Earn bitcoin via microtasks. With the “21 earn” command you can do microtasks for bitcoin. This feature is in early access release; please get in touch with firstname.lastname@example.org if you’d like to test it.
- Sell machine resources for bitcoin. Finally, with the “21 sell” command you can instruct your computer to sell its spare machine resources for micropayments, kind of like “Airbnb for machines”. This command works by turning your computer into a miniature server that connects to the 21 Marketplace (21.co/mkt).
The bitcoin obtained from any of these methods is stored in your local 21 wallet, a full featured Hierarchical Deterministic wallet. Each of these methods complements the others, as they have different time constants and prerequisites. We welcome pull requests that add support for more ways to get digital currency or other useful features.
Add Bitcoin to any app
Once you have some bitcoin in your local wallet, the 21 command line and associated libraries make it easy to write bitcoin-payable programs. Our approach here is different from (and complementary to) other projects like Ethereum; rather than trying to innovate on what can be done with distributed state per se, our focus is on integrating digital currency with key internet technologies like Linux, Python, Django, Flask, and HTTP.
Put another way, the simplest application of distributed state is simply a bitcoin payment: debit one address and credit another address, thereby updating a database (the Bitcoin Blockchain) distributed over several thousand nodes. Micropayments are more challenging, but increasingly doable through a variety of methods to reduce traffic on the Blockchain. And that’s the second part of what 21 provides: namely, seamless micropayment support for modern web applications.
But don’t take our word for it — check out this snippet to see how you can do bitcoin micropayments with one line of code, or read this tutorial on how to use 21 with Django and Heroku! Alternatively, if you want to run a little bitcoin-payable web service on a piece of dedicated hardware rather than in the cloud, you can go to 21.co/diy to learn how to build your own Bitcoin Computer.
Earn Bitcoin on every HTTP Request
So you’ve installed 21 onto your machine and used it to get some bitcoin. You’ve also built your first machine-payable web service. Now what? You’ll want to find buyers for your machine-payable web service to send you some bitcoin, and sellers of machine-payable web services to spend your bitcoin.
That’s the third part of 21: programmatic access to a full micropayments marketplace at 21.co/mkt where you can publish your machine-payable endpoints, along with a profile page under your username where you can monitor your earnings. In addition to profile pages, the members of the 21 developer community also congregate in our Slack channel at slack.21.co. We like to think of this as perhaps the first capitalist network: a social network where the primary purpose isn’t racking up likes or favorites, but trading digital goods for digital currency with a network of like-minded peers.
21 is now free software, and it turns any computer into a Bitcoin Computer. After you install 21, you can get bitcoin on any device in almost any country without a bank account or credit card, add micropayments to any app, and start earning bitcoin on every HTTP request. To get started, sign up here and then read the free tutorial. Then join fellow 21 developers in our Slack community at slack.21.co, and start building the machine-payable web!