This application is a simple python API using the FastAPI framework, Neon Postgres database, and SQLAlchemy to interact wiht it. It returns a list of authors and books written by them. We illustrate how to generate and run schema change migrations using the alembic
tool.
To build this project from scratch, check out the guide in Neon's documentation.
You will need the following:
- A Neon account and a project
- Python 3.8 or higher
- Clone this repository:
git clone https://github.com/neondatabase/guide-neon-sqlalchemy
- Navigate to the project directory and create a new virtual environment:
cd guide-neon-sqlalchemy
python -m venv myenv
- Activate the virtual environment and install the project dependencies:
source myenv/bin/activate
pip install -r requirements.txt
- Create a
.env
file in the root of the project and add the following environment variable with your Neon database URL:
DATABASE_URL=
- Run the Alembric migrations:
alembic upgrade head
- Run the seed script to populate the database with some data:
python seed.py
- Start the FastAPI server:
python main.py
- Visit
http://localhost:8000
in your browser to see the list of authors and books. Or use curl to access the api from the terminal.
# Get the list of authors
curl http://localhost:8000/authors
# Get books by author with id 1
curl http://localhost:8000/books/1