Mock api server with json-server

A free online REST API that you can use whenever you need some fake data. It can be in a README on GitHub, for a demo on CodeSandbox, in code examples on Stack Overflow, ...or simply to test things locally.

About

Free fake API for testing and prototyping.

Website: https://mock-api-demo.herokuapp.com

Features:

  • REST API with Node.js and json-server: Deploying to Heroku
  • Create random database with faker.js library
  • Custom server response
  • Home page

Deploy (local)

  • Requited: Node js environment Node js
  • Clone this project
  • Run step by step in terminal
npm install

node generate-data.js //optional: generate random data

npm run dev //start dev server
// Open in browser: https://localhost:3000/

Usage

Getting a resource

fetch('https://mock-api-demo.herokuapp.com/api/posts/1')
  .then((response) => response.json())
  .then((json) => console.log(json));

👇 Output

{
  id: 1,
  title: '...',
  body: '...',
  userId: 1
}

Listing all resources

fetch('https://mock-api-demo.herokuapp.com/api/posts')
  .then((response) => response.json())
  .then((json) => console.log(json));

👇 Output

[
  { id: 1, title: '...' /* ... */ },
  { id: 2, title: '...' /* ... */ },
  { id: 3, title: '...' /* ... */ },
  /* ... */
  { id: 100, title: '...' /* ... */ },
];

Creating a resource

fetch('https://mock-api-demo.herokuapp.com/api/posts', {
  method: 'POST',
  body: JSON.stringify({
    title: 'foo',
    body: 'bar',
    userId: 1,
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
  .then((response) => response.json())
  .then((json) => console.log(json));

👇 Output

{
  id: 101,
  title: 'foo',
  body: 'bar',
  userId: 1
}

Updating a resource

fetch('https://mock-api-demo.herokuapp.com/api/posts/1', {
  method: 'PUT',
  body: JSON.stringify({
    id: 1,
    title: 'foo',
    body: 'bar',
    userId: 1,
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
  .then((response) => response.json())
  .then((json) => console.log(json));

👇 Output

{
  id: 1,
  title: 'foo',
  body: 'bar',
  userId: 1
}

Patching a resource

fetch('https://mock-api-demo.herokuapp.com/api/posts/1', {
  method: 'PATCH',
  body: JSON.stringify({
    title: 'foo',
  }),
  headers: {
    'Content-type': 'application/json; charset=UTF-8',
  },
})
  .then((response) => response.json())
  .then((json) => console.log(json));

👇 Output

{
  id: 1,
  title: 'foo',
  body: '...',
  userId: 1
}

Deleting a resource

fetch('https://mock-api-demo.herokuapp.com/api/posts/1', {
  method: 'DELETE',
});

Filtering resources

// This will return all the posts that belong to the first user
fetch('https://mock-api-demo.herokuapp.com/api/posts?userId=1')
  .then((response) => response.json())
  .then((json) => console.log(json));

Listing nested resources

// This is equivalent to /comments?postId=1
fetch('https://mock-api-demo.herokuapp.com/api/posts/1/comments')
  .then((response) => response.json())
  .then((json) => console.log(json));

The available nested routes are:


License

MIT License.