[!IMPORTANT] Viewing beta v1 documentation – usable but expect breaking changes. For stable version, see here
[!NOTE] Using React ⚛️ and tired of CSS-in-JS? See MistCSS 👀
npm install json-server
Create a db.json or db.json5 file
{
"$schema": "./node_modules/json-server/schema.json",
"posts": [
{ "id": "1", "title": "a title", "views": 100 },
{ "id": "2", "title": "another title", "views": 200 }
],
"comments": [
{ "id": "1", "text": "a comment about post 1", "postId": "1" },
{ "id": "2", "text": "another comment about post 1", "postId": "1" }
],
"profile": {
"name": "typicode"
}
}
{
posts: [
{ id: "1", title: "a title", views: 100 },
{ id: "2", title: "another title", views: 200 },
],
comments: [
{ id: "1", text: "a comment about post 1", postId: "1" },
{ id: "2", text: "another comment about post 1", postId: "1" },
],
profile: {
name: "typicode",
},
}
You can read more about JSON5 format here.
Start JSON Server
npx json-server db.json
This starts the server at http://localhost:3000. You should see:
JSON Server started on PORT :3000
http://localhost:3000
Access your REST API:
curl http://localhost:3000/posts/1
Response:
{
"id": "1",
"title": "a title",
"views": 100
}
Run json-server --help for a list of options
Become a sponsor and have your company logo here
JSON Server supports advanced querying out of the box:
GET /posts?views:gt=100 # Filter by condition
GET /posts?_sort=-views # Sort by field (descending)
GET /posts?_page=1&_per_page=10 # Pagination
GET /posts?_embed=comments # Include relations
GET /posts?_where={"or":[...]} # Complex queries
See detailed documentation below for each feature.
For array resources like posts and comments:
GET /posts
GET /posts/:id
POST /posts
PUT /posts/:id
PATCH /posts/:id
DELETE /posts/:id
For singular object resources like profile:
GET /profile
PUT /profile
PATCH /profile
Use field:operator=value.
Operators:
eq (equal)lt less than, lte less than or equalgt greater than, gte greater than or equaleq equal, ne not equalin included in comma-separated listcontains string contains (case-insensitive)startsWith string starts with (case-insensitive)endsWith string ends with (case-insensitive)Examples:
GET /posts?views:gt=100
GET /posts?title:eq=Hello
GET /posts?id:in=1,2,3
GET /posts?author.name:eq=typicode
GET /posts?title:contains=hello
GET /posts?title:startsWith=Hello
GET /posts?title:endsWith=world
GET /posts?_sort=title
GET /posts?_sort=-views
GET /posts?_sort=author.name,-views
GET /posts?_page=1&_per_page=25
Response:
{
"first": 1,
"prev": null,
"next": 2,
"last": 4,
"pages": 4,
"items": 100,
"data": [
{ "id": "1", "title": "...", "views": 100 },
{ "id": "2", "title": "...", "views": 200 }
]
}
Notes:
_per_page defaults to 10 if not specified_page or _per_page values are automatically normalized to valid rangesGET /posts?_embed=comments
GET /comments?_embed=post
_where_where accepts a JSON object and overrides normal query params when valid.
GET /posts?_where={"or":[{"views":{"gt":100}},{"author":{"name":{"lt":"m"}}}]}
DELETE /posts/1?_dependent=comments
JSON Server automatically serves files from the ./public directory.
To serve additional static directories:
json-server db.json -s ./static
json-server db.json -s ./static -s ./node_modules
Static files are served with standard MIME types and can include HTML, CSS, JavaScript, images, and other assets.
If you are upgrading from json-server v0.x, note these behavioral changes:
id is always a string and will be auto-generated if not provided_per_page with _page instead of the deprecated _limit parameter_embed instead of _expand for including related resources--delay CLI optionNew to json-server? These notes are for users migrating from v0. If this is your first time using json-server, you can ignore this section.