Reading preferences
Concept

APIs, Validation, and Python Web Frameworks

Follow a request through validation and a prepared response.

python, api, fastapi, flask, pydantic, validation

A request crosses a boundary

An API defines how a caller requests an operation and how the service responds. A request can be syntactically valid but unauthorised or impossible to fulfil. Document methods, input constraints, status codes, and failure behaviour. FastAPI and Flask are Python web frameworks; Pydantic defines models that validate and parse data.

Python illustration

In a separate environment with FastAPI and Pydantic installed:

from fastapi import FastAPI
from pydantic import BaseModel, Field

app = FastAPI()

class Reading(BaseModel):
    title: str
    pages: int = Field(ge=1)

@app.post("/readings")
def create_reading(reading: Reading):
    return {"title": reading.title, "pages": reading.pages}

For the prepared request body {"title": "Notes", "pages": 3}, the handler returns those two values as JSON. A body with zero pages fails the declared constraint before the handler runs. The example stores nothing, has no authentication, and is not a production service.

See the reasoning

Trace a request boundary

Compare a valid request with a rejected page count. This is a prepared demonstration, not live code execution.

Receive a request

Request processing
  1. Requesttitle: Notes; pages: 3Current focus
  2. Validationpending
  3. Handlernot run
  4. Responsepending

The prepared request provides a title and page count.

Frameworks and boundaries

Flask can define a similar route with @app.post("/readings"), parse the request using request.get_json(), and return a dictionary. Unlike the FastAPI example above, a Flask handler must explicitly arrange the validation it needs, for example by calling a Pydantic model. Both need deliberate error handling, authentication, authorisation, request limits, and deployment configuration appropriate to the application.

For example, in a separate environment with Flask and Pydantic installed:

from flask import Flask, request
from pydantic import BaseModel, Field, ValidationError

app = Flask(__name__)

class Reading(BaseModel):
    title: str
    pages: int = Field(ge=1)

@app.post("/readings")
def create_reading():
    try:
        reading = Reading.model_validate(request.get_json())
    except ValidationError:
        return {"error": "Invalid reading"}, 400
    return {"title": reading.title, "pages": reading.pages}

For the same valid JSON, this handler returns the same fields. Invalid model data gets a 400 response; malformed JSON or an unsuitable content type is handled by Flask's request parser. This example also has no storage or authorisation.

Pydantic may coerce inputs depending on field configuration; choose strict validation where necessary. Avoid exposing secrets or internal exceptions in responses. An operation that changes stored data also needs a clear transaction and failure policy. Intuvo neither runs these services nor accepts requests from learners.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What should an API contract describe?
2. What does validation help check?
3. How do FastAPI and Flask relate to Intuvo?
4. Does a valid request body prove the caller may perform an operation?

References and further reading