A modern Python REST API using FastAPI and Supabase for authentication and database operations.
.
├── requirements.txt # Python dependencies
├── README.md # Project documentation
├── .env # Environment variables (not in version control)
├── .gitignore # Git ignore file
└── app/ # Main application package
├── __init__.py
├── main.py # FastAPI application and routing setup
├── config.py # Configuration management
├── auth.py # Authentication utilities
├── models/ # Database models
│ ├── __init__.py
│ └── base.py
├── schemas/ # Pydantic schemas for request/response
│ ├── __init__.py
│ └── base.py
├── api/ # API endpoints
│ ├── __init__.py
│ └── v1/
│ ├── __init__.py
│ ├── deps.py # Dependencies (auth, database)
│ └── endpoints/
│ ├── __init__.py
│ └── auth.py
└── db/ # Database configuration
├── __init__.py
└── client.py # Supabase client setup
- Python 3.8+
- Supabase account and project
-
Clone the repository
git clone <repository-url> cd <project-directory>
-
Create and activate virtual environment
python -m venv venv # On Windows .\venv\Scripts\activate # On macOS/Linux source venv/bin/activate
-
Install dependencies
pip install -r requirements.txt
-
Configure environment variables
Copy
.env.example
to.env
and update with your Supabase credentials:SUPABASE_URL=your-supabase-project-url SUPABASE_KEY=your-supabase-anon-key SUPABASE_SECRET_KEY=your-supabase-service-role-key API_V1_PREFIX=/api/v1 PROJECT_NAME=FastAPI Supabase Project BACKEND_CORS_ORIGINS=["http://localhost:8000"]
-
Start development server
uvicorn app.main:app --reload
The API will be available at
http://localhost:8000
Interactive API documentation (Swagger UI) will be at
http://localhost:8000/docs
POST /api/v1/auth/signup
- Create new user accountPOST /api/v1/auth/login
- Login with email and passwordPOST /api/v1/auth/logout
- Logout current user
GET /protected
- Example protected route (requires authentication)
- Import the provided Postman collection from
postman/collection.json
- Import the environment variables from
postman/environment.json
- Select the "FastAPI Supabase Local" environment
- Start testing the endpoints
- Create a new file in
app/api/v1/endpoints/
- Define your router and endpoints
- Import and include the router in
app/main.py
Example:
# app/api/v1/endpoints/users.py
from fastapi import APIRouter, Depends
from ....auth import get_current_user
router = APIRouter()
@router.get("/users/me")
async def get_my_profile(user = Depends(get_current_user)):
return {"user": user}
The Supabase client is available through dependency injection:
from fastapi import Depends
from ..deps import get_db
@router.get("/items")
async def get_items(db = Depends(get_db)):
return db.from_("items").select("*").execute()
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request