A backend API for a smart item bartering platform, built with FastAPI. This project allows users to register, log in, and post items they wish to trade, creating a foundation for a full-featured trading application.
- User Registration with password hashing
- User Login with JWT Token Authentication
- OAuth2 Password Flow for secure login
- Protected endpoints to require authentication
- Relational database schema with Users and Items
- Alembic database migrations for safe schema changes
- Pydantic schemas for data validation and serialization
- CRUD operations for Items (Create & Read)
- Framework: FastAPI
- Database: PostgreSQL
- ORM: SQLAlchemy
- Database Migrations: Alembic
- Data Validation: Pydantic
- Authentication: JWT Tokens, Passlib (for hashing), python-jose
- Server: Uvicorn
- Containerization: Docker (for PostgreSQL)
Follow these steps to get the project running locally.
git clone https://github.com/diegorbb/tradepost-api.git
cd tradepost-apiIt's highly recommended to use a virtual environment to manage project dependencies.
# For Windows
python -m venv venv
venv\Scripts\activate
# For macOS/Linux
python3 -m venv venv
source venv/bin/activateInstall all the required Python packages from the requirements.txt file.
pip install -r requirements.txtThis project uses a PostgreSQL database. The easiest way to run it locally is with Docker. Make sure you have Docker Desktop installed and running.
# This command will download the official Postgres image and run it in a container.
# It maps port 5433 on your machine to port 5432 in the container.
docker run --name tradepost-db -e POSTGRES_PASSWORD=1234 -p 5433:5432 -d postgresAfter running this, use a database client like DBeaver or pgAdmin to connect to the server and create a new, empty database named tradepost-db.
The application requires environment variables for database credentials and security keys. A .env.example file is provided in the repository.
First, create a copy of the example file.
# For Windows (PowerShell) or macOS/Linux
cp .env.example .env
# For Windows (Command Prompt)
copy .env.example .envNext, open the new .env file and generate a secret key. You can generate one using Python:
python -c "import secrets; print(secrets.token_hex(32))"Paste the generated key as the value for SECRET_KEY in your .env file.
With the database running and the .env file configured, apply all database schema changes using Alembic.
alembic upgrade headFinally, run the FastAPI development server.
uvicorn app.main:app --reloadThe API will now be available at http://127.0.0.1:8000.
Once the server is running, interactive API documentation (provided by Swagger UI) is available at:
This interface allows you to interact with all the API endpoints, handle authentication, and see real-time responses.