Skip to main content
JSON-Server Installation: Setting Up Mock APIs - Virtarix Blog

JSON-Server Installation: Setting Up Mock APIs

January 16, 2026 · Blog / Technical Guides

JSON Server turns a JSON file into a disposable REST API for frontend development, tests, workshops, and demonstrations. It is useful when the real backend is unavailable, but it is not a production database or an authenticated public API.

Last checked: 11 August 2026.

The current upstream README describes v1 as beta software that is usable but may introduce breaking changes. npm's latest tag and the newest GitHub release both point to 1.0.0-beta.15; the separate legacy line remains 0.17.4. Pin the line your project expects instead of relying on an unqualified install.

Choose the release line before installing

Requirement Use v1.0.0-beta.15 Keep v0.17.4
New short-lived mock API Yes, after testing the exact query behavior you need Only when a legacy example or dependency requires it
Node.js runtime Node.js 22.12.0 or newer, as declared by the package Node.js 12 or newer, as declared by the package
IDs Strings Numbers or strings
Pagination _page with _per_page; response is a pagination object _page with optional _limit; response remains an array
Conditions field:operator=value, such as views:gte=100 Legacy suffix syntax, such as views_gte=100
Relationships _embed; migration notes replace _expand with _embed _embed for children and _expand for a parent
Custom route file or CLI middleware Not present in the current v1 CLI help --routes and --middlewares are documented
Swipe to view the full table

Sources: current upstream README, v0.17.4 README, and npm package metadata, checked 11 August 2026.

Issue #1638, previously cited as unresolved, closed as completed on 29 January 2026. Current v1 documentation uses _embed for both relationship directions and lists _expand as a v0-to-v1 migration change. The current guide therefore uses _embed and does not retain the obsolete unresolved-issue workaround.

Prerequisites and a project-local install

Check Node.js and npm first:

Check Node.js and npm
node --version
npm --version

For a new v1 prototype, create a dedicated project and pin the checked release:

Create a pinned JSON Server v1 project
mkdir json-server-demo
cd json-server-demo
npm init -y
npm install --save-dev json-server@1.0.0-beta.15
npx json-server --version

The v1 package requires Node.js 22.12.0 or newer. If the installed runtime is older, update Node.js through a supported operating-system or Node.js distribution method before installing the package.

For an existing legacy fixture that depends on v0 behavior, pin it explicitly in that project:

Install the pinned legacy release
npm install --save-dev json-server@0.17.4
npx json-server --version

Do not install both release lines into one project and assume commands are interchangeable. Keep separate fixtures or branches and test the migration.

Create a v1 mock database

Add db.json with string IDs and a relationship field:

Define posts and comments for JSON Server v1
{
  "posts": [
    { "id": "1", "title": "First post", "views": 150 },
    { "id": "2", "title": "Second post", "views": 50 }
  ],
  "comments": [
    { "id": "1", "text": "Useful example", "postId": "1" }
  ],
  "profile": {
    "name": "Demo team"
  }
}

Start the pinned project-local binary:

Start the pinned JSON Server v1 release
npx json-server db.json

The current CLI watches the data file automatically and listens on localhost:3000 by default. Its verified options are --port, --host, and repeatable --static; run npx json-server --help against the installed version before adding another flag.

Verify CRUD behavior

Read the collection and one record:

Read posts from the mock API
curl --fail /posts
curl --fail /posts/1

Create, update, and delete a disposable record:

Exercise POST PATCH and DELETE
curl --fail -X POST /posts \
  -H 'Content-Type: application/json' \
  -d '{"title":"Temporary post","views":0}'

curl --fail -X PATCH /posts/1 \
  -H 'Content-Type: application/json' \
  -d '{"views":151}'

curl --fail -X DELETE /posts/2

JSON Server writes mutations back to the data file. Keep a clean fixture in version control or regenerate it before each repeatable test.

Use the current v1 query syntax

Conditions

The current v1 README uses field:operator=value. Available operators include lt, lte, gt, gte, eq, ne, in, contains, startsWith, and endsWith.

Filter with current v1 condition syntax
curl --fail '/posts?views:gte=100'
curl --fail '/posts?title:contains=post'

Legacy expressions such as views_gte=100 belong to v0.17.4 and should not be copied into a v1 test.

Sorting

Use a minus prefix for descending order and commas for multiple fields:

Sort with current v1 syntax
curl --fail '/posts?_sort=-views'
curl --fail '/posts?_sort=title,-views'

Pagination

Use _page with _per_page:

Paginate a v1 collection
curl --fail '/posts?_page=1&_per_page=10'

The v1 response is an object containing navigation fields and a data array. A client written for the v0 array response must be updated before migrating.

Relationships

Use _embed in the direction documented by the current README:

Embed related resources with current v1 syntax
curl --fail '/posts?_embed=comments'
curl --fail '/comments?_embed=post'

Do not use _expand in a v1 example. The current migration notes explicitly replace it with _embed.

Complex conditions

The current README also documents _where with a JSON object. URL-encode it in application code rather than assembling unescaped JSON into a user-controlled URL.

Keep legacy-only features with the legacy release

JSON Server 0.17.4 documents route mappings, CLI middleware, and a CommonJS module interface. The current v1 CLI help does not expose --routes or --middlewares, and current v1 package metadata declares an ES module.

If an existing mock depends on a route map, keep it pinned to v0.17.4 while you design and test a replacement:

Define a legacy route map
{
  "/api/*": "/$1"
}
Run a route map with pinned JSON Server 0.17.4
npx json-server@0.17.4 --watch db.json --routes routes.json

Do not present a v0 CommonJS middleware example as a v1 workaround. Use a separate, maintained application or reverse proxy when a v1 prototype needs authentication, validation, custom routing, or other server behavior.

Serve static development files

JSON Server automatically serves an existing public directory. The current v1 CLI can add another static directory with --static or -s:

Serve one additional static directory
npx json-server db.json --static ./static

Verify that API routes and static paths do not collide.

Share the mock API without making it public

The default localhost listener is the safest development choice. A remote team environment needs an explicit access design because JSON Server has no production authentication or authorization layer.

Before changing the host binding:

  1. Restrict the network path to a VPN, private network, or exact source addresses.
  2. Put HTTPS and authentication in a separate maintained gateway when traffic leaves a trusted host.
  3. Keep the JSON fixture free of real customer data, credentials, tokens, and secrets.
  4. Back up or regenerate the fixture before tests that write data.
  5. Confirm the service is not reachable from an unintended public address.

For a self-managed VPS, the customer owns installation, firewall and proxy configuration, TLS, authentication, updates, monitoring, fixture recovery, and removal of the temporary service.

Update and rollback

Keep the exact version in package.json and commit the lockfile. Before updating:

Record the installed JSON Server release
npm ls json-server
npx json-server --version

Run regression tests for every CRUD operation, condition, sort, pagination response shape, relationship, and static path the frontend uses. If the update changes behavior, restore package.json and the lockfile, reinstall, and restore the clean JSON fixture.

Do not move a legacy project from 0.17.4 to v1 by changing only the version number. String IDs, condition syntax, sorting, pagination response shape, relationships, CLI flags, and module integration all require review.

Troubleshooting

The package refuses the Node.js version

Run node --version and npm view json-server@1.0.0-beta.15 engines. The checked v1 release requires Node.js 22.12.0 or newer.

A copied flag is unknown

Run npx json-server --help in the project. --routes, --middlewares, and --watch appear in legacy documentation but not in the current v1 CLI help.

A filter returns the wrong records

Confirm the release. Use views:gte=100 with current v1, but views_gte=100 with v0.17.4.

Pagination breaks the frontend

Current v1 returns a pagination object with records under data; v0 returns an array. Update the client contract or remain on the pinned legacy release.

Relationship results are empty

Use string IDs and matching foreign keys in v1, then use _embed as documented. Do not use the removed _expand form.

When JSON Server is the wrong tool

Choose a purpose-built mock or test server when you need deterministic failure simulation, request matching, latency controls, contract validation, authentication, or complex state. Use a real application and database for production data, durability, access control, audit requirements, concurrent writes, migrations, and operational recovery.

JSON Server is most valuable when its scope stays small: a pinned, disposable API that lets frontend work proceed while the real backend contract is still being built.

Ready to host mock APIs on Virtarix VPS?

Compare self-managed VPS sizes for a private JSON Server development environment. Plans include root access, NVMe storage, IPv4 + IPv6, one backup, and one snapshot; you remain responsible for the application, access controls, updates, and recovery.

VPS S

For small sites, dev servers and Docker

$ 5 .50 /month
  • 3 cores
  • 6 GB
  • 50 GB NVMe
  • Unlimited
Get It Now
BEST SELLER

VPS M

For growing apps, websites and staging

$ 11 .40 /month
  • 6 cores
  • 16 GB
  • 100 GB NVMe
  • Unlimited
Get It Now
Peter French
About the Author Peter Frenchis the Managing Director at Virtarix, with over 17 years in the tech industry. He has co-founded a cloud storage business, led strategy at a global cloud computing leader, and driven market growth in cybersecurity and data protection.