Creating a serverless blockchain app with Symbol and Netlify Functions

serverless blockchain app

Tutorial about “Creating a serverless blockchain app” First appeared on David Garcia’s Blog

In this tutorial we will see how to create a serverless blockchain application using Symbol and Netlify Functions.

In particular, we are going to create a blockchain faucet. But first, let’s review what all these concepts mean and how well they fit together.

What is Symbol blockchain?

Symbol is a blockchain platform allowing developers to access advanced blockchain built-in features using a REST API. You can use Symbol to integrate blockchain capabilities in new or existing applications with lightweight code. Read more about Symbol here.

… and serverless?

An application needs to live somewhere to be accessed by others.

Initially, applications were located in servers within the companies facilities. Now, it’s common to hire virtual machines from providers such as Amazon or Google, instead of owning the iron at home. Still, developers have to shift the focus from development to configure the servers, virtual machines, or containers to be able to run their applications.

The serverless architecture provides a higher abstraction for devs, enabling them to execute code in an environment entirely managed by others. In short, there is no time spent setting up the infrastructure, including configuration, availability, load-balancing …

One of the platforms offering Function-as-a-Service(FaaS) is Netlify, which adds a simplistic configuration layer on top of Amazon Lambda.

How everything fits together?

The first clear benefit of running a serverless blockchain app is that there is less infrastructure to maintain. Executing serverless functions is usually cheaper than running a highly available and distributed server 24 hours a day, considering that functions are charged per-use.

Secondly, reading from Symbol blockchain is as straightforward as consuming an API call. However, things start getting trickier when we have to change the state of the blockchain since nodes only accept serialized data. To serialize transactions, we typically use a Software Development Kit (SDK), but up to the moment, Symbol does not offer an SDK for every language. We can fill this gap by creating custom serverless functions in plain Javascript, that later can be consumed by apps written in any other language.

Imagine a company willing to integrate Symbol blockchain into their existent app written in Ruby. Unfortunately, there is no Symbol SDK for Ruby. Still, to overcome the problem, they could use a lambda function to record transactions on the blockchain.

Overview of the application

To show how can we combine serverless and blockchain apps, we are going to build a serverless blockchain app faucet for Symbol. Faucets are simple contracts that allow accounts to requests blockchain tokens.

serverless blockchain app

As Symbol applications can be developed in plain JavaScript, we might fail into the error to put all the logic inside the client application. However, to announce transactions, we need first to sign them with the faucet’s private key, and as the name suggests, this key should remain private at all costs. For that reason, we need to add an extra layer and move part of the application behind a server, where users cannot access to this private key.

The faucet presented is hosted on GitHub, made available to others thanks to Netlify’s CDN and their service called Functions.


To run this example, make sure you have installed:

  • Git
  • NodeJS 12 LTS
  • yarn

If you have not developed Symbol apps before, I recommend you to complete Symbol’s Getting Started section to create an account and send your first transaction to the network.

Download the boilerplate project

The project symbol-serverless-faucet has all the necessary components to deploy a server-less app.

Deploy to Netlify

Note: We are going to test the app first locally, and then deploy it to Netlify. If you want to deploy a custom faucet, jump to “Deploying the app” section.

1. Fork the repository dgarcia360/symbol-serverless-faucet.

2. Clone the forked repository in your local system.

git clone

3. Enter the project and install yarn dependencies.

cd symbol-serverless-faucet
yarn install

Developing a lambda function

First, we need to define the serverless function. The file src/lamba/faucet.js contains the function the faucet is using.

import querystring from “querystring”;
import {
} from “nem2-sdk”;
exports.handler = async (event, context) => {
// Only allow POST
if (event.httpMethod !== “POST”) {
return {statusCode: 405, body: “Method Not Allowed”};
const params = querystring.parse(event.body);
const recipientAddress = Address.createFromRawAddress(params.address);
const amount = params.amount < 1000 ?
UInt64.fromUint(params.amount * Math.pow(10, parseInt(MOSAIC_DIVISIBILITY))) :
UInt64.fromUint(1000 * Math.pow(10, parseInt(MOSAIC_DIVISIBILITY)));
const transferTransaction = TransferTransaction.create(
[new Mosaic(new MosaicId(MOSAIC_ID), amount)],
const account = Account.createFromPrivateKey(FAUCET_PRIVATE_KEY, parseInt(NETWORK));
const signedTransaction = account.sign(transferTransaction, GENERATION_HASH);
const transactionHttp = new TransactionHttp(NODE_URL, NETWORK);
.subscribe(x => console.log(x),err => console.log(err));
return {
statusCode: 200,
body: `Transaction announced: ${signedTransaction.hash}`

As you can see, a lambda function is developed similarly to any other API. In this case, the function takes as the input amount and address. Then, we define a transaction, sign it using the faucet’s account, and send it to the network. Finally, the function returns an OK response if the transaction was successfully announced.

All the environment variables are defined in the file .env, except for one: the private key.


Change the environment variables as desired. Then, open a new terminal and set up the faucet’s private key.

export FAUCET_PRIVATE_KEY=“my value”

Calling the function

Now that we have defined the function, we can call it. The file src/index.html defines the form used to make a POST request to the serverless function.

<form action=/.netlify/functions/faucet method=POST>
<label>What’s your address?<br/><input name=address type=text></label>
<label>How many units (max 1000)?<br/><input name=amount type=number></label>
<button type=submit>Receive!</button>

Test the app locally by running npm run start. Then, open localhost:8080 in a new browser tab.

Deploying the app

Now that we have created and tested the faucet, we can use Netlify to host the app. If you don’t have a Netlify account, sign up here first.

1. Create a new site from Git using this link.

2. Choose to deploy the site using your GitHub account.

3. Authorize Netlify.

4. Select the repo symbol-serverless-faucet.

5. Open the tab Advanced build settings and define the following environment variables:

serverless blockchain app

6. Click on the “Deploy Site” button— and we are done!

If everything goes well, Netlify will show you the URL of the site after the app is deployed.

Putting all together

In this tutorial, we only have started to see what we can achieve by combining serverless functions and Symbol blockchain. Nevertheless, we can envision a wide range of promising possibilities. If you create a new serverless blockchain app using the boilerplate code, do not hesitate to share the link in the comments!

Previous articles from & with David Garcia

Guidelines for nem2-sdk development collaboration

Foundation Technology Department Update – July 2019

Getting started with catapult

You may also like...