Skip to content
/ fastapi-example Public template

A mimimal, modern example of developing APIs in Python with FastAPI

License

Notifications You must be signed in to change notification settings

dalryan/fastapi-example

Repository files navigation

Example FastAPI Project

GitHub Actions Workflow Status

This is a simple template for FastAPI projects. It contains the bare minimum needed to get started developing a modern async Python web API.

Features

  • Simple, extensible API structure with FastAPI
  • Fully asynchronous web server deployed with Uvicorn
  • Multi-stage Dockerfile for efficient image creation.
  • Dependency management and tool config with Poetry.
  • Pre-commit hooks for enforcing standards.
  • Ruff for code style and formatting.
  • MyPy for static type checking.
  • Example sentry integration with FastAPI hooks.

To come

  • Example database integration
  • Example auth with JWT
  • Example integration testing with testcontainers

Getting Started

Make sure you have installed the following:

  • Docker
  • Poetry
  • Pre-commit

Run it locally

  1. Clone this repository:
  2. Build the Docker image: docker build -t my-fastapi-app .
  3. Run the Docker container: docker run -p 8080:8080 my-fastapi-app

Start developing

  1. Clone this repository.
  2. Install pre-commit hooks: pre-commit install
  3. Install dependencies: poetry install
  4. Run the tests: poetry run pytest tests/
  5. Add new dependencies with Poetry: poetry add <package>
  6. Run the server locally: poetry run uvicorn src.main:app --reload

Project Structure

The project is structured as follows:

├── Dockerfile
├── README.md
├── docs
├── mypy.ini
├── poetry.lock
├── pyproject.toml
├── src
│   ├── __init__.py
│   ├── api
│   │   ├── __init__.py
│   │   └── v1
│   │       ├── __init__.py
│   │       ├── api.py
│   │       ├── models.py
│   │       └── routes
│   │           ├── __init__.py
│   │           └── health.py
│   ├── core
│   │   ├── __init__.py
│   │   ├── auth.py
│   │   ├── config.py
│   │   ├── logging.py
│   │   └── middleware.py
│   └── main.py
└── tests
    └── test_v1_route_health.py

To add a new v1 route/endpoint, create a new module in src/api/v1/routes and simply import it in src/api/v1/api.py. To add the v2 version of the route, create a new module in src/api/v2/routes, import it in src/api/v2/api.py, and add a new versioned route in src/api/api.py.

About

A mimimal, modern example of developing APIs in Python with FastAPI

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published