On this page

Defining Multiple Endpoints and HTTP Methods in FastAPI

Published on August 9, 2026

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

A FastAPI application is built up one route at a time: each combination of a URL path and an HTTP method gets its own decorated function, and the framework dispatches incoming requests to whichever function matches. GET and POST look similar on the surface — both are just decorators wrapping a function — but they serve different purposes, and treating a POST endpoint as if it were just "a GET that happens to say POST" skips over the part that actually makes POST useful: receiving data in a request body.

The sections below build out a small app with several GET routes, add a POST endpoint that genuinely accepts and validates incoming data, cover the remaining HTTP methods and what each implies, and end with a pattern for keeping routes organized once an app grows past a handful of them.


Endpoints, Routes, and Path Operations​

An endpoint is a specific URL a client can send a request to — /status or /users, for example. A route is more precise than that: it's the pairing of an endpoint with a particular HTTP method, since GET /users and POST /users are two different routes even though they share the same path. FastAPI's own documentation calls a decorated function like @app.get("/status") a path operation — "path" for the URL, "operation" for the HTTP method — and that's the term used going forward here, since it's the one that shows up directly in FastAPI's own error messages and interactive docs.


Adding Multiple GET Endpoints​

A single app can register as many path operations as needed, each with its own decorator and function:


from fastapi import FastAPI



app = FastAPI()



@app.get("/")

def home():

    return {"message": "Welcome to the FastAPI course!"}



@app.get("/status")

def status():

    return {"status": "OK", "uptime": "24h"}



@app.get("/about")

def about():

    return {"author": "Enrico", "course": "FastAPI Mastery"}

Each function is independent — nothing about status() depends on home() existing, and FastAPI matches an incoming request to a path operation purely by comparing the request's method and path against what's registered. Running the server,


fastapi dev main.py

and visiting http://127.0.0.1:8000/, http://127.0.0.1:8000/status, and http://127.0.0.1:8000/about in a browser returns the three JSON responses defined above, one per route, since a browser's address bar always sends a GET request by default.


Accepting Data with POST​

GET requests are meant for retrieving data and don't carry a request body — everything a GET request needs is expected to fit in the URL itself. POST requests exist specifically to carry a body, and a POST endpoint that ignores that body and always returns the same static response isn't really using POST for what it's for. A feedback endpoint is a natural place to actually accept one:


from pydantic import BaseModel



class Feedback(BaseModel):

    message: str

    rating: int



@app.post("/feedback")

def submit_feedback(feedback: Feedback):

    return {

        "received": True,

        "message": feedback.message,

        "rating": feedback.rating,

    }

Feedback is a Pydantic model — a class describing the shape of the data expected in the request body, with each field's type hint acting as a validation rule the same way a path parameter's type hint does. Declaring feedback: Feedback as the function's parameter tells FastAPI to read the incoming request body as JSON, validate it against the Feedback model, and hand back a fully-typed Feedback instance — a request missing rating or sending a string where an integer is expected gets rejected automatically, before submit_feedback ever runs.

Testing this from the command line requires sending an actual body along with the request, which a plain browser visit can't do:


http POST http://127.0.0.1:8000/feedback message="Great course!" rating=5

or, with curl:


curl -X POST http://127.0.0.1:8000/feedback \

  -H "Content-Type: application/json" \

  -d '{"message": "Great course!", "rating": 5}'

Both send the same JSON body; httpie's syntax just builds it from key=value arguments automatically.


The Other HTTP Methods​

FastAPI provides a matching decorator for every standard HTTP method — @app.put, @app.delete, @app.patch, and others — and each one carries a specific meaning in typical API design, not just a different verb attached to the same kind of action. PUT conventionally replaces a resource entirely with the data provided, PATCH applies a partial update to specific fields, and DELETE removes a resource. A DELETE route, for instance, usually identifies the resource through a path parameter rather than a body, since there's nothing left to send once the resource is gone:


@app.delete("/tasks/{task_id}")

def delete_task(task_id: int):

    return {"deleted": task_id}

Choosing the method that matches the actual intent of an action — not just picking whichever one seems convenient — is what makes an API's behavior predictable to anyone calling it without having to read the implementation first.


Organizing Routes by Resource​

As the number of routes grows, grouping them by the resource they operate on keeps related endpoints easy to find:


@app.get("/users")

def list_users():

    return {"users": ["Alice", "Bob"]}



@app.get("/tasks")

def list_tasks():

    return {"tasks": ["Buy milk", "Write code"]}

This grouping is a naming convention on its own, but FastAPI also provides a dedicated tool for splitting routes across separate files once an app grows large enough that keeping everything in one main.py becomes unwieldy: APIRouter.


# routers/users.py

from fastapi import APIRouter



router = APIRouter(prefix="/users", tags=["users"])



@router.get("/")

def list_users():

    return {"users": ["Alice", "Bob"]}

# main.py

from fastapi import FastAPI

from routers import users



app = FastAPI()

app.include_router(users.router)

APIRouter behaves like a smaller version of the main FastAPI app — routes get registered on it with the same @router.get/@router.post decorators — and prefix="/users" means every route defined on it is automatically mounted under /users once app.include_router(users.router) wires it into the main application. This is what makes the "group by resource" convention from above scale past a single file: a users.py, tasks.py, and feedback.py, each with their own router, wired together in main.py.


Practice: Adding Three More Endpoints​

Applying the same pattern to three small, self-contained endpoints:


from datetime import datetime



@app.get("/ping")

def ping():

    return {"pong": True}



@app.get("/version")

def version():

    return {"version": "1.0.0"}



@app.get("/time")

def current_time():

    return {"time": datetime.now()}

/ping and /version follow exactly the pattern used throughout this article — a decorator, a function, a returned dictionary. /time is worth a closer look: datetime.now() returns a Python datetime object, not a string, and yet returning it directly still produces a valid JSON response. FastAPI runs every returned value through its own JSON encoder before sending a response, and that encoder already knows how to convert a datetime object into an ISO 8601 formatted string — no manual .isoformat() call or str() conversion is needed for this specific type, since the framework handles it as part of its standard response processing.

Key Takeaways​

An endpoint, a route, and a path operation describe increasingly specific things: an endpoint is a URL, a route pairs that URL with a method, and a path operation is FastAPI's own name for the decorated function that handles it. GET and POST aren't interchangeable despite both being one-line decorators — a POST endpoint earns its method by actually accepting a request body, typically validated through a Pydantic model the same way a path parameter is validated through a type hint. PUT, PATCH, and DELETE each carry a specific convention worth matching to the action an endpoint actually performs, rather than being chosen arbitrarily. And once an app accumulates enough routes that a single file stops being manageable, APIRouter is the tool built specifically for splitting them across files while keeping the resulting URLs identical to what a single-file version would have produced.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Basic Routing

Next

Dynamic Routing and Query Parameters