FastAPI backend for a Discord bot project.
- Python 3.10+
pip- PostgreSQL
Create a virtual environment:
python -m venv venvActivate the virtual environment:
For Linux:
source venv/bin/activateFor Windows:
venv\Scripts\Activate.ps1Install the dependencies:
pip install -r requirements.txtCreate your environment file:
cp .env.example .envRun the FastAPI development server:
fastapi dev main.pyRun the database migration:
alembic upgrade headThis project is planned to use PostgreSQL as its database.
Make sure PostgreSQL is installed and running on your machine, then create a database for this project. A typical connection format looks like this:
postgresql://USERNAME:PASSWORD@localhost:5432/DATABASE_NAME
Example:
postgresql://postgres:password@localhost:5432/cody_api
You can place this value in your .env file as DATABASE_URL.
This project includes an example environment file at [.env.example].
Example:
DATABASE_URL=postgresql://postgres:password@localhost:5432/cody_api
INTERNAL_API_KEY=change-this-secret-keyThe project is already set up for SQLAlchemy and Alembic.
Recommended for local development: each developer should use their own local database or a separate dev database. Sharing one database while multiple people experiment with migrations makes it easy for the database state and local migration files to drift apart.
Apply the current migration:
alembic upgrade headCreate a new migration after changing your SQLAlchemy models:
alembic revision --autogenerate -m "describe your change"Apply that new migration:
alembic upgrade headIf Alembic shows an error like Can't locate revision identified by '...', the database is pointing at a revision that does not exist in your local alembic/versions/ folder.
Common causes:
- Someone applied an old migration to the shared database and that migration file was later deleted or renamed.
- A teammate did not pull the same migration files before running
alembic upgrade head. - Multiple developers are using the same database for local experiments.
Safe recovery options:
- Best option: restore the missing migration file into
alembic/versions/and commit it. - If the database is disposable dev data, recreate/reset the database and run
alembic upgrade head. - If the schema already matches the current repo and you only need Alembic to catch up, use
alembic stamp <revision>carefully.
Example for this repo's current revision:
alembic stamp 20260323_000001Use stamp only when you are sure the schema already matches the current migration, because it updates Alembic's version tracking without running SQL changes.
The initial model in this project is a simple tests table with:
idname
There is also a sample insert endpoint:
POST /api/tests
Content-Type: application/json
{
"name": "example"
}The current FastAPI app lives in main.py and exposes a basic root route plus a sample POST /api/tests insert endpoint.