Hapi Serverless API Example


This guide shows how an existing hapi API can be used with Stackery to run on a serverless infrastructure.


Using serverless technologies for APIs provides multiple benefits:

  • Requests are handled in parallel with unbounded horizontal scalability.
  • Increased infrastructure efficiency because serverless functions run only when API requests are made. Infrequently accessed APIs no longer require base-line server instance costs.
  • Ops overhead is reduced as much of the engineering challenges around scalability are handled automatically by the infrastructure provider.

However, many people build serverless-based APIs by decomposing functionality into individual functions servicing individual endpoints. This can be challenging to maintain due to the complexities of managing helper functionality used across multiple endpoints.

Instead, we can build a single function servicing all requests using a mature, popular API framework like hapi. This way developers can use tools and techniques they are already familiar with to power their API services.


This repo contains a Stackery stack that can be forked and imported into your own Stackery account. The stack contains two nodes: a Rest Api node and a Function node.

The Function node is a small wrapper around hapi-example. Only one modification to hapi apps is needed to enable them to work in serverless use cases: preventing the server from listening for HTTP connections when used as a dependency of a serverless function. You can see the modification here. With this change the server can still be run locally by running node server.js. This is great for local testing purposes!

The only other piece of functionality needed is a hookup between an incoming Stackery HTTP request message and the hapi server, and then a hookup between the hapi response and the Stackery HTTP response. This is performed by the api function handler:

const server = require('ProductsAPI')

module.exports = function handler(message, output) {
  /* If you want to output messages to further nodes, you can put the output
   * function on the server object: */
  server.app.output = output

  /* Transform Stackery message to request message for hapi */
  let request = {
        method: message.method,
        url: message.pathname,
        headers: message.headers,
        payload: message.body,
        remoteAddress: message.ip

  return server.initialize()
    .then(() => server.inject(request))
    .then((response) => {
      /* Transform hapi response to Stackery Rest Api response message */
      return {
        statusCode: response.statusCode,
        headers: response.headers,
        body: response.rawPayload


Steps To Deploy

  1. Fork the hapi-serverless-example repo to your own GitHub account
  2. Sign into Stackery
  3. Create a new Stackery stack
    1. Navigate to https://app.stackery.io/new/github/link to create a new stack from an existing GitHub repo
    2. Name the stack (e.g. hapi-demo)
    3. Select your fork of this repo from the list of repos
    4. Create the stack!
  4. Deploy your stack
    1. Click the Prepare Deployment button in the left sidebar of the Stackery dashboard.
    2. Wait for the deployment to be prepared. This takes about a minute.
    3. Click the Deploy button. This will open the AWS CloudFormation Console. Click the “Execute” button in the top right to deploy the new stack.
    4. Back in the Stackery dashboard, wait for the deployment to be deployed. The deployment in the left sidebar will move from the Prepared Deployment section to the Current Deployment section once it has been fully deployed. Deploying takes about 10-15 minutes the first time. Subsequent deployments take about a minute each time.
  5. Find the stackery-stacks.io domain for your API
    1. In the Stackery dashboard, select the current deployment if it is not already selected. In the canvas area on the right, select the Rest Api node. In the properties panel on the right find the Domain Name.
  6. Navigate to your domain and test it out! (e.g. https://api.0123456789.stackery-stacks.io/products)

Try Stackery For Free

Gain control and visibility of your serverless operations from architecture design to application deployment and infrastructure monitoring.