Every time I start a new frontend project, the same problem hits: the backend isn't ready yet.
I used to build minimal Express/Flask servers just to have something to fetch from. That's at least 30 minutes of boilerplate before writing any frontend code.
Then I built a tool that creates a full REST API from a JSON file in 10 seconds.
The Problem
You're building a React dashboard. You need:
-
GET /users— list of users -
GET /users/1— single user -
POST /users— create user -
PUT /users/1— update user -
DELETE /users/1— delete user
Plus filtering, pagination, and sorting.
Setting this up with Express: 30-60 minutes.
Setting this up with json-api-mocker: 10 seconds.
How It Works
- Create a JSON file with your data:
{
"users": [
{"id": 1, "name": "Alice", "email": "alice@test.com", "role": "admin"},
{"id": 2, "name": "Bob", "email": "bob@test.com", "role": "user"}
],
"posts": [
{"id": 1, "userId": 1, "title": "Hello World"},
{"id": 2, "userId": 2, "title": "Python Tips"}
]
}
- Run the mocker:
pip install flask
python mocker.py --data data.json --port 3000
- You now have full CRUD for every collection:
# List all users
curl http://localhost:3000/users
# Filter by field
curl http://localhost:3000/users?role=admin
# Pagination
curl http://localhost:3000/users?_page=1&_limit=10
# Sort
curl http://localhost:3000/users?_sort=name&_order=asc
# Full-text search
curl http://localhost:3000/users?q=alice
# Create
curl -X POST http://localhost:3000/users \
-H "Content-Type: application/json" \
-d '{"name": "Charlie", "email": "charlie@test.com"}'
The Code (Under 100 Lines)
The core is surprisingly simple — Flask + a JSON file:
from flask import Flask, jsonify, request, abort
import json
app = Flask(__name__)
DATA = {}
def register_routes(collection):
def get_all():
items = DATA.get(collection, [])
# Apply filters from query params
for key, val in request.args.items():
if not key.startswith('_'):
items = [i for i in items if str(i.get(key, '')) == val]
return jsonify(items)
def get_one(item_id):
item = next((i for i in DATA[collection] if i['id'] == item_id), None)
return jsonify(item) if item else abort(404)
def create():
item = request.get_json()
item['id'] = max(i['id'] for i in DATA[collection]) + 1
DATA[collection].append(item)
return jsonify(item), 201
app.add_url_rule(f'/{collection}', f'{collection}_list', get_all)
app.add_url_rule(f'/{collection}/<int:item_id>', f'{collection}_get', get_one)
app.add_url_rule(f'/{collection}', f'{collection}_create', create, methods=['POST'])
# Load data and register routes
with open('data.json') as f:
DATA = json.load(f)
for key in DATA:
register_routes(key)
app.run(port=3000)
That's it. Every key in your JSON becomes a REST endpoint.
Killer Features
1. Simulate Slow APIs
python mocker.py --data data.json --delay 500
Now every response takes 500ms. Perfect for testing loading states and skeleton screens.
2. Read-Only Mode
python mocker.py --data data.json --readonly
POST/PUT/DELETE return 405. Great for demo environments.
3. Auto-Persistence
Changes are saved back to the JSON file. Restart the server and your data is still there.
4. CORS Enabled by Default
No more "Access-Control-Allow-Origin" headaches. Works with any frontend framework out of the box.
When to Use This vs. Real Backends
| Use Mock API | Use Real Backend |
|---|---|
| Prototyping UI | Production |
| Frontend demos | User-facing features |
| Unit/integration tests | Load testing |
| Hackathons | When you need auth/validation |
| Teaching/tutorials | When data integrity matters |
Get It
git clone https://github.com/spinov001-art/json-api-mocker
cd json-api-mocker
pip install flask
python mocker.py --data data.json
The full version includes pagination, sorting, full-text search, delay simulation, and read-only mode — all in under 150 lines of Python.
What's your go-to tool for mocking APIs during development? I'd love to hear alternatives.
For more developer tools, check out my collection of 140+ open-source projects.
Follow for more Python tooling and API tutorials.
Top comments (0)