How to setup a mock ReSTful API in minutes…

Younes
Younes
Mar 7, 2019 · 2 min read

… 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 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

Marmicode

At Marmicode, we use our passion and experience in Web…

Marmicode

At Marmicode, we use our passion and experience in Web Development & eXtreme Programming to help you cook better apps, ship them fast and make you proud of your work. #javascript #python #angular #continuousdeployment

Younes

Written by

Younes

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

Marmicode

At Marmicode, we use our passion and experience in Web Development & eXtreme Programming to help you cook better apps, ship them fast and make you proud of your work. #javascript #python #angular #continuousdeployment

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store