Build a web API from scratch with FastAPI — the workshop

The first FastAPI workshop at PyCon Belarus

Last weekend I had the chance to go to PyCon Belarus, I had a great time and met a lot of great people.

Creating the workshop

When creating the workshop I got a bit excited and created too much content for the time I had available.

The first version of the workshop

The speed of a workshop like this has a constant tradeoff, as there’s always people that finish some part faster than others, so, at some points some people will be “bored” while others will be stressed finishing some part before the next comes.

Source code for the final version

I don’t have an easy way to provide it step by step with all the explanations here, but if you are curious you can still check here the last version of the code.

Create a project directory

Create a directory for the project.

$ mkdir apiapp
$ cd ./apiapp/

Create a Python environment

In the ./apiapp/ directory, create a new 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

Inside of that ./env/ directory, create a file .gitignore with the contents:

*

Activate the environment

Now we need to “activate” the environment.

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

Activate in Windows

If you are in Windows, in Git Bash, activate with:

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

Deactivate an environment

We don’t need to deactivate the environment because we are going to use it. But if you need to deactivate it later, just run:

$ deactivate

Open your editor

Open your editor and select that environment.

Visual Studio Code

If you have Visual Studio Code and a shell like Bash, you can just run:

$ code ./

PyCharm

If you use PyCharm as your editor, open it.

Using the correct environment

Using the correct environment in your editor as we described here and opening it exactly in your project directory will make your editor know the installed packages and will let it provide autocompletion, type checks, relative imports, etc.

Create files

Now, in your editor, create a directory app. It will store all your actual code.

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

Requirements

Edit your requirements.txt to have the following contents:

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

Install requirements

Now install the requirements from that requirements.txt file in the terminal with:

$ pip install -r requirements.txt

Dev requirements

Now, to facilitate development, we’ll also add extra packages that will help us during development.

black
mypy
flake8
requests
pytest
pytest-cov
isort
autoflake

Install dev requirements

And now install the development requirements in the same way:

$ pip install -r dev-requirements.txt

In VS Code

Enable Language Server, mypy, black in the settings.

Reload editor

You might need to reload your editor for it to be able to detect the newly installed packages.

Reload environment

Right after installing new Python packages in your environment, you should activate the environment again:

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

Note: Other package managers

If you used a different environment and package manager like Poetry or Pipenv, the requirements.txt file would be a different file and it would be managed differently, but here we are using the simplest version with the pure/standard Python packages (venv, pip, etc).

The app — version 1

Now we are going to create the first version of our app.

Edit — v1

Edit the file main.py...

Run — v1

Run it:

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

Final Version

The final version of the source code is here: https://github.com/tiangolo/blog-posts/tree/master/pyconby-web-api-from-scratch-with-fastapi/apiapp

Additional scripts

There’s a script to run the tests and report coverage in HTML so that you can explore it in your browser:

$ bash test.sh
$ bash format.sh

About me

You can follow me, contact me, ask questions, see what I do, or use my open source code:

--

--

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.