This repository provides a fast and easy starting point for building RESTful APIs with FastAPI. It includes a minimal, yet comprehensive setup to get you up and running quickly, with sensible defaults for projects of any size.
- FastAPI: Fast and modern web framework for building APIs with Python 3.7+.
- SQLAlchemy: ORM for interacting with databases.
- Alembic: For database migrations.
- Pydantic: For data validation and serialization.
- Docker: Containerization setup for easy deployment.
- Testing: Setup for unit tests and integration tests.
- Pre-configured CORS: Easily configurable for cross-origin requests.
- Environment configuration: Manage environment variables with
.envfiles. - Swagger UI: Automatic generation of API documentation.
Make sure you have the following tools installed:
Start by cloning this repository to your local machine:
git clone https://github.com/sid-146/fastapi-template.git
cd fastapi-templateUsing python venv module create a virtual environment
python -m venv venvor
py -m venv venvNavigate to Scripts in virtual environment.
cd venv/Scriptsrun activate script. After this your environment will be activated.
Using pip (for standard Python environments):
pip install -r requirements.txtOr using Poetry:
poetry installCopy the example environment file and edit the values as necessary:
cp .env.example .envMake sure to update your .env file with the appropriate settings for your local environment (e.g., database URL, secret keys).
To run the application, you can use the following command:
uvicorn app.main:app --reloadOr Run using app.py file
cd src
py app.pyThis will start the FastAPI server at http://localhost:8000.
If you prefer using Docker to run the application, you can build and start a container using:
docker-compose up --buildOnce the container is up, the app will be accessible at http://localhost:8000.
fastapi-template/
├── src/
│ ├── app.py # FastAPI application instance
│ ├── models/ # Database models
├── ├── api/
├── ├── ├── __init__.py # Holds all the routers created in routes folder
├── ├── ├── routes/ # Routes of different endpoint
├── ├── ├── middleware/ # Middleware required
├── ├── ├── models/ # Models for request and respones
├── ├── ├── ├── requests/ # Requests models
├── ├── ├── ├── responses/ # Responses models
├── ├── ├── functions/ # Business Logic Functions
├── ├── components/
├── ├── core/ # Core config like server starting config
├── ├── ├── config.py # Config flie
├── ├── db/
├── ├── ├── models/ # DB Models
├── ├── ├── scripts/ # DB Scripts
├── ├── utils/
├── ├── test/
├── alembic/ # Database migrations
├── .env.example # Example environment configuration file
├── Dockerfile # Dockerfile for containerizing the app (todo)
├── docker-compose.yml # Docker Compose configuration (todo)
├── requirements.txt # Python dependencies
└── README.md # This file
To run tests, use the following command:
pytestMake sure that your test cases are properly configured in the tests/ folder.
This template is ready for deployment with Docker and can be deployed on any platform that supports Docker (e.g., AWS, Heroku, DigitalOcean).
You can also deploy directly to a cloud service like Uvicorn with Gunicorn for better production performance.
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to customize this as needed by forking this repository 🍴!