Have you ever wanted to create a fake REST API for testing or prototyping purposes? If so, you might be interested in MY JSON Server, a free online service that lets you create and host your own mock API in minutes. In this blog post, I will show you how to use MY JSON Server to create a simple fake API with some sample data and how to access it from your web browser or code.
What is MY JSON Server?
MY JSON Server is a project by typicode, the creator of JSON Server, a popular open source tool for creating fake REST APIs with JSON files. MY JSON Server is a hosted version of JSON Server that allows you to use GitHub repositories as data sources for your mock APIs. This means you don’t need to install anything on your machine or set up any server configuration. You just need a GitHub account and a JSON file with some data.
How to use MY JSON Server?
To use MY JSON Server, you need to follow these steps:
- Create a GitHub repository with a
db.jsonfile in the root directory. This file will contain your data as an object with properties that correspond to the endpoints of your fake API. For example, if you want to have a/usersendpoint that returns a list of users, you need to have ausersproperty with an array of user objects in yourdb.jsonfile. You can use any valid JSON syntax and structure for your data, as long as it is an object at the root level. Here is an example of adb.jsonfile with some sample data:
jsonCopy code
{
"users": [
{
"id": 1,
"name": "Alice",
"email": "alice@example.com"
},
{
"id": 2,
"name": "Bob",
"email": "bob@example.com"
},
{
"id": 3,
"name": "Charlie",
"email": "charlie@example.com"
}
],
"posts": [
{
"id": 1,
"title": "Hello world",
"author": 1
},
{
"id": 2,
"title": "Lorem ipsum",
"author": 2
},
{
"id": 3,
"title": "Dolor sit amet",
"author": 3
}
],
"comments": [
{
"id": 1,
"body": "Nice post",
"postId": 1,
"userId": 2
},
{
"id": 2,
"body": "Thanks",
"postId": 1,
"userId": 1
},
{
"id": 3,
"body": "Great post",
"postId": 2,
"userId": 3
},
{
"id": 4,
"body": "Interesting",
"postId": 2,
"userId": 2
},
{
"id": 5,
"body": "Awesome post",
"postId": 3,
"userId": 1
}
]
}
- Push your
db.jsonfile to your GitHub repository and make sure it is publicly accessible. You can use any branch or subdirectory for yourdb.jsonfile, as long as you specify it in the URL of your fake API. - Use the following URL format to access your fake API:
https://my-json-server.typicode.com/<your-github-username>/<your-github-repository>/<optional-branch-or-subdirectory>
For example, if your GitHub username is john and your GitHub repository is my-fake-api, you can use this URL to access your fake API: https://my-json-server.typicode.com/john/my-fake-api
If you have your db.json file in a different branch or subdirectory, you need to append it to the URL. For example, if your db.json file is in the master branch and the data subdirectory, you can use this URL: https://my-json-server.typicode.com/john/my-fake-api/master/data
- Enjoy your fake API! You can use any HTTP method (GET, POST, PUT, PATCH, DELETE) and any query parameter (filter, sort, slice, etc.) supported by JSON Server to interact with your data. You can also use relationships between your data by using the
_embedand_expandparameters. For more details on how to use JSON Server features, check out the documentation here.
Here are some examples of requests you can make to your fake API:
- GET
https://my-json-server.typicode.com/john/my-fake-api/users: returns the list of users - GET
https://my-json-server.typicode.com/john/my-fake-api/users/1: returns the user with id 1 - GET
https://my-json-server.typicode.com/john/my-fake-api/users?name=Alice: returns the users with name Alice - GET
https://my-json-server.typicode.com/john/my-fake-api/posts?_embed=comments: returns the list of posts with their comments - GET
https://my-json-server.typicode.com/john/my-fake-api/posts/1?_expand=author: returns the post with id 1 with its author - POST
https://my-json-server.typicode.com/john/my-fake-api/users: creates a new user with the data in the request body - PUT
https://my-json-server.typicode.com/john/my-fake-api/users/1: updates the user with id 1 with the data in the request body - PATCH
https://my-json-server.typicode.com/john/my-fake-api/users/1: partially updates the user with id 1 with the data in the request body - DELETE
https://my-json-server.typicode.com/john/my-fake-api/users/1: deletes the user with id 1
That’s it! You have successfully created and used a fake REST API with MY JSON Server. You can use this service for any purpose, such as testing, prototyping, learning, or teaching. However, keep in mind that this is not a production-ready service and there are some limitations and caveats to be aware of:
- Your data is not persistent and may be reset at any time. You should not rely on it for any critical or sensitive data.
- Your data is publicly accessible and anyone can view or modify it. You should not use it for any private or confidential data.
- Your data is limited to 10,000 items per repository and 100 items per request. You should not use it for any large or complex data.
- Your data is cached by GitHub and may not reflect the latest changes. You should not use it for any real-time or dynamic data.
If you need more features or control over your fake API, you can always use JSON Server on your own machine or server. You can also check out other alternatives, such as Mocky, Mockoon, or Postman Mock Server.
I hope you found this blog post informative and useful. If you have any questions or feedback, feel free to leave a comment below or contact me on Twitter. Happy coding!




Leave a comment