Build API mocks with Amokjs

api mocking with nodejs and amokjs


API contract mocking is often used within application development lifecycle where required backend services are not yet available or not yet stable.

This post is about building REST service mocks in order to decouple and speed up application development.

In this tutorial, we will create 2 example API mocks by using the open source API mocking framework Amokjs.

One of the mocks will be lightweight Nodejs application and the second one will be Apigee bootstrap based API mock deployable on Apigee Api gateway platform.

Let’s get started.

Amokjs is a small Nodejs library which helps to serve example API responses from flat files.

It was created for quickly deploying API response mocks to unblock development of client applications and services.

Amokjs Example

Let’s build standalone Nodejs application which serves example API responses from flat files.

First, we create the package.json file to define your Nodejs project. (you need Nodejs and npm installed). It must have the amokjs dependency listed (example of such file is here).

"dependencies": {

Next, create the app.js file with the following content

const amokjs = require('amokjs');

// start mock service

Above lines are importing amokjs package dependency and setting the incoming port on which app will listen to http requests.

Whats left is to create the responses directory and place the flat files with example responses. Amokjs project provides few such example files.

Install npm dependencies and run the application.

npm install
node app.js

Nodejs application should be running on the specified port, and serving example API responses based on the flat file name.

curl -v localhost:3030/json

HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: text/html; charset=utf-8
Content-Length: 180
Connection: keep-alive

  "array": [1,2,3],
  "boolean": true,
  "null": null,
  "number": 123
"string": "Hello World"

Amokjs has few helpers. For example, support for desired http response codes via request header x-mock-response-code and x-mock-filename header support for removing file names from request paths.

You can find more sample responses in the Amokjs example project and start creating your own.

Apigee Edge and Amokjs Example

Apigee Edge is API management product used for building enterprise level API platforms.

This part of tutorial will show you how to host simple API mocks using Apigee Edge and Amokjs.

For quick setup of Apigee Edge based API, we are going to use skeleton Apigee API bootstrap project. It has all the setup we need to get started.

Clone the project.
git clone [email protected]:popularowl/apigeebootstrap.git
Change the project settings and do a deployment to Apigee Edge as described in readme instructions.

You should see the API endpoint deployed and running in Apigee Edge.

Next step is to take the application we have create in the previous step of this tutorial and add it to apiproxy/resources/hosted part of Apigee API bundle.

We need to introduce one change to the app

const amokjs = require('amokjs');
const port = process.env.PORT || 3000;



Capturing process.env.PORT allows us to use the port which Nodejs app container hosted in Apigee Edge will be exposing.

Finally, we have to add app.yaml config file, which configures how Nodejs container will be run in Apigee Edge.

runtime: node
runtimeVersion: 8
   - name: NODE_ENV
     value: production
   - name: LOG_LEVEL
     value: 3

Running Maven deploy command again, should deploy new version of Apigee Edge API with Amokjs mock application container running.

You can find example Apigee Edge bundle with mock endpoint application on Github as open source project.

As always, feel free to post any questions you have in the comments.

Did you like this post?
Subscribe to receive new Popularowl
tutorials and posts

No Responses

Post Your Comment

Your email address will not be published.