Build a web API from scratch with FastAPI — the workshop

The first FastAPI workshop at PyCon Belarus

Creating the workshop

The first version of the workshop

Source code for the final version

Create a project directory

$ mkdir apiapp
$ cd ./apiapp/

Create a Python environment

$ python --version# OR$ python3.6 --version# OR$ python3.7 --version
python -m venv env

Initialize git

$ git init

Ignore that environment in git

*

Activate the environment

$ source ./env/bin/activate
$ which python
/home/user/code/apiapp/env/bin/python

Activate in Windows

$ source ./env/Scripts/activate
$ .\env\Scripts\Activate.ps1
$ Get-Command python

Deactivate an environment

$ deactivate

Open your editor

Visual Studio Code

$ code ./

PyCharm

Using the correct environment

Create files

apiapp
├── app
│ ├── __init__.py
│ └── main.py
├── requirements.txt
└── env
└── ...

Requirements

fastapi
uvicorn
sqlalchemy
async-exit-stack
async-generator
passlib[bcrypt]
python-jose[cryptography]
python-multipart
python-dotenv

Install requirements

$ pip install -r requirements.txt

Dev requirements

black
mypy
flake8
requests
pytest
pytest-cov
isort
autoflake

Install dev requirements

$ pip install -r dev-requirements.txt

In VS Code

Reload editor

Reload environment

$ source ./env/bin/activate
$ which uvicorn

Note: Other package managers

The app — version 1

Edit — v1

Run — v1

$ uvicorn app.main:app --reload
  • Edit main.py again, Uvicorn auto-reloads.

Final Version

Additional scripts

$ bash test.sh
$ bash format.sh

About me

--

--

--

Creator of FastAPI and Typer. Dev at Exposion AI. APIs, Deep Learning/Machine Learning, full-stack distributed systems, SQL/NoSQL, Python, Docker, JS, TS, etc.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

keycloak integration Flutter

How to Install a Plex Media Server on Alibaba Cloud

Why you should learn ASP.NET Core in 2021

S3 and missing rows in Redshift

[23] First pages of the website

How to change your WordPress Website Domain

EC2 Fundamentals-AWS

Performance Evaluation Methods for Distributed MPP Databases — Best Practice for PostgreSQL

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sebastián Ramírez

Sebastián Ramírez

Creator of FastAPI and Typer. Dev at Exposion AI. APIs, Deep Learning/Machine Learning, full-stack distributed systems, SQL/NoSQL, Python, Docker, JS, TS, etc.

More from Medium

How to deploy FastAPI on Heroku using Github.

Python Flask scalable microservices using docker-compose and Nginx load balancer

Production checklist of Websockets using Flask, Celery and Javascript

Eight tips for celery best practices on production