Build a web API from scratch with FastAPI — the workshop

This article lives in:

The first FastAPI workshop at PyCon Belarus

I gave a talk there:

And a workshop with about 60 people:

Creating the workshop

The final app ended up having basic OAuth2 authentication, authorization handling with dependencies, tests with full coverage, etc.

I “gave” a test trial of the full workshop to Camila and the total time was about 9 hours, it wasn’t really possible to give it all in 3 hours.

But as it was made in incremental steps, completing a full new version of the app at every step (or every 2 steps), we could start it and go through it, step by step, and advance as much as possible. And wherever we ended up by the end would still be a valid version of the app.

The first version of the workshop

But nevertheless, at the workshop in PyCon Belarus developers were quite fast, and we were able to go up to version 8 of the app, while I was expecting to get only up to about version 5.

But there were 15 versions. So, for those that wanted to see the final version, here it is.

Source code for the final version

It was all based on the same FastAPI documentation, so, if you want to understand it all you can just follow the full FastAPI Tutorial — User Guide.

Below are the initial setup instructions and then the link to the full code of the last version.

Create a project directory

For example ./apiapp/:

$ mkdir apiapp
$ cd ./apiapp/

Create a Python environment

You could be using Poetry, Pipenv, or other tools.

To make it simple we are going to use pure Python with the venv module.

Make sure you have a Python version > 3.6:

$ python --version# OR$ python3.6 --version# OR$ python3.7 --version

Then use that Python 3.6+ to create a new environment for your project.

python -m venv env

That will create a directory ./env/ that will contain a full Python environment, with its own packages, etc.

And in that environment is that we are going to install packages and everything.

Initialize git

$ git init

Ignore that environment in git

*

(that’s a single * in the file).

That will tell git that we want to ignore everything inside that directory.

Activate the environment

This will tell your terminal that when you try to run python it should use the new Python you just installed in that ./env/ directory and not the global one.

Activate the environment:

$ source ./env/bin/activate

To make sure that it worked, check which Python binary is used by your terminal.

$ which python

It should show the path of the new Python, something like:

/home/user/code/apiapp/env/bin/python

Activate in Windows

$ source ./env/Scripts/activate

If you are using PowerShell, activate with:

$ .\env\Scripts\Activate.ps1

To confirm which Python you have in PowerShell use:

$ Get-Command python

Deactivate an environment

$ deactivate

Open your editor

Visual Studio Code

$ code ./

In any case, make sure you select the environment you just created for your editor.

If you use Visual Studio Code, make sure you have the Python Extension.

You can then create a dummy file dummy.py and open it. That will make VS Code load the extension and show the Python environment used.

In the lower left corner you will see the Python version used, if you click it, you can select a different one.

After this, you can delete the dummy.py file.

PyCharm

Select your project directory ./apiapp/ as the workspace.

Then Configure a Python interpreter for your project, and select the interpreter inside of the environment you just created.

Using the correct environment

If you didn’t configure the environment correctly or if you didn’t open it exactly in your project directory (for example, you open one directory above), your editor won’t be able to give you all those features.

Create files

Inside of that app directory, create 2 empty files main.py and __init__.py.

And inside of your project directory, right next to the app directory, create an empty requirements.txt file.

Your file structure should look like:

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

Create a file dev-requirements.txt with:

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

Note: If you are on Windows use the equivalent command.

This will ensure that packages that have a command, like uvicorn will be available in your terminal.

Make sure that uvicorn is available and is the correct version after installing and re-activating the environment:

$ which uvicorn

It should show the uvicorn from your environment.

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

And there is a script to format all the code automatically:

$ 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.

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