Skip to main content

Overview

FastrAPI provides decorators for all standard HTTP methods. Each decorator registers a route handler for the specified path and HTTP method.
from fastrapi import FastrAPI

app = FastrAPI()

@app.get("/users")
def get_users():
    return [{"id": 1, "name": "Alice"}]

@app.post("/users")
def create_user(data):
    return {"id": 2, "name": data["name"]}

HTTP method decorators

@app.get()

Define a GET endpoint for retrieving data.
@app.get("/")
def home():
    return {"message": "Hello World"}

@app.get("/items/{item_id}")
def get_item(item_id: int):
    return {"item_id": item_id, "name": "Item name"}

Parameters

path
str
required
The URL path for the endpoint. Supports path parameters with {param_name} syntax

Returns

decorator
callable
A decorator function that registers the route handler

@app.post()

Define a POST endpoint for creating resources or submitting data.
from pydantic import BaseModel

class User(BaseModel):
    name: str
    email: str

@app.post("/users")
def create_user(user: User):
    return {
        "id": 123,
        "name": user.name,
        "email": user.email
    }

Parameters

path
str
required
The URL path for the endpoint

Returns

decorator
callable
A decorator function that registers the route handler

@app.put()

Define a PUT endpoint for updating existing resources.
@app.put("/users/{user_id}")
def update_user(user_id: int, data):
    return {
        "id": user_id,
        "name": data["name"],
        "updated": True
    }

Parameters

path
str
required
The URL path for the endpoint. Typically includes an ID parameter

Returns

decorator
callable
A decorator function that registers the route handler

@app.delete()

Define a DELETE endpoint for removing resources.
@app.delete("/users/{user_id}")
def delete_user(user_id: int):
    return {
        "message": f"User {user_id} deleted",
        "success": True
    }

Parameters

path
str
required
The URL path for the endpoint. Typically includes an ID parameter

Returns

decorator
callable
A decorator function that registers the route handler

@app.patch()

Define a PATCH endpoint for partial updates to resources.
@app.patch("/users/{user_id}")
def patch_user(user_id: int, data):
    return {
        "id": user_id,
        "updated_fields": list(data.keys())
    }

Parameters

path
str
required
The URL path for the endpoint

Returns

decorator
callable
A decorator function that registers the route handler

@app.websocket()

Define a WebSocket endpoint for bidirectional communication.
@app.websocket("/ws")
async def websocket_endpoint(websocket):
    await websocket.accept()
    while True:
        data = await websocket.receive_text()
        await websocket.send_text(f"Echo: {data}")

Parameters

path
str
required
The URL path for the WebSocket endpoint. Must start with /

Returns

decorator
callable
A decorator function that registers the WebSocket handler

Path parameters

All route decorators support path parameters using the {param_name} syntax.
@app.get("/users/{user_id}")
def get_user(user_id: int):
    return {"user_id": user_id}

@app.get("/posts/{post_id}/comments/{comment_id}")
def get_comment(post_id: int, comment_id: int):
    return {
        "post_id": post_id,
        "comment_id": comment_id
    }
Path parameters are automatically extracted and passed to your handler function. Type hints are used for validation.

Query parameters

Query parameters are automatically extracted from the URL.
@app.get("/search")
def search(q: str, limit: int = 10):
    return {
        "query": q,
        "limit": limit,
        "results": []
    }

# GET /search?q=python&limit=5

Request body

For POST, PUT, PATCH, and DELETE methods, the request body is automatically parsed and passed to your handler.
from pydantic import BaseModel

class Item(BaseModel):
    name: str
    price: float
    description: str = None

@app.post("/items")
def create_item(item: Item):
    return {
        "name": item.name,
        "price": item.price,
        "description": item.description
    }

Complete example

from fastrapi import FastrAPI
from pydantic import BaseModel

app = FastrAPI()

class User(BaseModel):
    name: str
    email: str
    age: int

@app.get("/")
def root():
    return {"message": "Welcome to the API"}

@app.get("/users")
def list_users():
    return [{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]

@app.get("/users/{user_id}")
def get_user(user_id: int):
    return {"id": user_id, "name": "Alice"}

@app.post("/users")
def create_user(user: User):
    return {
        "id": 3,
        "name": user.name,
        "email": user.email,
        "age": user.age
    }

@app.put("/users/{user_id}")
def update_user(user_id: int, user: User):
    return {
        "id": user_id,
        "name": user.name,
        "email": user.email,
        "age": user.age,
        "updated": True
    }

@app.delete("/users/{user_id}")
def delete_user(user_id: int):
    return {"message": f"User {user_id} deleted"}

@app.patch("/users/{user_id}")
def patch_user(user_id: int, data):
    return {"id": user_id, "updated_fields": list(data.keys())}

if __name__ == "__main__":
    app.serve("127.0.0.1", 8080)

Build docs developers (and LLMs) love