FastAPI + JWT + SQLAlchemy + SQLite(or Postgres) demo. The code follows the official document of FastAPI OAuth2 JWT.
Clone the repository
Create virtual env and activate
$ pyenv shell 3.11
$ python -m venv venv
$ source venv/bin/activate
(venv) $ pip install -U pip
Install poetry and dependencies
(venv) $ pip install poetry
(venv) $ poetry install
Start FastAPI app locally
(venv) $ uvicorn main:app --reload
Visit http://localhost:8000/docs
If running the app for the first time, the users table in the database is empty. To be able to log in and use the API, you can send a POST request to endpoint:
http://localhost:8000/users/init
with an empty body. This will create the default superuser defined in ./config.py.
FastAPI has a built-in TestClient class which is based on pytest and requests, this makes it very easy to write tests. To run all test cases:
(venv) $ pytest
This will create a test database named fastapi_app_test.db at the beginning of the test, run all test cases, then remove the database file after finishing the last test case.