How I Started Working with FastAPI — And Why It Felt Surprisingly Easy

When I decided to learn backend development with Python, I kept hearing one name repeatedly: FastAPI.

People described it as fast, modern, and developer-friendly.
What I didn’t expect was how quickly I could go from zero to a working, documented API—with almost no boilerplate.

This post walks through how I started with FastAPI, why it felt easy, and a practical code example that shows its biggest strength: automatic Swagger documentation.


Why I Chose FastAPI

I wanted a framework that:

  • Uses modern Python
  • Enforces good practices by default
  • Makes APIs self-documented
  • Doesn’t hide what’s happening under the hood

FastAPI checked all those boxes.

The biggest surprise?
Swagger comes built-in and works automatically.


Step 1: Installing FastAPI

Setup was straightforward:

pip install fastapi uvicorn
  • fastapi → the framework
  • uvicorn → the ASGI server

No plugins. No configuration files.


Step 2: My First FastAPI App

Here’s the first working API I wrote:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message": "Hello FastAPI"}

Run it using:

uvicorn main:app --reload

At this point, two things work immediately:

  • API endpoint: http://127.0.0.1:8000/
  • Swagger UI (this is the magic): http://127.0.0.1:8000/docs

I hadn’t written a single line of documentation—yet Swagger was already there.


Step 3: Seeing Swagger in Action

Opening /docs shows:

  • All endpoints
  • HTTP methods
  • Parameters
  • Request/response schemas
  • A “Try it out” button

This changes how you build APIs.
Instead of guessing or using Postman blindly, Swagger becomes your development companion.


Step 4: Adding Real Inputs (Path & Query Parameters)

@app.get("/items/{item_id}")
def get_item(item_id: int, q: str | None = None):
    return {
        "item_id": item_id,
        "query": q
    }

What FastAPI does automatically:

  • Validates item_id is an integer
  • Rejects invalid input with clear errors
  • Updates Swagger UI instantly

No manual validation logic needed.


Step 5: Request Bodies with Validation

FastAPI uses Pydantic models for data validation.

from pydantic import BaseModel

class BlogPost(BaseModel):
    title: str
    content: str
    published: bool = True

@app.post("/posts")
def create_post(post: BlogPost):
    return post

In Swagger:

  • Input fields appear automatically
  • Required vs optional fields are clear
  • Types are enforced
  • Errors are descriptive

This is where FastAPI really feels easy but powerful.


Step 6: Status Codes and Clean APIs

from fastapi import status

@app.post("/posts", status_code=status.HTTP_201_CREATED)
def create_post(post: BlogPost):
    return post

Swagger now documents:

  • 201 Created instead of default 200
  • Clear API behavior for consumers

Step 7: Authentication with Swagger (JWT Example)

When I added JWT authentication later, Swagger adapted automatically:

  • “Authorize” button appeared
  • Bearer token support worked out of the box
  • Protected routes showed a lock icon 🔒

This meant:

I could test secure APIs without leaving the browser.


Why FastAPI Felt Easy (But Not Shallow)

FastAPI feels easy because:

  • It leverages Python type hints
  • It enforces good defaults
  • It eliminates repetitive code
  • It gives instant feedback via Swagger

But it’s not simplistic:

  • Async support
  • Dependency injection
  • Security utilities
  • Production-ready performance

Who FastAPI Is Perfect For

FastAPI is ideal if you are:

  • Learning backend development
  • Building REST APIs
  • Creating microservices
  • Serving ML models
  • Tired of undocumented APIs

Final Thoughts

What impressed me most wasn’t just speed—it was clarity.

FastAPI makes you write clean, explicit code, and in return, it gives you:

  • Validation
  • Documentation
  • Performance
  • Confidence

If you are starting backend development with Python today, FastAPI is one of the best places to begin.


fastapi

Link To Check On Amazon

Leave a Comment

Your email address will not be published. Required fields are marked *