How to Generate Serverless HTTP API Mocking Using Prism

Nauman Ali
by Nauman Ali on May 11, 2021 4 min read

Get started with a serverless mock server.

{{cta(‘3f490398-a225-456d-b3aa-317c4500f5a8′,’justifycenter’)}}

Typically a mock server is hosted locally or built up semi-permanently on a cloud server. In this post, however, we’ll generate a serverless mock server based on an OpenAPI document.

There are many benefits of a mock server. In your project, mocking could increase the speed of development. This allows your front-end team to work with dummy data or helps you test use cases for your team or to meet client specifications quickly.

Prism Saves Your Team Time

Prism allows you to create mock servers anywhere you want. You can run them locally, on your own infrastructure, or serverlessly, as we’ve shown here. Mock servers help your team get on the same page about what’s possible with your APIs, even before they’re fully-built. Generated mock servers let you easily build testing and feedback into your API design process.

With Prism, your mock server can now validate request and response data, as well as allow use of webhooks and proxy traffic to your API.

This article will provide you with a basic example of a serverless Prism mock server hosted as-needed with Netlify. Follow along so you can experiment with the advantages for you and your team.

Deploy to Netlify Functions

Now that you have the lay of the land, you’ll need a free Netlify account. Click the “Get Started for Free” button to enter into their web application. Next, click GitHub to begin the authentication process between the two services.

Authentication Through Your Git Provider

The authentication process is straightforward and automatic apart from a few button clicks. Netlify uses continuous deployment to update any changes you make in the repo, so click GitHub once again once you see the screen below:

Choose The Git Proovider Where Source Code Is Hosted

Netlify’s web application should open and you’ll be able to select from any of the repos you gave Netlify access to.

Note: You can restrict Netlify’s access to your account to just this forked repository. You can also uninstall Netlify in GitHub as soon as you are done with your demo.

In the app, select your forked repo of the demo by searching for “vncz.” You’ll see a screen with initial deployment settings and a button to deploy the demo. For now, leave these basic build settings sections filled t in with the default settings you get from Netlify and hit “Deploy site”. Your settings should match the ones you see below:

Netlify settings, your settings should match

You should see something like this—your project will get its own random name assigned by Netlify:

Example of random name assigned by Netlify

Once the text shown above changes from yellow to green, your last step is to hit the “Site settings” button. A new screen will allow you to click on Functions in the right nav bar, then Settings:

Where to find Functions in the right nav bar, then Settings

Click the “Edit settings” button when it appears and add the text .functions into the Functions Directory so Netlify knows where to look:

Adding the text .functions into the Functions Directory

The site should deploy with each change to the GitHub repo. You can also trigger a deploy manually.

Netlify has a Functions Overview in their documentation if you’d like to know more.

Call Your Serverless Mock API

Check that your Netlify site has successfully deployed by clicking into the Deploys tab.

You can then check the functions tab where you should see “1 Lambda function actively running in production.”:

checking the functions tab

Click on prism.js to see the function log, which will also show the endpoint: https://{your_unique_subdomain}.netlify.com/.netlify/functions/prism

And FINALLY, to call your serverless mock API, make a call in your browser or using cURL to: https://{your_unique_subdomain}.netlify.com/.netlify/functions/prism/pets

You’ll get a result similar to the following example from my build:

[
  {
    "name":"dolo",
    "id":7285662193856811000,
    "tag":"irure"
  },
  {
    "name":"in",
    "id":923325856014057500,
    "tag":"cupidatat in ut"
  }
]

You can see Prism’s Mock Server is feeding me an array of two objects with three key-value pairs for each pet generated—as described in the resources.json and served up as-needed by Netlify.

Get Started with a Serverless Mock Server

Learn more about Prism and how to use open source mocking in your projects.

get-started-with-prism

 

Share this post

Stoplight to Join SmartBear!

As a part of SmartBear, we are excited to offer a world-class API solution for all developers' needs.

Learn More
The blog CTA goes here! If you don't need a CTA, make sure you turn the "Show CTA Module" option off.

Take a listen to The API Intersection.

Hear from industry experts about how to use APIs and save time, save money, and grow your business.

Listen Now