Building scalable APIs can be a daunting task, especially when you're trying to balance speed, efficiency, and maintainability. FastAPI Boilerplate addresses this challenge by providing a well-structured, extendable framework that uses modern technologies like FastAPI, Pydantic V2, and SQLAlchemy 2.0. This starter boilerplate is perfect for developers who want to kickstart their projects without diving deep into configuration and setup.
What Is FastAPI Boilerplate?
FastAPI Boilerplate is an async API starter kit designed for rapid development of production-grade applications. It leverages the power of FastAPI to deliver high performance and responsiveness while utilizing Pydantic for data validation and SQLAlchemy for ORM. With built-in features for authentication, background jobs, and caching, it’s an excellent choice for developers who want to get to market quickly without sacrificing quality.
Key Features
- Fully Async: Built on FastAPI and SQLAlchemy 2.0, the boilerplate supports asynchronous programming to handle high traffic efficiently.
- Pydantic V2 Models: Uses the latest Pydantic version for robust data validation and serialization.
- JWT Authentication: Implement access and refresh tokens with secure cookie storage for better user authentication.
- Rate Limiting: Built-in support for rate limiting, allowing you to manage usage tiers (free, pro, etc.) effectively.
- FastCRUD: A convenient CRUD interface that simplifies database interactions and pagination.
- CRUDAdmin Panel: An optional minimal admin panel for managing your API resources with ease.
- Background Jobs with ARQ: Use Redis-based background jobs to handle asynchronous tasks without blocking API requests.
- One-command Docker Setup: With Docker Compose, you can get your API running in minutes.
Installation & Setup
Installing FastAPI Boilerplate is straightforward. Follow these steps to get started:
git clone https://github.com/benavlabs/FastAPI-boilerplate.git
cd FastAPI-boilerplate
# Create a virtual environment
python3 -m venv venv
source venv/bin/activate
# Install dependencies
pip install -r requirements.txt
# Run the application using Docker
docker-compose up
How to Use It
Once you have the setup completed, you can start building your API. Here’s a simple example of how to create a new user endpoint:
from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
username: str
email: str
@app.post("/users/")
async def create_user(user: User):
# Logic to save user to the database
return {"username": user.username, "email": user.email}
This snippet sets up a POST endpoint to create a new user, showcasing how easy it is to implement basic functionality with the FastAPI Boilerplate.
Who Should Use FastAPI Boilerplate?
This boilerplate is ideal for developers who want a pragmatic starter kit packed with essential features out of the box. If you're looking to develop a RESTful API with authentication, background jobs, and caching without getting bogged down by boilerplate code, this is for you. It’s especially beneficial for startups and projects where time to market is critical.
Final Thoughts
FastAPI Boilerplate is a powerful tool for any developer looking to build async APIs with speed and reliability. With its thoughtful architecture and comprehensive features, it reduces the friction of starting new projects. The clear documentation and production-ready defaults ensure that you can focus on building your application instead of wrestling with setup and configuration. If you're looking for a way to streamline your API development process, this boilerplate is definitely worth considering.