Boilerplate for Basic FastAPI CRUD You Can Reuse

Project Overview

This guide walks you through building a reusable FastAPI CRUD boilerplate from scratch.

The goal is not just to make APIs work.

The goal is to understand:

  • how FastAPI applications are structured
  • how routing works
  • how CRUD APIs are written
  • how backend servers run
  • how developers actually start real backend projects

By the end, you will have:

  • a working FastAPI server
  • reusable CRUD routes
  • Swagger API documentation
  • a clean starter setup for future projects

This setup is perfect for:

  • backend beginners
  • Python developers moving into APIs
  • interview preparation
  • side projects
  • reusable starter templates

Why FastAPI?

FastAPI is one of the most popular modern backend frameworks in Python.

Developers prefer FastAPI because it provides:

  • clean syntax
  • automatic API documentation
  • very high performance
  • async support
  • built-in validation
  • excellent developer experience

FastAPI is commonly used for:

  • REST APIs
  • AI backends
  • SaaS applications
  • authentication systems
  • internal tools
  • microservices

Step 1 — Create Project Folder

Start by creating a new project directory.

Open terminal:

mkdir fastapi-boilerplate
cd fastapi-boilerplate

At this point your folder is empty.

Everything for the backend project will live inside this directory.


Step 2 — Create Virtual Environment

A virtual environment isolates project dependencies.

Without it, packages installed for one project can conflict with another.

Windows

Create virtual environment:

python -m venv venv

Activate it:

venv\Scripts\activate

You should now see:

(venv)

That means the environment is active.

Mac/Linux

python3 -m venv venv
source venv/bin/activate

Step 3 — Install FastAPI and Uvicorn

Install required packages:

pip install fastapi uvicorn

What Are These Packages?

FastAPI

FastAPI is the backend framework.

It handles:

  • routing
  • request handling
  • validation
  • API documentation
  • response generation

Uvicorn

Uvicorn is the server that actually runs the application.

Think of it like this:

FastAPI -> defines application
Uvicorn -> runs application

Without Uvicorn, your API will not start.


Step 4 — Create the Main Application File

Create a file called:

main.py

Add the following code:

from fastapi import FastAPI
 
app = FastAPI()
 
 
@app.get("/")
def home():
    return {"message": "Server is running"}
 
 
@app.get("/login")
def login():
    return {"message": "Login ready"}

Understanding the Code

Before moving ahead, understand what is happening here.

Creating the FastAPI App

app = FastAPI()

This creates the FastAPI application instance.

Everything in your backend will attach to this app.

Creating Routes

@app.get("/")

This is called a route decorator.

It tells FastAPI:

  • which URL to listen to
  • which HTTP method to use

Returning JSON Responses

return {"message": "Server is running"}

FastAPI automatically converts Python dictionaries into JSON responses.

That is why APIs return JSON in browsers and frontend applications.


Step 5 — Run the FastAPI Server

Start the server:

uvicorn main:app --reload

Command Breakdown

main      -> filename (main.py)
app       -> FastAPI object
--reload  -> auto restart server on file changes

The --reload flag is extremely useful during development because the server restarts automatically whenever code changes.


Step 6 — Open API in Browser

Open:

http://127.0.0.1:8000

You should see:

{
  "message": "Server is running"
}

That confirms your FastAPI application is working correctly.


Step 7 — Test Another Route

Now test the login endpoint.

Open:

http://127.0.0.1:8000/login

Expected response:

{
  "message": "Login ready"
}

This confirms routing is functioning properly.


Step 8 — Swagger Documentation

One of FastAPI’s biggest advantages is automatic API documentation.

Open:

http://127.0.0.1:8000/docs

You will see Swagger UI.

Why Swagger Is Important

Swagger lets developers:

  • test APIs directly from browser
  • inspect request/response models
  • understand endpoints quickly
  • collaborate with frontend developers

Most backend frameworks require extra configuration for this.

FastAPI generates it automatically.


Understanding CRUD Operations

CRUD stands for:

OperationMeaning
CREATEAdd new data
READFetch existing data
UPDATEModify data
DELETERemove data

Almost every backend application is built around CRUD operations.


CREATE API — POST Method

@app.post("/users")
def create_user():
    return {"message": "User created"}

Used for creating resources.

Examples:

  • creating users
  • creating products
  • creating blog posts

READ API — GET Method

@app.get("/users")
def get_users():
    return {"message": "All users"}

Used for fetching data.


UPDATE API — PUT Method

@app.put("/users/{user_id}")
def update_user(user_id: int):
    return {"message": f"User {user_id} updated"}

Used for updating existing resources.

Understanding Path Parameters

Notice this part:

{user_id}

This is called a path parameter.

FastAPI automatically extracts it from the URL.


DELETE API — DELETE Method

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

Used for removing resources.


Full FastAPI CRUD Boilerplate

Here is the complete reusable starter boilerplate:

from fastapi import FastAPI
 
app = FastAPI()
 
 
@app.get("/")
def home():
    return {"message": "API working"}
 
 
# CREATE
@app.post("/users")
def create_user():
    return {"message": "User created"}
 
 
# READ
@app.get("/users")
def get_users():
    return {"message": "Users fetched"}
 
 
# UPDATE
@app.put("/users/{user_id}")
def update_user(user_id: int):
    return {"message": f"User {user_id} updated"}
 
 
# DELETE
@app.delete("/users/{user_id}")
def delete_user(user_id: int):
    return {"message": f"User {user_id} deleted"}

This is the simplest reusable FastAPI CRUD setup you can use for learning or starting projects.


Beginner-Friendly Folder Structure

fastapi-boilerplate/

├── venv/
├── main.py
├── requirements.txt

As projects grow, developers usually expand the structure like this:

fastapi-boilerplate/

├── routers/
├── models/
├── schemas/
├── database/
├── services/
├── middleware/

You will learn these gradually as you move toward production-grade backend systems.


Save Installed Packages

Generate requirements file:

pip freeze > requirements.txt

This stores all installed dependencies.

Later you can reinstall everything using:

pip install -r requirements.txt

This becomes important when:

  • deploying projects
  • working in teams
  • using Docker
  • setting up CI/CD

Important Concepts You Just Learned

ConceptMeaning
FastAPI()Creates FastAPI app
@app.get()Route decorator
defFunction definition
returnSends response
uvicornASGI server
--reloadAuto restart
Path Parameter/users/{id}

What to Learn Next

After understanding this boilerplate, continue learning in this order:

  1. Path Parameters
  2. Query Parameters
  3. Request Body
  4. Pydantic Models
  5. Response Models
  6. Status Codes
  7. Dependency Injection
  8. SQLAlchemy
  9. PostgreSQL
  10. JWT Authentication
  11. Async APIs
  12. Middleware
  13. Docker
  14. Deployment

These are the concepts used in real-world FastAPI backend systems.


Final Thoughts

This boilerplate may look simple, but the concepts here form the foundation of modern backend development.

Most production backend systems eventually scale from the same ideas:

  • routes
  • request handling
  • CRUD operations
  • validation
  • database interaction

Once comfortable with this setup, your next goal should be:

  • connecting PostgreSQL
  • learning Pydantic models
  • integrating SQLAlchemy
  • structuring larger FastAPI applications

That is where backend engineering starts becoming truly interesting.


Happy Coding! 🚀

This document was created with ❤️ for developers, by Anubhaw. Feel free to suggest improvements or connect @ ContactMe