FastAPI Best Practices

This page summarizes the projects mentioned and recommended in the original post on reddit.com/r/Python

Our great sponsors
  • Sonar - Write Clean Python Code. Always.
  • InfluxDB - Access the most powerful time series database as a service
  • SaaSHub - Software Alternatives and Reviews
  • fastapi-best-practices

    FastAPI Best Practices and Conventions we used at our startup

  • full-stack-fastapi-postgresql

    Full stack, modern web application generator. Using FastAPI, PostgreSQL as database, Docker, automatic HTTPS and more.

    I would encourage you to take a look at this repo: https://github.com/tiangolo/full-stack-fastapi-postgresql This is a boilerplate of an application made with fastapi, prepared by the creator of the fastapi himself. You can even set it up yourself locally and have a look how it’s organised. I know it has a lot of different services included, but I find the fastapi part itself to be well thought. Inside the api directory you can notice another folder named api_v1, so you can have multiple versions of your API routes when needed, with the general code in other places that is more generic and can be reused in all your different API versions. The schemas are separated from the models and models itself have different classes depending on what you would actually like to do with the data. The migrations are managed with alembic based on schemas rather than models itself. The settings are a python class that implicitly reads the .env file in your project’s directory. And many, many other interesting patterns to explore. Too much to write in one comment to be honest.

  • Sonar

    Write Clean Python Code. Always.. Sonar helps you commit clean code every time. With over 225 unique rules to find Python bugs, code smells & vulnerabilities, Sonar finds the issues while you focus on the work.

  • pre-commit

    A framework for managing and maintaining multi-language pre-commit hooks.

    The last suggestion is down at the bottom you have to use linters. I would recommend adding pre-commit hooks to run those linters. You'll still need to include linters in your CICD pipeline, but if you can setup a pre-commit config and include it in the readme's development instructions, people can catch these issues before they make the initial commit and before wasting CICD cycles. This tip is more for structured organizations - when we onboard a new developer, we pretty much make it one of their first tasks to setup their environment, then make a useless change and merge request so they can validation the pre-commit linters, the pipeline, etc. The MR/PR itself will get closed, but should provide a view of the process.

  • fastapi

    FastAPI framework, high performance, easy to learn, fast to code, ready for production

    Regarding 16. I had lots of issues with Background Tasks when using Middleware. As in https://github.com/tiangolo/fastapi/issues/3859. Has this been fixed? Doesn't look like it

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts