Test Automation Blog

Tools, Tips and Thoughts for Playwright, Selenium UI and API automated testing

Create a mock REST API with zero coding – fast!

Introduction

There are times when it’s super-useful to be able to quickly create your own RESTful API, whether it’s for learning test automation or for front-end developers needing a mock back-end.

You could code your own API but it’s a time-consuming and tricky process. Alternatively public APIs are available but suffer from significant drawbacks: you can rarely use your own data structure, you often can’t even save data, and you might conflict with other people using the same API, with unpredictable results.

You can avoid all this by using a free tool called json-server, which allows you to very quickly create your own local RESTful API for prototyping and mocking in just a few minutes and without any coding, using your own data which you can easily modify.

It’s a JavaScript application which runs under Node.js and can be installed on Windows, Linux and iOS, so let’s start by getting it installed and up and running.

Installing Node.js and json-server

First you’ll need to install Node.js if you haven’t already, so go to https://nodejs.org/en/download/ to download the latest version, run the installer and choose all the default options.

To check it has installed correctly open a command window and enter “node -v”. You should get back the Node version you have just installed.

C:\Users\myPC> node -v
v12.14.1

Once you have confirmed Node is running properly, you can then go ahead and install json-server. You can follow the instructions from the “Getting Started” section of their GitHub repository https://github.com/typicode/json-server, which will ask you to install it using this command: “npm install -g json-server“. You can then check it has been installed correctly using the “json-server -v” command.

C:\Users\myPC> json-server -v
0.16.3

Creating the API ‘database’

Before you actually run the json-server API you will need a ‘database’ so create a local file – let’s call it blog-db.json – and within it put this JSON data:

{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" },
    { "id": 2, "title": "node", "author": "joebloggs" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}

Of course you can change the data to suit your own project, just make sure it’s valid JSON or it won’t work! There’s plenty of online JSON validators so make use of those if you’re uncertain, and Notepad++ has a nice JSON formatting plugin too.

Starting up your RESTful API

Now the data for the API has been defined we can start up the json-server with your mock JSON database:

json-server --watch blog-db.json

… which should show you some useful information about the API, for example the URL which by default is http://localhost:3000

Check for error messages in case it does not start up correctly!

Using your API

At this stage you should now be able to see the API running by navigating to the API URL in your browser ( http://localhost:3000/ ), and specifying the data you want to retrieve, for example:

http://localhost:3000/posts      -  should return all 'posts' data
http://localhost:3000/posts/1    -  should return data for post with id = 1
http://localhost:3000/comments   -  should return all 'comments' data
and so on

Of course in addition to GETs to retrieve data, you can also issue POST, PUT and DELETE requests against this API to create, modify and delete data, as you would against any normal API.

There you have it, you now have a RESTful API which you can use to create automated tests, perhaps using REST Assured and Java, or RestSharp with C#.

Finally, I’d urge you to read all the documentation about json-server at their GitHub repo https://github.com/typicode/json-server in order to fully understand its capabilities, how it works and all the useful options it provides … it will save you time in the long run!

I hope you enjoyed this blog post and found it a useful mechanism for quickly setting up a local API for your test automation or other kind of project.

Create a mock REST API with zero coding – fast!
Scroll to top