2-how-to-build-restful-apis-with-fastapi-and-postgresql.html

How to Build RESTful APIs with FastAPI and PostgreSQL

In the world of web development, creating RESTful APIs has become a cornerstone of building effective applications. With the rise of microservices architecture, developers are increasingly turning to frameworks that offer speed and simplicity. One such framework is FastAPI, which allows for rapid development of APIs while ensuring high performance. When combined with a robust database like PostgreSQL, developers can create scalable and efficient applications. In this article, we’ll explore how to build RESTful APIs using FastAPI and PostgreSQL, complete with code examples and actionable insights.

What is FastAPI?

FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python type hints. It's designed to create RESTful APIs quickly and efficiently, providing several features:

  • Automatic OpenAPI documentation generation
  • Asynchronous support for high performance
  • Validation of request and response data using Pydantic
  • Dependency injection system for modular design

Why Use PostgreSQL?

PostgreSQL is a powerful, open-source object-relational database system. It is known for its robustness, scalability, and support for advanced data types. Here are some reasons to choose PostgreSQL for your application:

  • ACID compliance for reliable transactions
  • Support for JSON to store semi-structured data
  • Extensible architecture for custom functions and data types
  • Strong community support and extensive documentation

Setting Up Your Environment

Before we dive into coding, let’s set up our environment. You will need:

  • Python 3.6+
  • FastAPI
  • PostgreSQL

Installation

  1. Install FastAPI and Uvicorn (an ASGI server): bash pip install fastapi uvicorn

  2. Install SQLAlchemy and asyncpg for database interaction: bash pip install sqlalchemy asyncpg

  3. Set up PostgreSQL and create a database. You can use a GUI like pgAdmin or the command line: sql CREATE DATABASE fastapi_db;

Building the FastAPI Application

Creating the Project Structure

Create a directory for your project and navigate into it. Inside, create the following files:

/fastapi_project
    ├── main.py
    ├── models.py
    ├── schemas.py
    ├── database.py

Connecting to PostgreSQL

In database.py, set up the connection to PostgreSQL using SQLAlchemy:

from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker

DATABASE_URL = "postgresql+asyncpg://user:password@localhost/fastapi_db"

engine = create_engine(DATABASE_URL, connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()

Defining Models

In models.py, define your database models. For instance, let’s create a simple Item model:

from sqlalchemy import Column, Integer, String
from database import Base

class Item(Base):
    __tablename__ = 'items'

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, index=True)
    description = Column(String)

Creating Pydantic Schemas

In schemas.py, define Pydantic schemas for data validation:

from pydantic import BaseModel

class ItemBase(BaseModel):
    name: str
    description: str

class ItemCreate(ItemBase):
    pass

class Item(ItemBase):
    id: int

    class Config:
        orm_mode = True

Building the API Endpoints

Now, let’s implement the API in main.py:

from fastapi import FastAPI, Depends, HTTPException
from sqlalchemy.orm import Session
from database import SessionLocal, engine
import models, schemas

models.Base.metadata.create_all(bind=engine)

app = FastAPI()

# Dependency to get the database session
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

@app.post("/items/", response_model=schemas.Item)
def create_item(item: schemas.ItemCreate, db: Session = Depends(get_db)):
    db_item = models.Item(name=item.name, description=item.description)
    db.add(db_item)
    db.commit()
    db.refresh(db_item)
    return db_item

@app.get("/items/{item_id}", response_model=schemas.Item)
def read_item(item_id: int, db: Session = Depends(get_db)):
    db_item = db.query(models.Item).filter(models.Item.id == item_id).first()
    if db_item is None:
        raise HTTPException(status_code=404, detail="Item not found")
    return db_item

Running the Application

To run your FastAPI application, execute the following command in your terminal:

uvicorn main:app --reload

Once the server starts, you can access the interactive API documentation at http://127.0.0.1:8000/docs.

Final Thoughts

Building RESTful APIs with FastAPI and PostgreSQL is a powerful combination that leverages Python's simplicity and PostgreSQL's robustness. The steps outlined in this article should provide a solid foundation for creating scalable APIs. As you continue developing, consider exploring additional features of FastAPI, such as dependency injection and middleware, to enhance your application further.

Key Takeaways

  • FastAPI allows you to build high-performance APIs with minimal effort.
  • PostgreSQL provides a reliable and powerful database solution for your applications.
  • The combination of SQLAlchemy and Pydantic offers a structured way to handle data operations and validation.

With these tools in your arsenal, you're well on your way to developing robust web applications. Happy coding!

SR
Syed
Rizwan

About the Author

Syed Rizwan is a Machine Learning Engineer with 5 years of experience in AI, IoT, and Industrial Automation.