A production-ready link management API built with Clean Architecture, dual-token JWT authentication, and background task processing.
- Clean Architecture & Service Layer - Separation of concerns with dedicated service classes for business logic
- Dual-Token JWT Authentication - Secure access/refresh token system with Redis-backed token invalidation
- Background Task Analytics - Asynchronous click tracking with FastAPI BackgroundTasks
- Redis Caching with TTL - Optimized performance with time-to-live cache management
- Rate Limiting - Protection against brute force attacks on authentication endpoints
- Comprehensive API Testing - Postman collection and detailed testing guide included
- Dockerized Development - Full stack with PostgreSQL, Redis, and FastAPI in containers
Backend Framework
- FastAPI with Uvicorn ASGI server
- Pydantic for data validation
- SQLAlchemy 2.0 (async) with Alembic migrations
Database & Cache
- PostgreSQL 16 for relational data
- Redis 7.2 for caching and token management
Authentication & Security
- JWT with PyJWT
- Password hashing with Passlib
- OWASP security headers
Infrastructure
- Docker & Docker Compose
- Python 3.11 type hints
- Pytest for testing
API Documentation
- Swagger UI at
/docs - ReDoc at
/redoc - OpenAPI 3.0 specification
- Docker 24.0+
- Docker Compose v2+
- 4GB RAM minimum (for PostgreSQL + Redis containers)
-
Clone the repository
git clone https://github.com/yourusername/devlinks-api.git cd devlinks-api -
Set up environment variables
cp .env.example .env
Edit
.envwith your configuration (or use defaults for development) -
Start the development stack
docker-compose up -d
This will start:
- FastAPI API server on
http://localhost:8000 - PostgreSQL database on
localhost:5432 - Redis cache on
localhost:6379
- FastAPI API server on
-
Initialize the database
# First time setup - generate initial migrations docker-compose exec api alembic revision --autogenerate -m "initial" # Apply migrations to database docker-compose exec api alembic upgrade head
-
Access the API
- Swagger UI: http://localhost:8000/docs
- Health Check: http://localhost:8000/health
-
Run tests:
docker-compose exec api pytest -
Run migrations:
# Create new migration docker-compose exec api alembic revision --autogenerate -m "description" # Apply migrations docker-compose exec api alembic upgrade head
-
Rebuild containers:
docker-compose up -d --build
| Endpoint | Method | Description | Authentication |
|---|---|---|---|
/health |
GET | Health check endpoint | ❌ None |
/api/auth/register |
POST | Register new user | ❌ None |
/api/auth/login |
POST | Authenticate user | ❌ None |
/api/auth/refresh |
POST | Refresh access token | ❌ None |
/api/auth/logout |
POST | Invalidate refresh token | ❌ None |
/api/links |
GET | List all user links | ✅ Bearer Token |
/api/links |
POST | Create new link | ✅ Bearer Token |
/api/links/{link_id} |
PUT | Update existing link | ✅ Bearer Token |
/api/links/{link_id} |
DELETE | Delete link | ✅ Bearer Token |
/api/profile/{username} |
GET | Get public profile | ❌ None |
/api/analytics/click |
POST | Record link click | ❌ None |
/api/analytics |
GET | Get user analytics | ✅ Bearer Token |
The repository includes a comprehensive Postman collection:
- File:
DevLinks_API_Collection.postman_collection.json - Features: All endpoints with environment variables, sample requests, and response examples
- Testing Guide:
API_TESTING_GUIDE.mdwith step-by-step testing procedures
# Run all tests
docker-compose exec api pytest
# Run tests with coverage
docker-compose exec api pytest --cov=app --cov-report=term-missing
# Run specific test file
docker-compose exec api pytest tests/test_auth.py- Unit tests for service layer
- Integration tests for API endpoints
- Authentication flow tests
- Rate limiting tests
- Database transaction tests
devlinks-api/
├── app/ # Main application
│ ├── core/ # Core configurations and utilities
│ ├── models/ # Database models
│ ├── routers/ # API route handlers
│ ├── schemas/ # Pydantic models
│ ├── services/ # Business logic layer
│ └── main.py # FastAPI application
├── migrations/ # Alembic migration scripts
├── tests/ # Test suite
├── docker-compose.yml # Docker configuration
├── requirements.txt # Python dependencies
├── .env.example # Environment variables template
├── postman_collection.json # Postman collection
└── API_TESTING_GUIDE.md # Comprehensive testing guide
- JWT Token Security: Dual-token system with short-lived access tokens and long-lived refresh tokens
- Token Invalidation: Redis-backed refresh token invalidation on logout
- Password Security: Bcrypt hashing with work factor 12
- Rate Limiting: 5 requests per minute for login endpoint
- CORS: Configurable CORS origins
- Input Validation: Pydantic model validation for all requests
- Redis Caching: Frequently accessed data with TTL
- Database Connection Pooling: SQLAlchemy async connection pooling
- Background Tasks: Non-blocking analytics processing
- Efficient Queries: Optimized SQLAlchemy queries with proper indexing
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/your-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/your-feature) - Create a new Pull Request
- Follow PEP 8 style guide
- Write comprehensive docstrings
- Add tests for new features
- Update documentation as needed
- Keep dependencies updated
This project is licensed under the MIT License - see the LICENSE file for details.