How to Build a REST API with FastAPI in Python
Introduction
FastAPI has quickly become one of the most popular web frameworks in the Python ecosystem, especially for building RESTful APIs. It’s fast, modern, and incredibly beginner-friendly. If you’re looking to create APIs that are both easy to write and lightning fast, FastAPI is the way to go.
In this guide, you’ll learn the basics of setting up a REST API using FastAPI — with code examples and a practical explanation for each step. By the end, you’ll be able to build your own API endpoints and test them locally.
Why Choose FastAPI?
FastAPI stands out because of its:
- Speed — powered by Starlette and Pydantic under the hood
- Automatic documentation — Swagger UI and ReDoc included by default
- Type safety — uses Python type hints for data validation
- Ease of use — clean, intuitive syntax that reduces boilerplate code
Step 1: Install FastAPI and Uvicorn
You can install FastAPI and an ASGI server like Uvicorn using pip:
bash
Copy code
pip install fastapi uvicorn
Step 2: Create a Simple API
Create a file called main.py and add the following code:
python
Copy code
from fastapi import FastAPI
app = FastAPI()
@app.get(“/”)
def read_root():
return {“message”: “Welcome to your first FastAPI app!”}
@app.get(“/items/{item_id}”)
def read_item(item_id: int, q: str = None):
return {“item_id”: item_id, “query”: q}
This code sets up two endpoints:
- GET / returns a welcome message
- GET /items/{item_id} handles dynamic path parameters and optional query parameters
Step 3: Run the API Server
To run the FastAPI app, use Uvicorn:
bash
Copy code
uvicorn main:app –reload
- –reload enables auto-reloading when you make code changes.
- Visit http://127.0.0.1:8000 in your browser to see your API.
Step 4: Explore the Built-in Docs
FastAPI automatically provides interactive documentation:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
These tools let you test your API right in the browser — no Postman or external tool required.
Step 5: Add a POST Endpoint
Here’s how to accept data from a user using Pydantic models:
python
Copy code
from pydantic import BaseModel
class Item(BaseModel):
name: str
price: float
is_available: bool = True
@app.post(“/items/”)
def create_item(item: Item):
return {“message”: “Item created”, “item”: item}
Now you can send JSON data in your requests and FastAPI will automatically validate it.
Practice Challenge
Add a new endpoint:
Try adding a PUT /items/{item_id} endpoint to update item data. Use the same Item model and return the updated information.
Practicing basic CRUD operations will help reinforce your understanding of REST principles and FastAPI syntax.
To continue building your skills with Python and FastAPI, explore our full backend development track at:
👉 https://www.thefullstack.co.in/courses/
We offer real-world projects, mentorship, and guided learning paths to help you build production-grade applications confidently.
You might be like this:-
What is AWS Lambda?A Beginner’s Guide to Serverless Computing in 2025
Java vs. Kotlin: Which One Should You Learn for Backend Development?

Leave a Reply