Skip to content

This repository provides a boilerplate code for building a scalable and high-performance API using FastAPI, along with PostgreSQL as the database. This setup is ideal for developers looking to quickly start a project with these technologies.

License

Notifications You must be signed in to change notification settings

karrtik159/python-api-engine

Repository files navigation

Python FastAPI Boilerplate with PostgreSQL

Overview

Python FastAPI Boilerplate with PostgreSQL is a boilerplate that can serve as a base for FastAPI with PostgreSQL.

🚀 FastAPI is a modern, fast (high-performance) web framework for building APIs with Python 3.6+ based on standard Python-type hints. I made and used this boilerplate for projects that I’m working on. 🧑‍💻 it was powerful for building data-driven applications using schema for data validation, serialization, and documentation. 📝

Technology Stacks

  • 🐍 Python 3.11 - A programming language that lets you work quickly and integrate systems more effectively.
  • 🚀 FastAPI - A modern, fast (high-performance), web framework for building APIs with Python 3.8+ based on standard Python type hints.
  • 🐳 Docker - A platform designed to help developers build, share, and run container applications.
  • 🐘 PostgreSQL - An open-source object-relational database system.
  • 💾 SQLAlchemy - the Python SQL toolkit and Object Relational Mapper.
  • 📦 Poetry - A tool for dependency management and packaging in Python.
  • 🧪 Pytest - A framework that makes writing small, readable tests easy.
  • 🦀 Ruff - A fast Python linter and code formatter written in Rust.

Development Features

  • 🚫 JWT authentication, to ensure secure access to the API.
  • 🎢 A Layered architecture (Controller, Service, Repository, Model).
  • 📦 Dependency injection for better code organization.
  • 📝 Swagger and Redoc for API documentation and testing.
  • ♻️ Schema validation, to ensure that input and output data conform to the defined schema.

Requirements

  • Python 3.10+
  • Poetry
  • PostgreSQL
  • Docker

Getting Started

Every command below except build and run is executed in a docker container.

Set Environment Variables

# Copy service env file
$ cp .env.example .env
# Copy db env file
$ cp .db.env.example .db.env

Build and run the app with Docker Compose

# Build docker image
$ docker compose build

# Run the app in the background
$ docker compose up -d

# Watch logs
$ docker compose logs -f

# Execute a command in a running container
$ docker compose exec app <command>

Migrate database

before test or use the app, you need to migrate the database.

# init User table
$ docker compose exec app poetry run alembic upgrade head

Test

# Run unit tests using pytest
$ docker compose exec app poetry run pytest

Lint and format

The Ruff is an extremely fast Python linter and code formatter written in Rust. It can replace flake8, isort, and black at once.

# Run Ruff

## Code linting
$ docker compose exec app poetry run ruff check .

## Code formatting
$ docker compose exec app poetry run ruff format .

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

This repository provides a boilerplate code for building a scalable and high-performance API using FastAPI, along with PostgreSQL as the database. This setup is ideal for developers looking to quickly start a project with these technologies.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published