A modern FastAPI template with user management, API key authentication, PostgreSQL database, and Docker support.
- 🚀 FastAPI - Modern, fast web framework for building APIs
- 🔐 API Key Authentication - Secure endpoints with API keys
- 📦 PostgreSQL - Robust database with SQLAlchemy ORM
- 🐳 Docker - Containerized deployment with Docker Compose
- 📝 Alembic - Database migrations
- 📚 OpenAPI Documentation - Automatic API documentation
- ⚡ Async Support - Built with async/await
- 🔍 Type Checking - Leveraging Python type hints
- 🧪 Testing Ready - Prepared for pytest
- 🎯 Postman Collection - Ready-to-use API collection
- Python 3.12+
- Docker and Docker Compose
- PostgreSQL (if running locally)
- Postman (for using the API collection)
- Clone the repository:
git clone <repository-url>
cd fastapi-template- Start the services using Docker Compose:
make upThis will:
- Build the Docker images
- Start the PostgreSQL database
- Run database migrations
- Start the FastAPI application
The API will be available at: http://localhost:8000
Once the application is running, you can access:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
POST /api/v1/users/- Create a new userGET /api/v1/users/me/- Get current user info (requires authentication)GET /api/v1/users/- List all users (requires authentication)
Protected endpoints require an API key to be sent in the header:
X-API-Key: your-api-key
You'll receive an API key when creating a new user.
- Create a virtual environment:
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Start the PostgreSQL database:
make up db- Run migrations:
make migrate- Start the application:
uvicorn app.main:app --reloadmake build- Build Docker imagesmake up- Start all servicesmake down- Stop all servicesmake logs- View logsmake shell- Open a shell in the API containermake migrate- Run database migrationsmake migrate-down- Rollback last migrationmake test- Run testsmake lint- Run lintermake format- Format code
.
├── app/
│ ├── api/
│ │ └── v1/
│ │ ├── api.py
│ │ └── endpoints/
│ │ └── users.py
│ ├── core/
│ │ ├── auth.py
│ │ └── config.py
│ ├── db/
│ │ ├── base.py
│ │ ├── base_class.py
│ │ └── session.py
│ ├── models/
│ │ └── user.py
│ └── schemas/
│ └── user.py
├── scripts/
│ ├── init.sh
│ └── wait_for_db.py
├── alembic/
│ ├── env.py
│ └── versions/
├── docker-compose.yml
├── Dockerfile
├── requirements.txt
└── README.md
The following environment variables can be set in a .env file:
POSTGRES_SERVER=db
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=fastapi_db- Fork the repository
- Create a new branch
- Make your changes
- Submit a pull request
The project includes a Postman collection for easy API testing:
-
Import the following files into Postman:
FastAPI_Template.postman_collection.json- API endpoints collectionFastAPI_Template.postman_environment.json- Environment variables
-
Select the "FastAPI Template - Local" environment in Postman
-
The collection includes:
- Create User endpoint (POST)
- Get Current User endpoint (GET)
- List Users endpoint (GET)
-
After creating a user, copy the returned API key and:
- Open the "FastAPI Template - Local" environment
- Update the
api_keyvariable with your key - Save the environment
-
Now you can use all authenticated endpoints
This project is licensed under the MIT License - see the LICENSE file for details.