How to setup a mock ReSTful API in minutes…

Younes
Marmicode
Published in
2 min readMar 7, 2019

--

… using JSON Generator, JSON Server & Zeit’s Now.

During some of the trainings we deliver, we need some operational mock ReSTful API with CRUD operations support and filtering features.

Generate JSON data

JSON Generator is an original online tool that allows us to generate a bunch of resources using a specific but yet intuitive syntax.

JSON Generator

You can now download or copy the generate JSON array.

Setup the server

First, let’s create a NodeJS module!

mkdir api
cd api
yarn init # or npm init

JSON Server

JSON Server is an easy to setup ReSTful API mock server that uses a JSON file as a database for storing resources.

  1. Let’s install it.
yarn add json-server # or npm install --save json-server

2. Now, we can add the JSON array generated before to a db.json file with the following structure:

{
"users": [
{
"id": ...
},
...
]
}

3. Finally, we just have to add the start script in the package.json.

{
...
"scripts": {
"start": "json-server db.json"
}
...
}

The API should be running now and you can start querying it locally but you might want to deploy it somewhere in the cloud so you can share it with your friends or use it from Stackblitz or any other online code editor.

Deploy using Zeit’s Now

The good news is that thanks to Zeit’s Now, deploying the mock API will just require us to type one command: now.

Well, first you will have to sign up on Zeit (https://zeit.co/signup) and install now CLI using yarn global add now (or npm install -g now).

Now, we can go back to our API’s NodeJS module we created before and run the now command.

Zeit’s Now

--

--

Younes
Marmicode

Google Developer Expert for Angular & Web Technologies | eXtreme Programming Coach | Tech Advisor