Getting Started with FastAPI: Build Your First REST API in Minutes

SENA DOMONHEDOSENA DOMONHEDO
3 min read

Introduction

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.7+ based on standard Python type hints. Whether you're creating a microservice, a full-fledged backend for your application, or just learning modern backend development, FastAPI is your friend.

In this article, we’ll go step-by-step to build a simple yet well-structured REST API using FastAPI — perfect for beginners or anyone looking to level up their backend skills.


Why FastAPI?

Here’s why developers love it:

Fast: Built on Starlette and Pydantic — incredibly performant
Automatic Docs: Swagger UI and ReDoc generated instantly
Type-Safe: Python typing for inputs, outputs, validation
Easy to Learn: Minimal boilerplate and async-first


1. Project Setup

Let’s start with a clean environment.

🔧 Install FastAPI and Uvicorn:

bashCopierModifierpip install fastapi uvicorn

We’ll use Uvicorn as the ASGI server to run our app.

cssCopierModifierfastapi-tutorial/
│
├── main.py
├── models.py
├── schemas.py
└── routers/
    └── items.py

We’ll keep it modular and scalable from the beginning.


2. Creating Your First Endpoint

Let’s start with main.py:

pythonCopierModifierfrom fastapi import FastAPI
from routers import items

app = FastAPI()

# Include our router
app.include_router(items.router)

@app.get("/")
def read_root():
    return {"message": "Welcome to FastAPI!"}

3. Create Models and Schemas

Let’s add a basic Item object.

schemas.py – using Pydantic for validation

pythonCopierModifierfrom pydantic import BaseModel

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    in_stock: bool = True

4. Add a Router

Let’s build our endpoints in routers/items.py.

routers/items.py

pythonCopierModifierfrom fastapi import APIRouter
from schemas import Item

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

# In-memory database
items_db = []

@router.post("/")
def create_item(item: Item):
    items_db.append(item)
    return {"message": "Item created", "item": item}

@router.get("/")
def list_items():
    return items_db

Now when you run the app, the /items route is available.


5. Run Your FastAPI App

bashCopierModifieruvicorn main:app --reload

Visit http://localhost:8000/docs
🎉 You get an auto-generated Swagger UI to test your API!


6. Try It Out

POST /items with this JSON:

jsonCopierModifier{
  "name": "Notebook",
  "description": "A productivity tool",
  "price": 12.99,
  "in_stock": true
}

GET /items will return all added items.


Bonus: Add Modularity from the Start

Using routers/, schemas.py, and models.py (if needed for DB) helps your project scale.

You can also easily switch to SQLAlchemy, add middleware, auth, async DB access, etc.


Conclusion

You’ve just created a modular, modern, and production-friendly API with FastAPI in minutes!

✅ Modular structure
✅ Auto-validation and docs
✅ Beginner-friendly and scalable


🔗 Resources


💬 Have questions or want a part 2 with database integration (SQLite + SQLAlchemy)? Drop a comment below!

0
Subscribe to my newsletter

Read articles from SENA DOMONHEDO directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

SENA DOMONHEDO
SENA DOMONHEDO

Fullstack Developer · Founder @ Sena Dev Studio I build modern SaaS tools with Vue.js & FastAPI · Sharing tutorials, dev logs, and business insights for solo devs & makers. Building in public 🚀