Production-Ready API System
A FastAPI based task management API with JWT authentication, role based access control, rate limiting, structured logging, and production ready observability.
- Project Overview
- Features
- Tech Stack
- Setup & Installation
- Environment Variables
- Running Locally
- API Documentation
- Endpoints
- Testing
- Deployment
- Observability & Monitoring
- Rate Limiting
This API provides full CRUD functionality for task management, with secure user authentication, role based access control, Redis based rate limiting, and structured logging. It demonstrates production ready API design patterns, including:
- RESTful resource based routes
- JWT authentication and password hashing
- Rate limiting using Redis
- Asynchronous endpoints and background task capability
- Health checks and observability
- API versioning (URL-based
/v1)
- User Registration & Login (JWT-based)
- CRUD for Tasks (
Create,Read,Update,Delete) - Role based access control (user/admin)
- Rate limiting with Redis (configurable per IP)
- Structured JSON logging with request IDs
- Async external API call example (
/tasks/external-joke) - Health and detailed health endpoints (
/health,/health/detailed) - Dockerized for containerized deployment
- OpenAPI/Swagger documentation
- Framework: FastAPI
- Server: Uvicorn
- Database: SQLite (development)
- Authentication: JWT, bcrypt (via passlib)
- Rate Limiting: Redis
- Async HTTP: httpx
- Testing: pytest, pytest-asyncio, pytest-cov
- Containerization: Docker & Docker Compose
- Clone the repository
git clone https://github.com/Chelsy-AI/Advanced-API-Patterns.git
cd task-management-api- Install dependencies
pip install -r requirements.txt- Start Redis locally (optional if using Docker)
redis-server| Variable | Description | Default |
|---|---|---|
REDIS_URL |
Redis connection string | redis://localhost:6379/0 |
SECRET_KEY |
JWT signing key | 4MRzVM8PWPDNACAUBm+IKR5WEDQB2jXzuLNWeW48tkE= |
Note: For production, make sure to set
SECRET_KEYas a strong, random string.
Option 1: Using Python directly
uvicorn app.main:app --host 0.0.0.0 --port 8000 --reloadOption 2: Using Docker
docker-compose up --build- API will be accessible at
http://localhost:8000 - Redis will be available at
redis://localhost:6379/0
FastAPI automatically provides OpenAPI/Swagger:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
All endpoints, payloads, and response models are documented here.
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /v1/auth/register |
Register new user | No |
| POST | /v1/auth/login |
Login user, returns JWT token | No |
| Method | Endpoint | Description | Auth Required |
|---|---|---|---|
| POST | /v1/tasks/ |
Create a new task | Yes |
| GET | /v1/tasks/ |
List tasks (pagination) | Yes |
| GET | /v1/tasks/{task_id} |
Retrieve a single task | Yes |
| PUT | /v1/tasks/{task_id} |
Update task | Yes |
| DELETE | /v1/tasks/{task_id} |
Delete task | Yes |
| GET | /v1/tasks/external-joke |
Async external API call example | No |
| Method | Endpoint | Description |
|---|---|---|
| GET | /health |
Basic health check |
| GET | /health/detailed |
Database + Redis status check |
Run all tests with pytest:
pytest --cov=app tests/- Ensure minimum 80% test coverage
- Tests include authentication, task CRUD, and health endpoints
- Dockerized with
Dockerfileanddocker-compose.yml - Ready for deployment on platforms like Heroku, Railway, Render, or AWS ECS
- Example Docker Compose command:
docker-compose up --build- API versioning allows safe future upgrades (
/v1/)
- Structured JSON logging with timestamps, log level, and request ID
- Request ID middleware:
X-Request-IDheader included in responses - Global exception handler ensures consistent error messages
- Health endpoints allow load balancer and monitoring integration
- Configurable via Redis (
RATE_LIMIT= 5 requests perRATE_PERIOD= 60s per IP) - Returns
429 Too Many Requestswith remaining time - Included as a FastAPI dependency for protected routes
- Add filtering & sorting for tasks (
/tasks?completed=true&sort=created_at) - Background task processing examples (email notifications, batch operations)
- Response caching using Redis for expensive queries
- Load testing for production deployment