Skip to content
devcards.space
ToolsBackend

How to Use JSON Server: Build a Fake REST API in Seconds

Spin up a fully featured REST API from a single db.json file with JSON Server. Hands-on guide to filtering, relationships, pagination and full CRUD endpoints.

7 minute read
JSON Server infographic: db.json automatically generates GET/POST/PUT/DELETE endpoints, plus filtering and relationships across 12 sections

JSON Server is a small but powerful tool that turns a single JSON file into a fully featured REST API in seconds — designed for frontend prototypes, demos and learning environments. It's the fastest way to start working before the backend exists, mock services or build training material. This guide expands every section of the infographic, taking you from setup to advanced patterns.

What is JSON Server?

A Node.js package that creates a full REST API from a simple JSON file. It works with a single-file database (db.json) and auto-generates every CRUD endpoint your frontend needs — no backend code to write. It's ideal for prototyping, demos and teaching.

Why Use JSON Server?

When a project starts and the backend isn't ready, the frontend team is blocked. JSON Server removes that delay and lets you build the UI as if there were a real API.

  • Instant REST API setup — seconds
  • No database needed — just a JSON file
  • Speeds up frontend development
  • Great for demos and training environments
  • Works without any backend dependencies

Installation

Install globally or per-project. The most common modern approach is via an npm script.

bash
# Global
npm install -g json-server

# Verify install
npx json-server --version

Create a Database File

JSON Server expects a JSON object at the root level. Each key becomes a collection that's automatically available at /key.

db.json
json
{
  "posts": [
    { "id": 1, "title": "Hello", "views": 120 },
    { "id": 2, "title": "Mock API", "views": 87 }
  ],
  "comments": [
    { "id": 1, "postId": 1, "body": "Awesome!" }
  ],
  "profile": { "name": "DevCards" }
}

Start the Server

The command below watches db.json and starts an API on port 3001. Use --port to choose another port and --delay to simulate latency.

bash
npx json-server --watch db.json --port 3001 --delay 250

REST API Endpoints

All verbs are auto-generated; GET, POST, PUT, PATCH, DELETE write directly to the file.

bash
GET    /posts all posts
GET    /posts/1 one post
POST   /posts create (id assigned)
PUT    /posts/1 replace whole record
PATCH  /posts/1 partial update
DELETE /posts/1 delete

Query Features

Rich query parameters cover filtering, search, sort and pagination so you get an experience close to a real API.

bash
# Filtering
GET /posts?views_gte=100

# Full-text search
GET /posts?q=hello

# Sort
GET /posts?_sort=views&_order=desc

# Paginate
GET /posts?_page=1&_limit=10

Relationships

Foreign-key fields support embed and expand. Nested routes like /posts/1/comments work out of the box.

bash
# Comments of a post
GET /posts/1/comments

# Embed: include comments inside the post
GET /posts?_embed=comments

# Expand: include the parent post
GET /comments?_expand=post

Key Features

JSON Server alone gives you a remarkably capable mock backend:

  • Full CRUD support
  • Auto-generated routes
  • Custom routes via middleware
  • Watch mode auto-reloads on file change
  • Realistic latency simulation with --delay
  • CORS enabled

Use Cases

Where JSON Server shines:

  • Starting the frontend before the backend
  • Prototyping APIs quickly
  • Building demos with middleware and seed data
  • Teaching and training
  • Pairing with libraries like React Query

Summary

JSON Server turns a db.json file into a working REST API instantly. It shortens development time, is great for prototypes and works seamlessly with libraries like React Query. It's not built for production — use it for demos, prototypes and development.

Frequently Asked Questions

Common questions about this topic.

Can I use JSON Server in production?

No. It has no concurrency-safe writes, authentication, indexing or scalability. For production, prefer Express, Fastify, NestJS or BaaS services like Supabase or Firebase.

Can I add authentication?

Yes — middleware like json-server-auth lets you add JWT-based auth, or you can write your own Express middleware. routes.json offers further customization.

Where is data stored?

Every POST/PUT/PATCH/DELETE writes directly to db.json. Keeping the original seed data in a separate file (db.seed.json) is a good practice.

What changes when I switch to a real backend?

If your fetch calls go through an API client (axios, ky, ofetch) with a baseURL, switching is just an environment variable change. Defining the endpoint contract up front makes the migration clean.

How does it compare with MSW?

MSW intercepts network calls in the browser/tests; JSON Server is a real HTTP server. If mobile or QA clients also need to hit the mock, JSON Server fits — for tests alone, MSW is ideal.

Other infographics on connected topics.

Discover more developer infographics

Visit the homepage so you don't miss new content.

See all infographics