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.
FastAPI - official website
FastAPI - source code (published on Github)
Learn FastAPI by coding - sample project
As posted on the official website, FastAPI key features are listed below:
Fast-to-code and intuitive
Robust - provides interactive documentation
out-of-the-box
Standards-based - fully compatible with
OpenAPI
andJSON 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:
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)
Create/activate a Virtual Environment for Windows Systems
Install FastAPI
Create
main.py
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
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;
Different database support.
Different ORM support.
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;
Custom user class.
Top-level dependency.
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;
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;
The project is based on MVC architectural pattern.
WSGI + ASGI production server.
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;
Some core features include;
Modern admin interface using React-Admin
Integration tests with Cypress
Docker images for frontend and backend
FastAPI Resources
Last updated