Creating Mock APIs with json-server: A Beginner's Guide to Faking It Til You Make It
Tags: node, mocking, tools
Published: 2023-01-14 14:27:35

---
title: Creating Mock APIs with json-server: A Beginner's Guide to Faking It Til You Make It
published: true
date: 2023-01-13 19:37:35 UTC
tags: Nodejs,Mocking,Tools
canonical_url: https://techdevblog.io/creating-mock-apis-with-json-server-a-beginners-guide-to-faking-it-til-you-make-it/
---

 ![Creating Mock APIs with json-server: A Beginner's Guide to Faking It Til You Make It](https://techdevblog.io/content/images/2023/01/image_0-14.jpg)

Are you looking to build a simple, yet powerful to-do list API? Look no further! In this guide, we'll walk you through the process of creating a JSON REST API using json-server, a popular and easy-to-use tool for creating mock REST APIs.

## Prerequisites

Before we begin, make sure you have the following installed on your computer:

- [Node.js](https://nodejs.org)
- npm (comes with [Node.js](https://nodejs.org))
- [json-server](https://github.com/typicode/json-server) (you can install it via npm by running npm install -g json-server)

## Step 1: Create a JSON file for your to-do list

The first step is to create a JSON file that will serve as the foundation for our API. This file will contain all the to-do items and their respective details.

Create a new file called db.json in a directory of your choice and add the following data to it:

```
{
    "todos": [
        {
            "id": 1,
            "title": "Buy milk",
            "completed": false
        },
        {
            "id": 2,
            "title": "Finish report",
            "completed": false
        },
        {
            "id": 3,
            "title": "Attend meeting",
            "completed": true
        }
    ]
}

```

This file represents a simple to-do list containing three items. Each item has an id, a title, and a completed field.

## Step 2: Start the json-server

With the JSON file in place, we can now start the json-server. Open a terminal and navigate to the directory where you saved the db.json file. Then, run the following command:

```
json-server --watch db.json

```

This command starts the json-server and tells it to use the db.json file as its data source. The --watch flag tells the server to watch for changes in the db.json file and automatically reload the data when changes are detected.

Once the server is running, you should see a message similar to the following:

```
\{^_^}/ hi!

  Loading db.json
  Done

  Resources
  http://localhost:3000/todos

  Home
  http://localhost:3000

```

The server is now running and accessible at `http://localhost:3000`. You can test it by opening a web browser and navigating to `http://localhost:3000/todos`. You should see the list of to-dos that we added to the `db.json` file.

## Step 3: Add routes to the API

By default, [json-server](https://github.com/typicode/json-server) exposes the entire contents of the `db.json` file as a single resource at the root of the API. However, we can also add custom routes to expose specific parts of the data or perform specific actions.

To add a custom route, create a new file called routes.json in the same directory as the db.json file and add the following data to it:

```
{
    "/api/todos/:id": {
        "GET": {
            "action": "db.todos.find(todo => todo.id == :id)"
		},
		"PUT": {
			"action": "db.todos.find(todo => todo.id == :id).completed = true"
		},
		"DELETE": {
			"action": "db.todos = db.todos.filter(todo => todo.id != :id)"
		}
	}
}

```

This file defines three custom routes for our API, each corresponding to a specific HTTP method (GET, PUT, DELETE). The `:id` parameter in each route is a placeholder that will be replaced by the actual id of the to-do item being requested or modified.

To use the `routes.json` file, we need to tell the json-server to load it when starting. To do this, we need to add the `-r` flag to the command we use to start the server. The final command should look like this:

```
json-server --watch db.json -r routes.json

```

With the custom routes in place, we can now perform specific actions on individual to-do items using the API. For example, we can retrieve a specific to-do item by sending a GET request to `http://localhost:3000/api/todos/1`, mark a to-do item as completed by sending a PUT request to `http://localhost:3000/api/todos/1`, and delete a to-do item by sending a DELETE request to `http://localhost:3000/api/todos/1`.

## Conclusion

That's it! With just a few simple steps, we've created a fully functional JSON REST API for a to-do list using json-server. The [json-server](https://github.com/typicode/json-server) is a great tool for creating mock APIs, but you can also use it as a simple and easy-to-use backend for small projects.

Don't forget to stop the [json-server](https://github.com/typicode/json-server) when you're done working on your project, to avoid unnecessary usage of resources. Remember to use the `Ctrl+c` command.

Thanks for following along and happy coding!

<!--kg-card-end: markdown-->