Getting Started

A comprehensive introduction to FastAPI

FastAPI is an open-source high-performance, easy to learn web framework, at the same time fast to code, and production-ready.

As posted on the official website, FastAPI key features are listed below:

  • Very high performance - thanks to Starlette and Pydantic

  • Fast-to-code and intuitive

  • Robust - provides interactive documentation out-of-the-box

  • Standards-based - fully compatible with OpenAPI and JSON Schema

Simple FastAPI Project

FastAPI requires Python 3.6 (or above) to execute successfully. For most of the Unix environments, Python3 might be already installed. To check the version, open a terminal and type:

$ python3 --version

If the version is 3.6 or above we can move forward and create a virtual environment for our first project powered by FastAPI.

Create/activate a Virtual Environment (Unix systems)

$ virtualenv env
$ source env/bin/activate

Create/activate a Virtual Environment for Windows Systems

$ # virtualenv env
$ # .\env\Scripts\activate

Install FastAPI

$ pip install fastapi
$ pip install uvicorn

Create main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def root():
    return {"message": "Hello FastAPI"}

On line one, we are importing FastAPI which is a python class that provides functionality to our API.

On line three, we have created an instance app for our class, FastAPI which will be the main point of interaction for creating our API.

Line five creates a path operation @app.get("/") to instruct FastAPI that the function below will handle requests going to the / path and the get operation.

Other operations include;

  • @app.post()

  • @app.put()

  • @app.delete()

Line six defines the operation path function. This is a python function that is called by FastAPI when it receives an operation path request.

Line seven returns the specified content.

Save the file and start the project using uvicorn

$ uvicorn main:app --reload
$ 
$ The project is LIVE - https://localhost:8000 

At this point we should be able to visit the API in the browser:

  • API Root: http://localhost:8000

  • OpenAPI Specs: http://localhost:8000/docs

  • Redoc: http://localhost:8000/redoc

Open-Source FastAPI Starters

GitHub has several FastApi starters/boilerplates that almost provide a non-code experience. These templates can be used as starting points for most FastAPI-based projects. Here is a list with a few actively supported FastAPI starters:

This is a flexible general-purpose FastAPI template that allows you to choose between databases and ORMs. Supported databases include SQLAlchemy1.4, TortoiseORM, Piccolo, and Ormar.

Generator features include;

  1. Different database support.

  2. Different ORM support.

  3. Optional Redis support.

This template employs the SQLAlchemy toolkit that provides threadsafe pool implementation. You won't be needing an ORM with this one. Here are some of its cool features;

  1. Custom user class.

  2. Top-level dependency.

  3. Event dispatcher.

This is a user-friendly project since you do not need to fork it. All you need is the cookiecutter CLI. To generate the FastApi project, run;

cookiecutter gh:arthurhenrique/cookiecutter-fastapi

This is a developer productivity tool that makes it easier to create a FastAPI web framework by making assumptions on what we need to get started. Some core features include;

  1. The project is based on MVC architectural pattern.

  2. WSGI + ASGI production server.

  3. Sphinx documentation and 100% unit test coverage.

This is an easy-to-use project that relies on existing frameworks to provide an almost non-code experience. You will need Cookiecutter installed. To generate the project, run;

cookiecutter https://github.com/gaganpreet/fastapi-starter

Some core features include;

  1. Modern admin interface using React-Admin

  2. Integration tests with Cypress

  3. Docker images for frontend and backend

FastAPI Resources

Last updated