How to use 21 to create and host a machine-payable API on Heroku or AWS Earn.com Follow Jun 24, 2016 · 5 min read

by Jeremy Kun, John Granata, and Balaji S. Srinivasan

In our last post, we open sourced 21, a library which makes it easy to get bitcoin, create machine-payable APIs, list them in a marketplace, and purchase these APIs with BTC. Today we’ll see how 21 can be used to host your own machine-payable APIs using industry standard tools such as Amazon Web Services, Heroku, and Django.

If you want to skip right ahead to the technical tutorials, go here, here, and here. In this blog post, though, the overall idea we want to illustrate is that the open source version of 21 is now unlocked. You can install the 21 software on any client/server pair, load the client wallet with bitcoin, and then execute machine-to-machine transactions.

On the server side, it’s your choice as to whether to list your machine-payable endpoint in the 21 Marketplace with the publish command. By doing so, you allow clients with the 21 software to automatically discover and purchase the endpoint. However, clients with the 21 software can buy your endpoint even if it’s not listed in the 21 Marketplace by using either on-chain transactions or micropayment channels.

On the client side, it’s your choice as to exactly how to load up your client wallet with bitcoin. 21 provides five different convenient ways of doing that (including a simple faucet), with the sixth being to simply just send yourself bitcoin from an external wallet by running “wallet payoutaddress” after installing 21 and sending bitcoin to the displayed address.

We illustrate both the client and server concepts here by showing how to build a fairly sophisticated machine-payable API with 21 that uses deep learning to build logos for bitcoin.

Why machine-payable APIs are useful for computationally intensive applications

Infrequent but computationally intensive applications like deep learning-powered algorithms are ideal candidates for turning into machine-payable APIs.

Clients don’t want to spend a lot of CPU time or disk space on running large compute jobs locally. They just want to outsource the computation to an API. However, the more infrequently used the API, the less they want to pay the cost in time of signing up for a new API.

Servers must recoup the costs of running large compute jobs, so they can’t offer the API for free. But even with modern tools like Stripe it’s a hassle on the server side to set up a corporate bank account and the necessary tooling for managing a paid API, including user accounts.

Our solution (tutorial, source) is to just set up a 21-powered API that accepts bitcoin micropayments from the client. For long-running server jobs, the server then generates a cryptographic token which the client can redeem for their output upon job completion.

This obviates the concept of customer signup or API keys, as your money is now your API key. You still need a bitcoin wallet on the client and server side, but there are many forces pushing for that to happen and it can now be accomplished by a convenient install in a few seconds — much faster than setting up a corporate bank account!

A Sample Machine-Payable API for Deep Learning

By way of demonstration, we’ve implemented a bitcoin-payable endpoint that uses deep learning to perform artistic style transfer. A style transfer algorithm applies the “style” of one image to another image in a more subtle way than a brute force copy/paste. It’s useful for logo generation, photo filtering, digital art, movie special effects, and more besides.

As an example, here we’ve used a 21-powered style transfer to render the 21 logo in the style of Yayoi Kusama’s Infinity Mirrored Room:

This API is only about 200 lines of Python code, not including boilerplate. Our implementation launches a fresh EC2 instance on each request, computes the requested style transfer, and then uploads the output to Imgur, returning you a link.

You can use the API in three different ways:

Download the source code from Github and deploy it yourself.

Go to Github and one-click deploy it to Heroku after configuring your AWS credentials.

Or play around with our pre-deployed version in the 21 Marketplace (full documentation here).

Buying Deep Learning from the 21 Marketplace

Regarding that last point, here is a sample API call to the 21 Marketplace that executes deep learning for bitcoin using the 21 command line tools:

21 buy "https://apps.21.co/neuralart/buy" --data '{"iterations": 200, "style":"http://i.imgur.com/GEEYfD7.jpg", "content":"http://i.imgur.com/Go86JXN.jpg"}' --maxprice 175000

Note you can get bitcoin to execute this purchase through a variety of methods, including simply sending it to your 21 wallet by generating an address with wallet payoutaddress and send bitcoin to it.

Because style transfer algorithms take around 15 minutes to run on specialized hardware, this command returns a token which you can redeem when the computation is finished, like this:

{

"token": "Q8jXE"

}

And here’s the redeem call using that token, which you can run 15 minutes later:

21 buy "https://apps.21.co/neuralart/redeem?token=Q8jXE"

Note that this can only be used once. Here’s the result of the redemption:

{

"message": "Thanks!",

"status": "finished",

"url": "http://i.imgur.com/cSiLvsQ.jpg"

}

To see what this looks like visually, here is the input content image:

And here is the style image that we used for the transfer:

Finally, here’s the output image which we got back after running the redeem command:

Pretty cool! And that’s how to buy a bitcoin-payable API that uses deep learning behind the scenes from the 21 Marketplace. The full documentation for the API is available here.

Setting up your own bitcoin-payable API on Heroku or Amazon Web Services

If you want to take the next step and learn how to make APIs like this yourself, please check out these links:

If you have questions or want to join the discussion, join our Slack channel at slack.21.co. We can’t wait to see what you build!