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 (event, context, callback) {
  /* Transform API Gateway message to request message for hapi */
  let request = {
    method: event.method,
    url: event.path,
    headers: event.headers,
    payload: event.body,
    remoteAddress: event.ip

  return server.initialize()
    .then(() => server.inject(request))
    .then((response) => {
      /* Transform hapi response to API Gateway response message */
      callback(null, {
        statusCode: response.statusCode,
        headers: response.headers,
        body: response.payload


Steps To Deploy

  1. Sign into Stackery
  2. Create a new Stackery stack
    1. Name the stack (e.g. hapi-demo)
    2. Create the stack
    3. Update your new stack with the code from hapi-serverless-example.
  3. 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.
  4. 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.
  5. 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.