Building a RESTful API with FastAPI in 10 Minutes

Shalinga Manasinghe
2 min readOct 21, 2024

--

If you’re looking to build APIs fast and efficiently, FastAPI is a tool you’ll love. It’s designed to be intuitive, simple, and highly performant — perfect for building RESTful APIs quickly without sacrificing flexibility. In this article, I’ll show you how to set up a basic REST API in FastAPI that handles CRUD operations (Create, Read, Update, Delete) in just 10 minutes.

Why FastAPI?

FastAPI stands out because:

  • It automatically generates interactive API docs (Swagger and ReDoc).
  • It supports asynchronous request handling (using Python’s async/await).
  • It’s easy to learn, using Python type hints for data validation.
  • It’s fast — taking full advantage of Python’s async capabilities for handling requests.

Let’s dive into the code.

Step 1: Install FastAPI and Uvicorn

Start by installing FastAPI and Uvicorn, a lightning-fast ASGI server for running your app.

pip install fastapi uvicorn

Step 2: Create Your First FastAPI App

Create a file, main.py, and add the following code:

from fastapi import FastAPI
app = FastAPI()@app.get("/")
def read_root():
return {"Hello": "World"}

This defines a FastAPI app with a simple GET route that returns a JSON response. To run this app, use Uvicorn:

uvicorn main:app --reload

Now, open your browser and navigate to http://127.0.0.1:8000. You should see:

{
"Hello": "World"
}

Step 3: Add CRUD Operations

Let’s add a few more endpoints to simulate a simple REST API for managing “items.”

from pydantic import BaseModel
class Item(BaseModel):
name: str
description: str = None
price: float
tax: float = None
# Read all items
@app.get("/items/")
def read_items(skip: int = 0, limit: int = 10):
return [{"name": "Item 1"}, {"name": "Item 2"}]
# Read a single item by ID
@app.get("/items/{item_id}")
def read_item(item_id: int):
return {"item_id": item_id, "name": "Item 1"}
# Create a new item
@app.post("/items/")
def create_item(item: Item):
return {"name": item.name, "price": item.price}
# Update an item
@app.put("/items/{item_id}")
def update_item(item_id: int, item: Item):
return {"item_id": item_id, "name": item.name}
# Delete an item
@app.delete("/items/{item_id}")
def delete_item(item_id: int):
return {"message": f"Item {item_id} deleted"}

Step 4: Explore the Interactive API Docs

FastAPI automatically generates interactive API documentation for you. Head over to http://127.0.0.1:8000/docs for Swagger UI or http://127.0.0.1:8000/redoc for ReDoc. These docs let you test the API right in the browser, without writing any extra code for documentation!

Step 5: Conclusion

In just a few lines of code, you now have a fully functional REST API with FastAPI. It supports essential CRUD operations, provides interactive docs out of the box, and leverages Python type hints for data validation.

FastAPI is perfect for developers who need both speed and simplicity. Whether you’re building a small project or scaling to a larger application, FastAPI’s efficiency can help you save time and build better APIs.

--

--

No responses yet