base on Get a full fake REST API with zero coding in less than 30 seconds (seriously) # json-server [![Node.js CI](https://github.com/typicode/json-server/actions/workflows/node.js.yml/badge.svg)](https://github.com/typicode/json-server/actions/workflows/node.js.yml) > [!IMPORTANT] > Viewing beta v1 documentation – usable but expect breaking changes. For stable version, see [here](https://github.com/typicode/json-server/tree/v0) šŸ‘‹ _Hey! Using React, Vue or Astro? Check my new project [MistCSS](https://github.com/typicode/mistcss) to write 50% less code._ ## Install ```shell npm install json-server ``` ## Usage Create a `db.json` or `db.json5` file ```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" } } ``` <details> <summary>View db.json5 example</summary> ```json5 { 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](https://github.com/json5/json5). </details> Pass it to JSON Server CLI ```shell $ npx json-server db.json ``` Get a REST API ```shell $ curl http://localhost:3000/posts/1 { "id": "1", "title": "a title", "views": 100 } ``` Run `json-server --help` for a list of options ## Sponsors ✨ | Sponsors | | :---: | | <a href="https://mockend.com/" target="_blank"><img src="https://jsonplaceholder.typicode.com/mockend.svg" height="100px"></a> | | <a href="https://zuplo.link/json-server-gh"><img src="https://github.com/user-attachments/assets/adfee31f-a8b6-4684-9a9b-af4f03ac5b75" height="100px"></a> | | Sponsors | | :---: | | <a href="https://konghq.com/products/kong-konnect?utm_medium=referral&utm_source=github&utm_campaign=platform&utm_content=json-server"><img src="https://github.com/typicode/json-server/assets/5502029/e8d8ecb2-3c45-4f60-92d0-a060b820fa7f" height="75px"></a> | | Sponsors | | | :---: | :---: | | <a href="https://www.storyblok.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/c6b10674-4ada-4616-91b8-59d30046b45a" height="35px"></a> | <a href="https://betterstack.com/" target="_blank"><img src="https://github.com/typicode/json-server/assets/5502029/44679f8f-9671-470d-b77e-26d90b90cbdc" height="35px"></a> | | <a href="https://route4me.com"><img src="https://github.com/user-attachments/assets/4eab0bac-119e-4b27-8183-8b136190b776" height="35px" alt="Delivery Routing Software and Route Optimization Software"></a> | <a href="https://www.speechanddebate.org"><img src="https://github.com/user-attachments/assets/cc7980e4-2147-4499-8de4-4d0c265d0c07" height="35px"></a> | [Become a sponsor and have your company logo here](https://github.com/users/typicode/sponsorship) ## Sponsorware > [!NOTE] > This project uses the [Fair Source License](https://fair.io/). Only organizations with 3+ users are kindly asked to contribute a small amount through sponsorship [sponsor](https://github.com/sponsors/typicode) for usage. __This license helps keep the project sustainable and healthy, benefiting everyone.__ > > For more information, FAQs, and the rationale behind this, visit [https://fair.io/](https://fair.io/). ## Routes Based on the example `db.json`, you'll get the following routes: ``` GET /posts GET /posts/:id POST /posts PUT /posts/:id PATCH /posts/:id DELETE /posts/:id # Same for comments ``` ``` GET /profile PUT /profile PATCH /profile ``` ## Params ### Conditions - ` ` → `==` - `lt` → `<` - `lte` → `<=` - `gt` → `>` - `gte` → `>=` - `ne` → `!=` ``` GET /posts?views_gt=9000 ``` ### Range - `start` - `end` - `limit` ``` GET /posts?_start=10&_end=20 GET /posts?_start=10&_limit=10 ``` ### Paginate - `page` - `per_page` (default = 10) ``` GET /posts?_page=1&_per_page=25 ``` ### Sort - `_sort=f1,f2` ``` GET /posts?_sort=id,-views ``` ### Nested and array fields - `x.y.z...` - `x.y.z[i]...` ``` GET /foo?a.b=bar GET /foo?x.y_lt=100 GET /foo?arr[0]=bar ``` ### Embed ``` GET /posts?_embed=comments GET /comments?_embed=post ``` ## Delete ``` DELETE /posts/1 DELETE /posts/1?_dependent=comments ``` ## Serving static files If you create a `./public` directory, JSON Server will serve its content in addition to the REST API. You can also add custom directories using `-s/--static` option. ```sh json-server -s ./static json-server -s ./static -s ./node_modules ``` ## Notable differences with v0.17 - `id` is always a string and will be generated for you if missing - use `_per_page` with `_page` instead of `_limit`for pagination - use Chrome's `Network tab > throtling` to delay requests instead of `--delay` CLI option ", Assign "at most 3 tags" to the expected json: {"id":"1396","tags":[]} "only from the tags list I provide: [{"id":77,"name":"3d"},{"id":89,"name":"agent"},{"id":17,"name":"ai"},{"id":54,"name":"algorithm"},{"id":24,"name":"api"},{"id":44,"name":"authentication"},{"id":3,"name":"aws"},{"id":27,"name":"backend"},{"id":60,"name":"benchmark"},{"id":72,"name":"best-practices"},{"id":39,"name":"bitcoin"},{"id":37,"name":"blockchain"},{"id":1,"name":"blog"},{"id":45,"name":"bundler"},{"id":58,"name":"cache"},{"id":21,"name":"chat"},{"id":49,"name":"cicd"},{"id":4,"name":"cli"},{"id":64,"name":"cloud-native"},{"id":48,"name":"cms"},{"id":61,"name":"compiler"},{"id":68,"name":"containerization"},{"id":92,"name":"crm"},{"id":34,"name":"data"},{"id":47,"name":"database"},{"id":8,"name":"declarative-gui "},{"id":9,"name":"deploy-tool"},{"id":53,"name":"desktop-app"},{"id":6,"name":"dev-exp-lib"},{"id":59,"name":"dev-tool"},{"id":13,"name":"ecommerce"},{"id":26,"name":"editor"},{"id":66,"name":"emulator"},{"id":62,"name":"filesystem"},{"id":80,"name":"finance"},{"id":15,"name":"firmware"},{"id":73,"name":"for-fun"},{"id":2,"name":"framework"},{"id":11,"name":"frontend"},{"id":22,"name":"game"},{"id":81,"name":"game-engine "},{"id":23,"name":"graphql"},{"id":84,"name":"gui"},{"id":91,"name":"http"},{"id":5,"name":"http-client"},{"id":51,"name":"iac"},{"id":30,"name":"ide"},{"id":78,"name":"iot"},{"id":40,"name":"json"},{"id":83,"name":"julian"},{"id":38,"name":"k8s"},{"id":31,"name":"language"},{"id":10,"name":"learning-resource"},{"id":33,"name":"lib"},{"id":41,"name":"linter"},{"id":28,"name":"lms"},{"id":16,"name":"logging"},{"id":76,"name":"low-code"},{"id":90,"name":"message-queue"},{"id":42,"name":"mobile-app"},{"id":18,"name":"monitoring"},{"id":36,"name":"networking"},{"id":7,"name":"node-version"},{"id":55,"name":"nosql"},{"id":57,"name":"observability"},{"id":46,"name":"orm"},{"id":52,"name":"os"},{"id":14,"name":"parser"},{"id":74,"name":"react"},{"id":82,"name":"real-time"},{"id":56,"name":"robot"},{"id":65,"name":"runtime"},{"id":32,"name":"sdk"},{"id":71,"name":"search"},{"id":63,"name":"secrets"},{"id":25,"name":"security"},{"id":85,"name":"server"},{"id":86,"name":"serverless"},{"id":70,"name":"storage"},{"id":75,"name":"system-design"},{"id":79,"name":"terminal"},{"id":29,"name":"testing"},{"id":12,"name":"ui"},{"id":50,"name":"ux"},{"id":88,"name":"video"},{"id":20,"name":"web-app"},{"id":35,"name":"web-server"},{"id":43,"name":"webassembly"},{"id":69,"name":"workflow"},{"id":87,"name":"yaml"}]" returns me the "expected json"