This article originally appeared on The Lead via Graphite Publishing.

It’s become increasingly easy to deploy servers over the years, and for the most part, it’s also become increasingly cost efficient. However, when you start needing servers for many single-task (or few-task) functions, it doesn’t make a ton of sense to configure, deploy, maintain, and pay for a server. When that happens, there are a few options available, but one that I’ve found to be especially useful. That is Webtask.

Webtask is a platform as a service application built by Auth0. It allows you to drop server-side code into a hosted file on Webtask then make http request to the unique URL Webtask provides to run said code. This is great for apps like Graphite and Graphite Publishing that are entirely decoupled from the storage layer. Apps like these are really just front-end containers making nothing but API calls.

In this tutorial, we’re going to run through setting up and using Blockstack’s Radiks framework via Webtask. It’s a perfect example of a single-function server that probably doesn’t make a ton of sense for you to deploy and maintain. But what is Radiks, really?

Radiks is a framework that gives decentralized apps the ability to perform actions that are generally limited to centralized apps. Things like collaboration, access controls, indexing, and more, are either difficult to do with any real speed or impossible to pull off with decentralized apps. Radiks changes this. It adds an encrypted database layer to Blockstack’s user-selected storage, so users still have full control over their data while apps have a queryable model to do the things traditional apps have always been able to do with a database.

As you can see in the Radiks documentation, there is a client-side portion and a server-side portion. When building your Blockstack app, the client-side documentation should be pretty straight-forward. And to be honest, the server-side documentation is pretty clear as well. But what if you got into this so that you didn’t have to run a server? What if you wanted to build a strictly client-side app and not maintain a server?

That’s where Webtask comes in.

Getting Started With Webtask

It’s pretty painless to get started. Simply go to webtask.io and sign in. Because this is an Auth0 product, authentication is a breeze. Once signed in, you now have access to your free account. (There are limits to the free account, so you should read up on those)

The documentation you’ll see largely focuses on command-line creation of webtask functions, but we’re going to use the good ‘ol UI. Go to https://webtask.io/make and you’ll be able to create a new task by clicking the plus button on the left side of the screen. This will pop up a window asking you if you’d like to create an empty function or use a template.

We’re going to choose to use a template. But let’s take a moment and think about how flexible Webtask is. If you don’t find a template for exactly what you’d like to do, you can start from scratch and simply follow along with API documentation for the service you’re trying to integrate with. It’s pretty incredible.

When you choose Select a template, you’ll see a screen like this:

Here we are going to choose “Express.” You don’t have to use Express to run a Radiks server, but if you ever want to extend this serverless webtask function further than just Radiks, it’s a lot easier to do if you are using Express.

Once we have done this, we’re ready to drop in our Radiks code. From the Radiks-Server documentation, you should be able to drop in the following:

const express = require('express');

const { setup } = require('radiks-server');

const app = express();

app.get('/', (req, res) => {

  setup({

    mongoDBUrl: req.webtaskContext.secrets.MONGO_URI

  }).then((RadiksController) => {

    app.use('/radiks', RadiksController);

  });

});

We’re almost done, but not quite there. Just like in a local environment, you can’t just require in dependencies without installing them. So, let’s do that here in Webtask.

At the top of your screen, you should see a wrench icon. Click that then click NPM Modules. This will allow you to search for packages. Type in “radiks-server” and you should get back the appropriate result. Click on the result and then save your project file.

Finally, we need to tell Webtask what our MongoDB URI is. You can see in the code above, it is referenced as `req.webtaskContext.secrets.MONGO_URI`, but we need to actually provide that URI. I’m not going to go into detail on how to setup MongoDB and get the URI as that’s outside the scope of this tutorial. But you will need that URI. To add it to Webtask, click that wrench icon again and click on “Secrets” and you’ll be able to add the secret reference value (MONGO_URI) and the secret value (your actual MongoDB URI). Save that then save your file and that’s all the code you need for Webtask.

This is all well and good, but how exactly do you use this file? Normally, you would have had to write this code locally, built it, deployed it, then you would be given a URL for your new server. In this case, you’ve written the code and you have a URL. It’s right there at the bottom of your screen. Copy that because you’ll need it for the Radiks client-side code.

The Radiks client-side docs show you how to configure Radiks in your project. This is where we’re going to use that Webtask URL. It should look like this:

import { UserSession, AppConfig } from 'blockstack';
import { configure } from 'radiks';

const userSession = new UserSession({
  appConfig: new AppConfig(['store_write', 'publish_data'])
})

configure({
  apiServer: 'http://your-webtask-url.com',
  userSession
});

Once you’ve added your URL, guess what? You’re done. No server deployment. No maintenance. No Heroku “App Crashed” debugging. Just a quick, clean solution.

Graphite makes use of Webtask anytime there’s server-side code that needs to be run since Graphite is a serverless app in the true sense of the word. No server (besides a websockets discovery server for real-time collaboration) and no database.

Hopefully this tutorial helps you get started with Radiks quicker or any server-side code project.