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.
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.
# Global
npm install -g json-server
# Verify install
npx json-server --versionCreate a Database File
JSON Server expects a JSON object at the root level. Each key becomes a collection that's automatically available at /key.
{
"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.
npx json-server --watch db.json --port 3001 --delay 250REST API Endpoints
All verbs are auto-generated; GET, POST, PUT, PATCH, DELETE write directly to the file.
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 → deleteQuery Features
Rich query parameters cover filtering, search, sort and pagination so you get an experience close to a real API.
# 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=10Relationships
Foreign-key fields support embed and expand. Nested routes like /posts/1/comments work out of the box.
# 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=postKey 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.
Related Posts
Other infographics on connected topics.
- ToolsFrontend
What is Vite? Next-Generation Frontend Tooling
Vite ships an ESM-native dev server that starts instantly and lightning-fast HMR. Walk through setup, the plugin system, production builds and a Webpack comparison.
- FrontendTools
What is Zod? Type-Safe Validation for Modern Apps
Generate runtime validation from TypeScript types with Zod. Schema definitions, parse, safeParse, type inference and React Hook Form integration.
- FrontendTools
What is Axios? A Practical HTTP Client for Modern Apps
Make HTTP requests with Axios — a Promise-based client more powerful than fetch. Walkthrough covers instances, interceptors, error handling and query params.
Discover more developer infographics
Visit the homepage so you don't miss new content.
See all infographics