All projects
Project case study
URL Shortener API
A FastAPI service that validates destination URLs, reuses existing mappings, generates collision-checked short codes, and redirects visitors to the original URL.
FastAPISQLAlchemyPydantic
Open source repositoryThe problem
Turn a submitted URL into a short, reusable link and resolve it with a redirect.
Request flow
- 01HTTP request
- 02Pydantic URL validation
- 03SQLAlchemy mapping lookup
- 04HTTP 302 redirect
Example route: GET /shortner/{short_code}
Implementation decisions
- Validate destinations as HTTP URLs before storing them.
- Use database uniqueness constraints for original URLs and short codes.
- Reuse an existing mapping and retry generated six-character codes after collisions.
Validation and failure handling
- Invalid destinations fail validation.
- Unknown short codes return 404.