Backend · 6 min read
Building a Flask REST API from scratch
When I started learning backend development, the hardest part wasn’t writing a route — it was structuring a project so it stayed readable as it grew. This is the setup I now reach for every time I build a Flask REST API.
Start with a factory
Instead of a giant app.py, create the app inside a function. It keeps configuration
flexible and makes testing painless.
from flask import Flask
def create_app(config=None):
app = Flask(__name__)
app.config.from_object(config or "app.config.DefaultConfig")
register_blueprints(app)
register_errors(app)
return app
Organise with blueprints
Group related endpoints into blueprints. Each resource gets its own folder with routes, schemas and a service layer — so business logic never leaks into the view functions.
Clean structure is a feature. Future-you is the main user of your code.
Test as you build
With the factory pattern, tests are simple: build the app, use the test client, assert on JSON. I write a test for every endpoint — happy path and failure case.
def test_create_todo(client):
res = client.post("/todos", json={"title": "Ship it"})
assert res.status_code == 201
assert res.get_json()["title"] == "Ship it"
That’s the core loop: factory, blueprints, explicit errors and tests. You can explore the full template on GitHub.