Setting up FastAPI with SupabaseDB
Tags: fastapi, supabase, webdev, python
Published: 2023-01-10 13:35:24


## What is Supabase?

Supabase is an open source Firebase alternative. It comes with a Postgres database, Authentication, instant APIs, Edge Functions, Realtime subscriptions, and Storage.

## What is FastAPI?

FastAPI is a modern, fast (high-performance), web framework for building APIs with Python. It's commonly used to rapidly get an API up and running

Here we demonstrate how to make use of Supabase as a Postgres Database and connect it to FastAPI so that we can manage migrations. We will make use of Alembic as a migrations versioning tool. This example also heavily draws reference from the [example provided by FastAPI](https://fastapi.tiangolo.com/tutorial/sql-databases/)


## Setup

Let's first set up a virtual environment and install some dependencies

```python3
virtualenv .env
pip3 install supabase fastapi psycopg2 alembic
```

## Create Base Files

In the `supafast` directory, create the core files by running 
`touch app.py __init__.py database.py models.py schema.py`
the key point to note is that `models.py` contains [pydantic models](https://docs.pydantic.dev) while `schema.py` contains the *database* models which are used to generate the database schema.

## Populating the models and schema file

Here, we create a users model and schema model so that we can create a users table in the database. The working of both files are best explained by [FastAPI's fantastic tutorial](https://fastapi.tiangolo.com/tutorial/sql-databases/#create-model-attributescolumns) so please refer there instead.

### Configure Database URL

Head to `SQLALCHEMY_DATABASE_URL` and change the URL to what's found on your Supabase Dashboard (Screenshot of what it looks like at time of writing)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tce7al6a1f1l0o1x7z5w.png)

Run `alembic init migrations` to generate a `migrations` folder containing all alembic migrations.


### Linking Database models to Alembic

We need to let Alembic view the FastAPI database models we defined in `models.py`. In this case we only have a `User` model so let's import that in out `migrations/env.py` file by adding 

```python3
from supafast.models import User
``` 
in `migrations/env.py`

### Generating and applying the migrations
From here, we can go ahead and generate an initial migration by running

`alembic revision --autogenerate -m "generate initial migration"`

This should create a new version of the migration under `migrations/versions/`

We can then run `alembic upgrade head` to apply the migration changes to Supabase DB. Thereafter, we should be able to see the newly created table in the table editor. 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/grts99mz75czuffk7138.png)



## Conclusion

You can find the final state of the source code [in this Github repository](https://github.com/J0/supabasedb-fastapi-tutorial). If you prefer a video form of this tutorial you can [view the video](https://www.youtube.com/watch?v=6ipV_yAP5I4)

For a comprehensive overview of Authentication, Functions, Storage and other items in the Supabase Python SDK, you can also check out [the Supabase Python Crash Course](https://www.youtube.com/watch?v=M6cfT2pqpSc) kindly put together by Patrick.

If you have any questions, feel free to leave them in the comments below.


References:
[1] https://fastapi.tiangolo.com/tutorial/